|
| 1 | +package groupcache |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync/atomic" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/groupcache/groupcache-go/v3/transport" |
| 8 | + "github.com/maypok86/otter" |
| 9 | +) |
| 10 | + |
| 11 | +type NowFunc func() time.Time |
| 12 | + |
| 13 | +// OtterCache is an alternative cache implementation which uses a high performance lockless |
| 14 | +// cache suitable for use in high concurrency environments where mutex contention is an issue. |
| 15 | +type OtterCache struct { |
| 16 | + cache otter.Cache[string, transport.ByteView] |
| 17 | + rejected atomic.Int64 |
| 18 | + gets atomic.Int64 |
| 19 | + |
| 20 | + // Now is the Now() function the cache will use to determine |
| 21 | + // the current time which is used to calculate expired values |
| 22 | + // Defaults to time.Now() |
| 23 | + Now NowFunc |
| 24 | +} |
| 25 | + |
| 26 | +// NewOtterCache instantiates a new cache instance |
| 27 | +// |
| 28 | +// Due to the algorithm otter uses to evict and track cache item costs, it is recommended to |
| 29 | +// use a larger maximum byte size when creating Groups via Instance.NewGroup() when using |
| 30 | +// OtterCache if you expect your cached items to be very large. This is because groupcache |
| 31 | +// uses a "Main Cache" and a "Hot Cache" system where the "Hot Cache" is 1/8th the size of |
| 32 | +// the maximum bytes requested. Because Otter cache may reject items added to the cache |
| 33 | +// which are larger than 1/10th of the total capacity of the "Hot Cache" this may result in |
| 34 | +// a lower hit rate for the "Hot Cache" when storing large cache items and penalize the |
| 35 | +// efficiency of groupcache operation. |
| 36 | +// |
| 37 | +// For Example: |
| 38 | +// If you expect the average item in cache to be 100 bytes, and you create a Group with a cache size |
| 39 | +// of 100,000 bytes, then the main cache will be 87,500 bytes and the hot cache will be 12,500 bytes. |
| 40 | +// Since the largest possible item in otter cache is 1/10th of the total size of the cache. Then the |
| 41 | +// largest item that could possibly fit into the hot cache is 1,250 bytes. If you think any of the |
| 42 | +// items you store in groupcache could be larger than 1,250 bytes. Then you should increase the maximum |
| 43 | +// bytes in a Group to accommodate the maximum cache item. If you have no estimate of the maximum size |
| 44 | +// of items in the groupcache, then you should monitor the `Cache.Stats().Rejected` stat for the cache |
| 45 | +// in production and adjust the size accordingly. |
| 46 | +func NewOtterCache(maxBytes int64) (*OtterCache, error) { |
| 47 | + o := &OtterCache{ |
| 48 | + Now: time.Now, |
| 49 | + } |
| 50 | + |
| 51 | + var err error |
| 52 | + o.cache, err = otter.MustBuilder[string, transport.ByteView](int(maxBytes)). |
| 53 | + CollectStats(). |
| 54 | + Cost(func(key string, value transport.ByteView) uint32 { |
| 55 | + return uint32(value.Len()) |
| 56 | + }). |
| 57 | + Build() |
| 58 | + return o, err |
| 59 | +} |
| 60 | + |
| 61 | +// Get returns the item from the cache |
| 62 | +func (o *OtterCache) Get(key string) (transport.ByteView, bool) { |
| 63 | + i, ok := o.cache.Get(key) |
| 64 | + |
| 65 | + // We don't use otter's TTL as it is universal to every item |
| 66 | + // in the cache and groupcache allows users to set a TTL per |
| 67 | + // item stored. |
| 68 | + if !i.Expire().IsZero() && i.Expire().Before(o.Now()) { |
| 69 | + o.cache.Delete(key) |
| 70 | + return transport.ByteView{}, false |
| 71 | + } |
| 72 | + o.gets.Add(1) |
| 73 | + return i, ok |
| 74 | +} |
| 75 | + |
| 76 | +// Add adds the item to the cache. However, otter has the side effect |
| 77 | +// of rejecting an item if the items size (aka, cost) is larger than |
| 78 | +// the capacity (max cost) of the cache divided by 10. |
| 79 | +// |
| 80 | +// If Stats() reports a high number of Rejected items due to large |
| 81 | +// cached items exceeding the maximum cost of the "Hot Cache", then you |
| 82 | +// should increase the size of the cache such that no cache item is |
| 83 | +// larger than the total size of the cache divided by 10. |
| 84 | +// |
| 85 | +// See s3fifo/policy.go NewPolicy() for details |
| 86 | +func (o *OtterCache) Add(key string, value transport.ByteView) { |
| 87 | + if ok := o.cache.Set(key, value); !ok { |
| 88 | + o.rejected.Add(1) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func (o *OtterCache) Remove(key string) { |
| 93 | + o.cache.Delete(key) |
| 94 | +} |
| 95 | + |
| 96 | +func (o *OtterCache) Stats() CacheStats { |
| 97 | + s := o.cache.Stats() |
| 98 | + return CacheStats{ |
| 99 | + Bytes: int64(o.cache.Capacity()), |
| 100 | + Items: int64(o.cache.Size()), |
| 101 | + Rejected: o.rejected.Load(), |
| 102 | + Evictions: s.EvictedCount(), |
| 103 | + Gets: o.gets.Load(), |
| 104 | + Hits: s.Hits(), |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// Bytes always returns 0 bytes used. Otter does not keep track of total bytes, |
| 109 | +// and it is impractical for us to attempt to keep track of total bytes in the |
| 110 | +// cache. Tracking the size of Add and Eviction is easy. However, we must also |
| 111 | +// adjust the total bytes count when items with the same key are replaced. |
| 112 | +// Doing so is more computationally expensive as we must check the cache for an |
| 113 | +// existing item, subtract the existing byte count, then add the new byte count |
| 114 | +// of the replacing item. |
| 115 | +// |
| 116 | +// Arguably reporting the total bytes used is not as useful as hit ratio |
| 117 | +// in a production environment. |
| 118 | +func (o *OtterCache) Bytes() int64 { |
| 119 | + return 0 |
| 120 | +} |
| 121 | + |
| 122 | +func (o *OtterCache) Close() { |
| 123 | + o.cache.Close() |
| 124 | +} |
0 commit comments