You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.0 KiB
Go

package state
import (
"context"
"strings"
"time"
sharedredis "knowfoolery/backend/shared/infra/database/redis"
)
// Store provides redis-backed cache operations.
type Store struct {
redis *sharedredis.Client
}
// NewStore creates cache store.
func NewStore(redisClient *sharedredis.Client) *Store {
return &Store{redis: redisClient}
}
// Get retrieves a cached string value.
func (s *Store) Get(ctx context.Context, key string) (string, bool) {
if s.redis == nil {
return "", false
}
value, err := s.redis.Get(ctx, key)
if err != nil || strings.TrimSpace(value) == "" {
return "", false
}
return value, true
}
// Set writes a cache value with TTL.
func (s *Store) Set(ctx context.Context, key, value string, ttl time.Duration) error {
if s.redis == nil {
return nil
}
return s.redis.Set(ctx, key, value, ttl)
}
// Delete removes cache keys.
func (s *Store) Delete(ctx context.Context, keys ...string) error {
if s.redis == nil || len(keys) == 0 {
return nil
}
return s.redis.Delete(ctx, keys...)
}