valkey.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package valkey
  2. import (
  3. "context"
  4. "time"
  5. "github.com/TecharoHQ/anubis/lib/store"
  6. valkey "github.com/redis/go-redis/v9"
  7. )
  8. // Store implements store.Interface on top of Redis/Valkey.
  9. type Store struct {
  10. client redisClient
  11. }
  12. var _ store.Interface = (*Store)(nil)
  13. func (s *Store) Get(ctx context.Context, key string) ([]byte, error) {
  14. cmd := s.client.Get(ctx, key)
  15. if err := cmd.Err(); err != nil {
  16. if err == valkey.Nil {
  17. return nil, store.ErrNotFound
  18. }
  19. return nil, err
  20. }
  21. return cmd.Bytes()
  22. }
  23. func (s *Store) Set(ctx context.Context, key string, value []byte, expiry time.Duration) error {
  24. return s.client.Set(ctx, key, value, expiry).Err()
  25. }
  26. func (s *Store) Delete(ctx context.Context, key string) error {
  27. res := s.client.Del(ctx, key)
  28. if err := res.Err(); err != nil {
  29. return err
  30. }
  31. if n, _ := res.Result(); n == 0 {
  32. return store.ErrNotFound
  33. }
  34. return nil
  35. }
  36. // IsPersistent tells Anubis this backend is “real” storage, not in-memory.
  37. func (s *Store) IsPersistent() bool {
  38. return true
  39. }