-
Notifications
You must be signed in to change notification settings - Fork 11
/
cache_ristretto.go
50 lines (41 loc) · 1.32 KB
/
cache_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
package sqlcache
import (
"context"
"fmt"
"time"
"github.com/prashanthpai/sqlcache/cache"
"github.com/dgraph-io/ristretto"
)
// Ristretto implements cache.Cacher interface to use ristretto as backend with
// go-redis as the redis client library.
type Ristretto struct {
c *ristretto.Cache
}
// Get gets a cache item from ristretto. Returns pointer to the item, a boolean
// which represents whether key exists or not and an error.
func (r *Ristretto) Get(ctx context.Context, key string) (*cache.Item, bool, error) {
i, ok := r.c.Get(key)
if !ok {
return nil, false, nil
}
item, ok := i.(*cache.Item)
if !ok {
return nil, false, fmt.Errorf("Ristretto.Get(): i.(*cache.Item) failed")
}
return item, ok, nil
}
// Set sets the given item into ristretto with provided TTL duration.
func (r *Ristretto) Set(ctx context.Context, key string, item *cache.Item, ttl time.Duration) error {
// using # of rows as cost
_ = r.c.SetWithTTL(key, item, int64(len(item.Rows)), ttl)
return nil
}
// NewRistretto creates a new instance of ristretto backend wrapping the
// provided *ristretto.Cache instance. While creating the ristretto
// instance, please note that number of rows will be used as "cost"
// (in ristretto's terminology) for each cache item.
func NewRistretto(c *ristretto.Cache) *Ristretto {
return &Ristretto{
c: c,
}
}