blob: ade286ab0ccd417273b70a6601dfb6b166229c46 (
plain)
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
|
// Package cache implements a simple LRU cache which can be completely purged
// whenever needed.
package cache
import (
lru "github.com/hashicorp/golang-lru"
)
// Cache describes an in-memory cache for arbitrary objects, which has a Purge
// method for clearing everything at once.
type Cache interface {
Get(key string) interface{}
Set(key string, value interface{})
Purge()
}
type cache struct {
cache *lru.Cache
}
// New instantiates and returns a new Cache which can hold up to the given
// number of entries.
func New(size int) Cache {
c, err := lru.New(size)
// instantiating the lru cache can't realistically fail
if err != nil {
panic(err)
}
return &cache{c}
}
func (c *cache) Get(key string) interface{} {
value, ok := c.cache.Get(key)
if !ok {
return nil
}
return value
}
func (c *cache) Set(key string, value interface{}) {
c.cache.Add(key, value)
}
func (c *cache) Purge() {
c.cache.Purge()
}
|