Skip to content

Commit 0dc952b

Browse files
- add ristretto driver
1 parent a1fc1c0 commit 0dc952b

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

ristretto/ristretto.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package ristretto
2+
3+
import (
4+
"context"
5+
"errors"
6+
"time"
7+
8+
"github.com/dgraph-io/ristretto"
9+
"github.com/rocketlaunchr/remember-go"
10+
)
11+
12+
// ErrItemDropped signifies that the item to store was not inserted into the cache.
13+
//
14+
// See: https://godoc.org/github.com/dgraph-io/ristretto#Cache.Set
15+
var ErrItemDropped = errors.New("item dropped")
16+
17+
// RistrettoStore is used to create an in-memory ristretto cache.
18+
//
19+
// See: https://godoc.org/github.com/dgraph-io/ristretto
20+
type RistrettoStore struct {
21+
Cache *ristretto.Cache
22+
}
23+
24+
// NewRistrettoStore creates an in-memory ristretto cache.
25+
//
26+
// See: https://godoc.org/github.com/dgraph-io/ristretto#Config
27+
func NewRistrettoStore(config *ristretto.Config) *RistrettoStore {
28+
cache, err := ristretto.NewCache(config)
29+
if err != nil {
30+
panic(err)
31+
}
32+
33+
return &RistrettoStore{
34+
Cache: cache,
35+
}
36+
}
37+
38+
// Conn does nothing for this storage driver.
39+
func (r *RistrettoStore) Conn(ctx context.Context) (remember.Cacher, error) {
40+
return r, nil
41+
}
42+
43+
// StorePointer sets whether a storage driver requires itemToStore to be
44+
// stored as a pointer or as a concrete value.
45+
func (r *RistrettoStore) StorePointer() bool {
46+
return false
47+
}
48+
49+
// Get returns a value from the cache if the key exists.
50+
// It is possible for nil to be returned while found is also true.
51+
//
52+
// See: https://godoc.org/github.com/dgraph-io/ristretto#Cache.Get
53+
func (r *RistrettoStore) Get(key string) (interface{}, bool, error) {
54+
item, found := r.Cache.Get(key)
55+
return item, found, nil
56+
}
57+
58+
// Set sets a item into the cache for a particular key.
59+
// cost must be converted to a time.Duration despite being unrelated to time.
60+
//
61+
// See: https://godoc.org/github.com/dgraph-io/ristretto#Cache.Set
62+
func (r *RistrettoStore) Set(key string, cost time.Duration, itemToStore interface{}) error {
63+
stored := r.Cache.Set(key, itemToStore, int64(cost))
64+
if stored {
65+
return nil
66+
}
67+
return ErrItemDropped
68+
}
69+
70+
// Close returns the connection back to the pool for storage drivers that utilize a pool.
71+
// For this driver, it does nothing.
72+
func (r *RistrettoStore) Close() {}
73+
74+
// Forget clears the value from the cache for the particular key.
75+
//
76+
// See: https://godoc.org/github.com/dgraph-io/ristretto#Cache.Del
77+
func (r *RistrettoStore) Forget(key string) error {
78+
r.Cache.Del(key)
79+
return nil
80+
}
81+
82+
// ForgetAll clears all values from the cache.
83+
// Note that this is not an atomic operation.
84+
//
85+
// See: https://godoc.org/github.com/dgraph-io/ristretto#Cache.Clear
86+
func (r *RistrettoStore) ForgetAll() error {
87+
r.Cache.Clear()
88+
return nil
89+
}

0 commit comments

Comments
 (0)