Total Lines | 49 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package infrastructures |
||
2 | |||
3 | import ( |
||
4 | "sync" |
||
5 | |||
6 | "github.com/dgraph-io/ristretto" |
||
7 | "github.com/memnix/memnix-rest/config" |
||
8 | ) |
||
9 | |||
10 | // CacheSingleton is the singleton for the ristretto cache. |
||
11 | type CacheSingleton struct { |
||
12 | cache *ristretto.Cache |
||
13 | } |
||
14 | |||
15 | var ( |
||
16 | cacheInstance *CacheSingleton //nolint:gochecknoglobals //Singleton |
||
17 | cacheOnce sync.Once //nolint:gochecknoglobals //Singleton |
||
18 | ) |
||
19 | |||
20 | // GetCacheInstance gets the cache instance. |
||
21 | func GetCacheInstance() *CacheSingleton { |
||
22 | cacheOnce.Do(func() { |
||
23 | cacheInstance = &CacheSingleton{} |
||
24 | }) |
||
25 | return cacheInstance |
||
26 | } |
||
27 | |||
28 | // CreateRistrettoCache creates a new ristretto cache. |
||
29 | func (c *CacheSingleton) CreateRistrettoCache() error { |
||
30 | var err error |
||
31 | if c.cache, err = ristretto.NewCache(&ristretto.Config{ |
||
32 | NumCounters: config.RistrettoNumCounters, // number of keys to track frequency of (10M). |
||
33 | MaxCost: config.RistrettoMaxCost, // maximum cost of cache (1GB). |
||
34 | BufferItems: config.RistrettoBufferItems, // number of keys per Get buffer. |
||
35 | }); err != nil { |
||
36 | return err |
||
37 | } |
||
38 | |||
39 | return nil |
||
40 | } |
||
41 | |||
42 | // GetRistrettoCache gets the ristretto cache. |
||
43 | func (c *CacheSingleton) GetRistrettoCache() *ristretto.Cache { |
||
44 | return c.cache |
||
45 | } |
||
46 | |||
47 | // GetRistrettoCache gets the ristretto cache. |
||
48 | func GetRistrettoCache() *ristretto.Cache { |
||
49 | return GetCacheInstance().GetRistrettoCache() |
||
50 | } |
||
51 |