|
| 1 | +package valkey |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + lib_store "github.com/eko/gocache/lib/v4/store" |
| 9 | + "github.com/valkey-io/valkey-go" |
| 10 | + "github.com/valkey-io/valkey-go/valkeycompat" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + // ValkeyType represents the storage type as a string value |
| 15 | + ValkeyType = "valkey" |
| 16 | + // ValkeyTagPattern represents the tag pattern to be used as a key in specified storage |
| 17 | + ValkeyTagPattern = "gocache_tag_%s" |
| 18 | + |
| 19 | + defaultClientSideCacheExpiration = 10 * time.Second |
| 20 | +) |
| 21 | + |
| 22 | +// ValkeyStore is a store for Valkey |
| 23 | +type ValkeyStore struct { |
| 24 | + client valkey.Client |
| 25 | + options *lib_store.Options |
| 26 | +} |
| 27 | + |
| 28 | +// NewValkey creates a new store to Valkey instance(s) |
| 29 | +func NewValkey(client valkey.Client, options ...lib_store.Option) *ValkeyStore { |
| 30 | + // defaults client side cache expiration to 10s |
| 31 | + appliedOptions := lib_store.ApplyOptions(options...) |
| 32 | + |
| 33 | + if appliedOptions.ClientSideCacheExpiration == 0 { |
| 34 | + appliedOptions.ClientSideCacheExpiration = defaultClientSideCacheExpiration |
| 35 | + } |
| 36 | + |
| 37 | + return &ValkeyStore{ |
| 38 | + client: client, |
| 39 | + options: appliedOptions, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Get returns data stored from a given key |
| 44 | +func (s *ValkeyStore) Get(ctx context.Context, key any) (any, error) { |
| 45 | + cmd := s.client.B().Get().Key(key.(string)).Cache() |
| 46 | + res := s.client.DoCache(ctx, cmd, s.options.ClientSideCacheExpiration) |
| 47 | + str, err := res.ToString() |
| 48 | + if valkey.IsValkeyNil(err) { |
| 49 | + err = lib_store.NotFoundWithCause(err) |
| 50 | + } |
| 51 | + return str, err |
| 52 | +} |
| 53 | + |
| 54 | +// GetWithTTL returns data stored from a given key and its corresponding TTL |
| 55 | +func (s *ValkeyStore) GetWithTTL(ctx context.Context, key any) (any, time.Duration, error) { |
| 56 | + cmd := s.client.B().Get().Key(key.(string)).Cache() |
| 57 | + res := s.client.DoCache(ctx, cmd, s.options.ClientSideCacheExpiration) |
| 58 | + str, err := res.ToString() |
| 59 | + if valkey.IsValkeyNil(err) { |
| 60 | + err = lib_store.NotFoundWithCause(err) |
| 61 | + } |
| 62 | + return str, time.Duration(res.CacheTTL()) * time.Second, err |
| 63 | +} |
| 64 | + |
| 65 | +// Set defines data in Valkey for given key identifier |
| 66 | +func (s *ValkeyStore) Set(ctx context.Context, key any, value any, options ...lib_store.Option) error { |
| 67 | + opts := lib_store.ApplyOptionsWithDefault(s.options, options...) |
| 68 | + ttl := int64(opts.Expiration.Seconds()) |
| 69 | + var cmd valkey.Completed |
| 70 | + switch value.(type) { |
| 71 | + case string: |
| 72 | + cmd = s.client.B().Set().Key(key.(string)).Value(value.(string)).ExSeconds(ttl).Build() |
| 73 | + |
| 74 | + case []byte: |
| 75 | + cmd = s.client.B().Set().Key(key.(string)).Value(valkey.BinaryString(value.([]byte))).ExSeconds(ttl).Build() |
| 76 | + } |
| 77 | + err := s.client.Do(ctx, cmd).Error() |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + if tags := opts.Tags; len(tags) > 0 { |
| 82 | + s.setTags(ctx, key, tags) |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (s *ValkeyStore) setTags(ctx context.Context, key any, tags []string) { |
| 89 | + ttl := 720 * time.Hour |
| 90 | + for _, tag := range tags { |
| 91 | + tagKey := fmt.Sprintf(ValkeyTagPattern, tag) |
| 92 | + s.client.DoMulti(ctx, |
| 93 | + s.client.B().Sadd().Key(tagKey).Member(key.(string)).Build(), |
| 94 | + s.client.B().Expire().Key(tagKey).Seconds(int64(ttl.Seconds())).Build(), |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// Delete removes data from Valkey for given key identifier |
| 100 | +func (s *ValkeyStore) Delete(ctx context.Context, key any) error { |
| 101 | + return s.client.Do(ctx, s.client.B().Del().Key(key.(string)).Build()).Error() |
| 102 | +} |
| 103 | + |
| 104 | +// Invalidate invalidates some cache data in Valkey for given options |
| 105 | +func (s *ValkeyStore) Invalidate(ctx context.Context, options ...lib_store.InvalidateOption) error { |
| 106 | + opts := lib_store.ApplyInvalidateOptions(options...) |
| 107 | + |
| 108 | + if tags := opts.Tags; len(tags) > 0 { |
| 109 | + for _, tag := range tags { |
| 110 | + tagKey := fmt.Sprintf(ValkeyTagPattern, tag) |
| 111 | + |
| 112 | + cacheKeys, err := s.client.Do(ctx, s.client.B().Smembers().Key(tagKey).Build()).AsStrSlice() |
| 113 | + if err != nil { |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + for _, cacheKey := range cacheKeys { |
| 118 | + s.Delete(ctx, cacheKey) |
| 119 | + } |
| 120 | + |
| 121 | + s.Delete(ctx, tagKey) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +// GetType returns the store type |
| 129 | +func (s *ValkeyStore) GetType() string { |
| 130 | + return ValkeyType |
| 131 | +} |
| 132 | + |
| 133 | +// Clear resets all data in the store |
| 134 | +func (s *ValkeyStore) Clear(ctx context.Context) error { |
| 135 | + return valkeycompat.NewAdapter(s.client).FlushAll(ctx).Err() |
| 136 | +} |
0 commit comments