Import path:
gitlab.soludian.com/soludian/fountain/libs/cache/lru_cache
lru_cache
import "gitlab.soludian.com/soludian/fountain/libs/cache/lru_cache"Package cache implements a LRU cache.
The implementation borrows heavily from SmallLRUCache (originally by Nathan Schrenk). The object maintains a doubly-linked list of elements. When an element is accessed, it is promoted to the head of the list. When space is needed, the element at the tail of the list (the least recently used element) is evicted.
Index
- Constants
- Variables
- func NewLRUCache() cache.Cache
- type Cache
- func NewLRUCacheRaw(capacity int64) *Cache
- func (rc *Cache) ClearAll(ctx context.Context) error
- func (rc *Cache) Close() error
- func (rc *Cache) Decr(key string) (int64, error)
- func (rc *Cache) Delete(key string) error
- func (rc *Cache) Evictions() int64
- func (rc *Cache) Get(key string) ([]byte, error)
- func (rc *Cache) GetDefaultConfig() string
- func (rc *Cache) GetEntry(key string) (*entry, error)
- func (rc *Cache) GetMulti(keys []string) ([][]byte, error)
- func (rc *Cache) Has(key string) bool
- func (rc *Cache) Incr(key string) (int64, error)
- func (rc *Cache) Len() int64
- func (rc *Cache) Peek(key string) (any, error)
- func (rc *Cache) PeekMulti(keys []string) ([]any, error)
- func (rc *Cache) Put(key string, val any, ttl time.Duration) error
- func (rc *Cache) PutMulti(values map[string]any, ttl time.Duration) error
- func (rc *Cache) Reset() error
- func (rc *Cache) Set(key string, val []byte, ttl time.Duration) error
- func (rc *Cache) SetMulti(values map[string][]byte, ttl time.Duration) error
- func (rc *Cache) SetName(name string)
- func (rc *Cache) Size() int64
- func (rc *Cache) StartAndGC(configs ...string) error
- type Item
- type Value
Constants
const (
KPackageName = "lru_cache"
DefaultCapacity = 10 * 1024
)Variables
var DefaultConfig = fmt.Sprintf(`{"capacity": %d}`, DefaultCapacity)func NewLRUCache
func NewLRUCache() cache.CacheNewLRUCache creates a new empty cache with the given capacity.
type Cache
LRUCache is a typical LRU cache implementation. If the cache reaches the capacity, the least recently used item is deleted from the cache. Note the capacity is not the number of items, but the total sum of the Size() of each item.
type Cache struct {
// contains filtered or unexported fields
}func NewLRUCacheRaw
func NewLRUCacheRaw(capacity int64) *CacheNewLRUCache creates a new empty cache with the given capacity.
func (*Cache) ClearAll
func (rc *Cache) ClearAll(ctx context.Context) errorClear all cache.
func (*Cache) Close
func (rc *Cache) Close() errorClose
func (*Cache) Decr
func (rc *Cache) Decr(key string) (int64, error)Decrement a cached int value by key, as a counter.
func (*Cache) Delete
func (rc *Cache) Delete(key string) errorDelete cached value by key. Should not return error if key not found
func (*Cache) Evictions
func (rc *Cache) Evictions() int64func (*Cache) Get
func (rc *Cache) Get(key string) ([]byte, error)Get gets a key's value from memcache.
func (*Cache) GetDefaultConfig
func (rc *Cache) GetDefaultConfig() stringfunc (*Cache) GetEntry
func (rc *Cache) GetEntry(key string) (*entry, error)Peek a cached value by key.
func (*Cache) GetMulti
func (rc *Cache) GetMulti(keys []string) ([][]byte, error)GetMulti gets one or keys values from ssdb.
func (*Cache) Has
func (rc *Cache) Has(key string) boolCheck cache existed by key.
func (*Cache) Incr
func (rc *Cache) Incr(key string) (int64, error)Increment a cached int value by key, as a counter.
func (*Cache) Len
func (rc *Cache) Len() int64is len of list elements
func (*Cache) Peek
func (rc *Cache) Peek(key string) (any, error)Peek a cached value by key.
func (*Cache) PeekMulti
func (rc *Cache) PeekMulti(keys []string) ([]any, error)PeekMulti is a batch version of Get.
func (*Cache) Put
func (rc *Cache) Put(key string, val any, ttl time.Duration) errorSet a cached value with key and expire time.
func (*Cache) PutMulti
func (rc *Cache) PutMulti(values map[string]any, ttl time.Duration) errorSet a cached values with key and expire time
func (*Cache) Reset
func (rc *Cache) Reset() errorfunc (*Cache) Set
func (rc *Cache) Set(key string, val []byte, ttl time.Duration) errorfunc (*Cache) SetMulti
func (rc *Cache) SetMulti(values map[string][]byte, ttl time.Duration) errorfunc (*Cache) SetName
func (rc *Cache) SetName(name string)func (*Cache) Size
func (rc *Cache) Size() int64is Size of all elements, NOT len of list elements
func (*Cache) StartAndGC
func (rc *Cache) StartAndGC(configs ...string) errorStart gc routine based on config string settings. config: must be in the format {"capacity": 1024}
type Item
Item is what is stored in the cache
type Item struct {
Key string
Value Value
}type Value
Value is the interface values that go into LRUCache need to satisfy
type Value interface {
// Size returns how big this value is. If you want to just track
// the cache by number of objects, you may return the size as 1.
Size() int
}Generated by gomarkdoc