|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + lru "github.com/hashicorp/golang-lru/simplelru" |
| 9 | + "github.com/prometheus/client_golang/prometheus" |
| 10 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 11 | +) |
| 12 | + |
| 13 | +type LRUCache struct { |
| 14 | + c Cache |
| 15 | + defaultTTL time.Duration |
| 16 | + name string |
| 17 | + |
| 18 | + mtx sync.Mutex |
| 19 | + lru *lru.LRU |
| 20 | + |
| 21 | + requests prometheus.Counter |
| 22 | + hits prometheus.Counter |
| 23 | + items prometheus.GaugeFunc |
| 24 | +} |
| 25 | + |
| 26 | +type Item struct { |
| 27 | + Data []byte |
| 28 | + ExpiresAt time.Time |
| 29 | +} |
| 30 | + |
| 31 | +// WrapWithLRUCache wraps a given `Cache` c with a LRU cache. The LRU cache will always store items in both caches. |
| 32 | +// However it will only fetch items from the underlying cache if the LRU cache doesn't have the item. |
| 33 | +// Items fetched from the underlying cache will be stored in the LRU cache with a default TTL. |
| 34 | +// The LRU cache will also remove items from the underlying cache if they are expired. |
| 35 | +// The LRU cache is limited in number of items using `lruSize`. This means this cache is not tailored for large items or items that have a big |
| 36 | +// variation in size. |
| 37 | +func WrapWithLRUCache(c Cache, name string, reg prometheus.Registerer, lruSize int, defaultTTL time.Duration) (*LRUCache, error) { |
| 38 | + lru, err := lru.NewLRU(lruSize, nil) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + cache := &LRUCache{ |
| 44 | + c: c, |
| 45 | + lru: lru, |
| 46 | + name: name, |
| 47 | + defaultTTL: defaultTTL, |
| 48 | + |
| 49 | + requests: promauto.With(reg).NewCounter(prometheus.CounterOpts{ |
| 50 | + Name: "cache_memory_requests_total", |
| 51 | + Help: "Total number of requests to the in-memory cache.", |
| 52 | + ConstLabels: map[string]string{"name": name}, |
| 53 | + }), |
| 54 | + hits: promauto.With(reg).NewCounter(prometheus.CounterOpts{ |
| 55 | + Name: "cache_memory_hits_total", |
| 56 | + Help: "Total number of requests to the in-memory cache that were a hit.", |
| 57 | + ConstLabels: map[string]string{"name": name}, |
| 58 | + }), |
| 59 | + } |
| 60 | + |
| 61 | + cache.items = promauto.With(reg).NewGaugeFunc(prometheus.GaugeOpts{ |
| 62 | + Name: "cache_memory_items_count", |
| 63 | + Help: "Total number of items currently in the in-memory cache.", |
| 64 | + ConstLabels: map[string]string{"name": name}, |
| 65 | + }, func() float64 { |
| 66 | + cache.mtx.Lock() |
| 67 | + defer cache.mtx.Unlock() |
| 68 | + |
| 69 | + return float64(cache.lru.Len()) |
| 70 | + }) |
| 71 | + |
| 72 | + return cache, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (l *LRUCache) Store(ctx context.Context, data map[string][]byte, ttl time.Duration) { |
| 76 | + // store the data in the shared cache. |
| 77 | + l.c.Store(ctx, data, ttl) |
| 78 | + |
| 79 | + l.mtx.Lock() |
| 80 | + defer l.mtx.Unlock() |
| 81 | + |
| 82 | + for k, v := range data { |
| 83 | + l.lru.Add(k, &Item{ |
| 84 | + Data: v, |
| 85 | + ExpiresAt: time.Now().Add(ttl), |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (l *LRUCache) Fetch(ctx context.Context, keys []string) (result map[string][]byte) { |
| 91 | + l.requests.Add(float64(len(keys))) |
| 92 | + l.mtx.Lock() |
| 93 | + defer l.mtx.Unlock() |
| 94 | + var ( |
| 95 | + found = make(map[string][]byte, len(keys)) |
| 96 | + miss = make([]string, 0, len(keys)) |
| 97 | + now = time.Now() |
| 98 | + ) |
| 99 | + |
| 100 | + for _, k := range keys { |
| 101 | + val, ok := l.lru.Get(k) |
| 102 | + if !ok { |
| 103 | + miss = append(miss, k) |
| 104 | + continue |
| 105 | + } |
| 106 | + item := val.(*Item) |
| 107 | + if item.ExpiresAt.After(now) { |
| 108 | + found[k] = item.Data |
| 109 | + continue |
| 110 | + } |
| 111 | + l.lru.Remove(k) |
| 112 | + miss = append(miss, k) |
| 113 | + |
| 114 | + } |
| 115 | + l.hits.Add(float64(len(found))) |
| 116 | + |
| 117 | + if len(miss) > 0 { |
| 118 | + result = l.c.Fetch(ctx, miss) |
| 119 | + for k, v := range result { |
| 120 | + // we don't know the ttl of the result, so we use the default one. |
| 121 | + l.lru.Add(k, &Item{ |
| 122 | + Data: v, |
| 123 | + ExpiresAt: now.Add(l.defaultTTL), |
| 124 | + }) |
| 125 | + found[k] = v |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return found |
| 130 | +} |
| 131 | + |
| 132 | +func (l *LRUCache) Name() string { |
| 133 | + return "in-memory-" + l.name |
| 134 | +} |
0 commit comments