Passed
Push — main ( 7e619e...36c76a )
by Yume
01:54
created

ton.CreateRistrettoCache   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
nop 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