Total Lines | 63 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package ristretto |
||
2 | |||
3 | import ( |
||
4 | "github.com/dgraph-io/ristretto" |
||
5 | "github.com/dustin/go-humanize" |
||
6 | ) |
||
7 | |||
8 | // Ristretto - Structure for Ristretto |
||
9 | type Ristretto struct { |
||
10 | numCounters int64 |
||
11 | maxCost string |
||
12 | bufferItems int64 |
||
13 | |||
14 | c *ristretto.Cache[string, any] |
||
15 | } |
||
16 | |||
17 | // New - Creates new ristretto cache |
||
18 | func New(opts ...Option) (*Ristretto, error) { |
||
19 | rs := &Ristretto{ |
||
20 | numCounters: _defaultNumCounters, |
||
21 | maxCost: _defaultMaxCost, |
||
22 | bufferItems: 64, |
||
23 | } |
||
24 | |||
25 | // Custom options |
||
26 | for _, opt := range opts { |
||
27 | opt(rs) |
||
28 | } |
||
29 | |||
30 | mc, err := humanize.ParseBytes(rs.maxCost) |
||
31 | if err != nil { |
||
32 | return nil, err |
||
33 | } |
||
34 | |||
35 | c, err := ristretto.NewCache(&ristretto.Config[string, any]{ |
||
36 | NumCounters: rs.numCounters, |
||
37 | MaxCost: int64(mc), |
||
38 | BufferItems: rs.bufferItems, |
||
39 | }) |
||
40 | |||
41 | rs.c = c |
||
42 | |||
43 | return rs, err |
||
44 | } |
||
45 | |||
46 | // Get - Gets value from cache |
||
47 | func (r *Ristretto) Get(key string) (interface{}, bool) { |
||
48 | return r.c.Get(key) |
||
49 | } |
||
50 | |||
51 | // Set - Sets value to cache |
||
52 | func (r *Ristretto) Set(key string, value any, cost int64) bool { |
||
53 | return r.c.Set(key, value, cost) |
||
54 | } |
||
55 | |||
56 | // Wait - Waits for cache to be ready |
||
57 | func (r *Ristretto) Wait() { |
||
58 | r.c.Wait() |
||
59 | } |
||
60 | |||
61 | // Close - Closes cache |
||
62 | func (r *Ristretto) Close() { |
||
63 | r.c.Close() |
||
64 | } |
||
65 |