-
Notifications
You must be signed in to change notification settings - Fork 0
/
ristretto.go
149 lines (120 loc) · 3.71 KB
/
ristretto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package store
import (
"context"
"errors"
"fmt"
"strings"
"time"
)
//go:generate mockgen -destination=./mock_store_ristretto_interface_test.go -package=store_test -source=ristretto.go
const (
// RistrettoType represents the storage type as a string value
RistrettoType = "ristretto"
// RistrettoTagPattern represents the tag pattern to be used as a key in specified storage
RistrettoTagPattern = "gocache_tag_%s"
)
// RistrettoClientInterface represents a dgraph-io/ristretto client
type RistrettoClientInterface interface {
Get(key any) (any, bool)
SetWithTTL(key, value any, cost int64, ttl time.Duration) bool
Del(key any)
Clear()
}
// RistrettoStore is a store for Ristretto (memory) library
type RistrettoStore struct {
Client RistrettoClientInterface
Options *Options
}
// NewRistretto creates a new store to Ristretto (memory) library instance
func NewRistretto(client RistrettoClientInterface, options ...Option) *RistrettoStore {
return &RistrettoStore{
Client: client,
Options: applyOptions(options...),
}
}
// Get returns data stored from a given key
func (s *RistrettoStore) Get(_ context.Context, key any) (any, error) {
var err error
value, exists := s.Client.Get(key)
if !exists {
err = NotFoundWithCause(errors.New("value not found in Ristretto store"))
}
return value, err
}
// GetWithTTL returns data stored from a given key and its corresponding TTL
func (s *RistrettoStore) GetWithTTL(ctx context.Context, key any) (any, time.Duration, error) {
value, err := s.Get(ctx, key)
return value, 0, err
}
// Set defines data in Ristretto memoey cache for given key identifier
func (s *RistrettoStore) Set(ctx context.Context, key any, value any, options ...Option) error {
opts := ApplyOptionsWithDefault(s.Options, options...)
var err error
if set := s.Client.SetWithTTL(key, value, opts.Cost, opts.Expiration); !set {
err = fmt.Errorf("An error has occurred while setting value '%v' on key '%v'", value, key)
}
if err != nil {
return err
}
if tags := opts.Tags; len(tags) > 0 {
s.setTags(ctx, key, tags)
}
return nil
}
func (s *RistrettoStore) setTags(ctx context.Context, key any, tags []string) {
for _, tag := range tags {
tagKey := fmt.Sprintf(RistrettoTagPattern, tag)
cacheKeys := []string{}
if result, err := s.Get(ctx, tagKey); err == nil {
if bytes, ok := result.([]byte); ok {
cacheKeys = strings.Split(string(bytes), ",")
}
}
alreadyInserted := false
for _, cacheKey := range cacheKeys {
if cacheKey == key.(string) {
alreadyInserted = true
break
}
}
if !alreadyInserted {
cacheKeys = append(cacheKeys, key.(string))
}
_ = s.Set(ctx, tagKey, []byte(strings.Join(cacheKeys, ",")), WithExpiration(720*time.Hour))
}
}
// Delete removes data in Ristretto memoey cache for given key identifier
func (s *RistrettoStore) Delete(_ context.Context, key any) error {
s.Client.Del(key)
return nil
}
// Invalidate invalidates some cache data in Redis for given options
func (s *RistrettoStore) Invalidate(ctx context.Context, options ...InvalidateOption) error {
opts := applyInvalidateOptions(options...)
if tags := opts.Tags; len(tags) > 0 {
for _, tag := range tags {
tagKey := fmt.Sprintf(RistrettoTagPattern, tag)
result, err := s.Get(ctx, tagKey)
if err != nil {
return nil
}
cacheKeys := []string{}
if bytes, ok := result.([]byte); ok {
cacheKeys = strings.Split(string(bytes), ",")
}
for _, cacheKey := range cacheKeys {
_ = s.Delete(ctx, cacheKey)
}
}
}
return nil
}
// Clear resets all data in the store
func (s *RistrettoStore) Clear(_ context.Context) error {
s.Client.Clear()
return nil
}
// GetType returns the store type
func (s *RistrettoStore) GetType() string {
return RistrettoType
}