github.com/bhojpur/cache@v0.0.4/pkg/engine/cache.go (about) 1 package engine 2 3 // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved. 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 // Cache is a generic interface type for a data structure that keeps recently used 24 // objects in memory and evicts them when it becomes full. 25 type Cache interface { 26 Get(key string) (interface{}, bool) 27 Set(key string, val interface{}) bool 28 ForEach(callback func(interface{}) bool) 29 30 Delete(key string) 31 Clear() 32 33 // Wait waits for all pending operations on the cache to settle. Since cache writes 34 // are asynchronous, a write may not be immediately accessible unless the user 35 // manually calls Wait. 36 Wait() 37 38 Len() int 39 Evictions() int64 40 UsedCapacity() int64 41 MaxCapacity() int64 42 SetCapacity(int64) 43 } 44 45 type cachedObject interface { 46 CachedSize(alloc bool) int64 47 } 48 49 // NewDefaultCacheImpl returns the default cache implementation for Vitess. The options in the 50 // Config struct control the memory and entry limits for the cache, and the underlying cache 51 // implementation. 52 func NewDefaultCacheImpl(cfg *Config) Cache { 53 switch { 54 case cfg == nil: 55 return &nullCache{} 56 57 case cfg.LFU: 58 if cfg.MaxEntries == 0 || cfg.MaxMemoryUsage == 0 { 59 return &nullCache{} 60 } 61 return NewRistrettoCache(cfg.MaxEntries, cfg.MaxMemoryUsage, func(val interface{}) int64 { 62 return val.(cachedObject).CachedSize(true) 63 }) 64 65 default: 66 if cfg.MaxEntries == 0 { 67 return &nullCache{} 68 } 69 return NewLRUCache(cfg.MaxEntries, func(_ interface{}) int64 { 70 return 1 71 }) 72 } 73 } 74 75 // Config is the configuration options for a cache instance 76 type Config struct { 77 // MaxEntries is the estimated amount of entries that the cache will hold at capacity 78 MaxEntries int64 79 // MaxMemoryUsage is the maximum amount of memory the cache can handle 80 MaxMemoryUsage int64 81 // LFU toggles whether to use a new cache implementation with a TinyLFU admission policy 82 LFU bool 83 } 84 85 // DefaultConfig is the default configuration for a cache instance in Vitess 86 var DefaultConfig = &Config{ 87 MaxEntries: 5000, 88 MaxMemoryUsage: 32 * 1024 * 1024, 89 LFU: true, 90 }