Documentation
¶
Overview ¶
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 ¶
- type Cache
- func (lru *Cache) Capacity() int64
- func (lru *Cache) Clear()
- func (lru *Cache) Delete(key string) bool
- func (lru *Cache) Evictions() int64
- func (lru *Cache) Get(key string) (v Value, ok bool)
- func (lru *Cache) Items() []Item
- func (lru *Cache) Keys() []string
- func (lru *Cache) Length() int64
- func (lru *Cache) Oldest() (oldest time.Time)
- func (lru *Cache) Peek(key string) (v Value, ok bool)
- func (lru *Cache) Set(key string, value Value)
- func (lru *Cache) SetCapacity(capacity int64)
- func (lru *Cache) SetIfAbsent(key string, value Value)
- func (lru *Cache) Size() int64
- func (lru *Cache) Stats() (length, size, capacity, evictions int64, oldest time.Time)
- func (lru *Cache) StatsJSON() string
- type Item
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache 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.
func (*Cache) Items ¶
Items returns all the values for the cache, ordered from most recently used to last recently used.
func (*Cache) Keys ¶
Keys returns all the keys for the cache, ordered from most recently used to last recently used.
func (*Cache) Oldest ¶
Oldest returns the insertion time of the oldest element in the cache, or a IsZero() time if cache is empty.
func (*Cache) SetCapacity ¶
SetCapacity will set the capacity of the cache. If the capacity is smaller, and the current cache size exceed that capacity, the cache will be shrank.
func (*Cache) SetIfAbsent ¶
SetIfAbsent will set the value in the cache if not present. If the value exists in the cache, we don't set it.