Documentation ¶
Overview ¶
Package tiny 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 Item
- type LRU
- type LRUCache
- func (lru *LRUCache) Capacity() int64
- func (lru *LRUCache) Clear()
- func (lru *LRUCache) Delete(key interface{}) bool
- func (lru *LRUCache) Evictions() int64
- func (lru *LRUCache) Exist(key interface{}) bool
- func (lru *LRUCache) Get(key interface{}) (v interface{}, ok bool)
- func (lru *LRUCache) Init(capacity int64)
- func (lru *LRUCache) Items() []Item
- func (lru *LRUCache) Keys() []interface{}
- func (lru *LRUCache) Length() int64
- func (lru *LRUCache) Peek(key interface{}) (v interface{}, ok bool)
- func (lru *LRUCache) Set(key interface{}, value interface{})
- func (lru *LRUCache) SetAndGetRemoved(key interface{}, value interface{}) (removedValueList []interface{})
- func (lru *LRUCache) SetCapacity(capacity int64)
- func (lru *LRUCache) SetIfAbsent(key interface{}, value interface{})
- func (lru *LRUCache) Size() int64
- func (lru *LRUCache) Stats() (length, size, capacity, evictions int64)
- func (lru *LRUCache) StatsJSON() string
- type WideLRUCache
- func (w *WideLRUCache) Delete(key interface{}) bool
- func (w *WideLRUCache) Exist(key interface{}) bool
- func (w *WideLRUCache) Get(key interface{}) (v interface{}, ok bool)
- func (w *WideLRUCache) Peek(key interface{}) (v interface{}, ok bool)
- func (w *WideLRUCache) Set(key interface{}, value interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Item ¶
type Item struct { Key interface{} Value interface{} }
Item is what is stored in the cache
type LRU ¶
type LRU interface { // Get returns a value from the cache, and marks the entry as most recently used. Get(key interface{}) (v interface{}, ok bool) // Peek returns a value from the cache without changing the LRU order. Peek(key interface{}) (v interface{}, ok bool) // Exist : return true if key in map Exist(key interface{}) bool // Set sets a value in the cache. Set(key interface{}, value interface{}) // Delete removes an entry from the cache, and returns if the entry existed. Delete(key interface{}) bool }
LRU an interface to define a LRU cache, each v is regard same size
func NewSingleLRUCache ¶
NewSingleLRUCache create a single lru cache
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
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.
func NewLRUCache ¶
NewLRUCache creates a new empty cache with the given capacity.
func (*LRUCache) Get ¶
Get returns a value from the cache, and marks the entry as most recently used.
func (*LRUCache) Items ¶
Items returns all the values for the cache, ordered from most recently used to last recently used.
func (*LRUCache) Keys ¶
func (lru *LRUCache) Keys() []interface{}
Keys returns all the keys for the cache, ordered from most recently used to last recently used.
func (*LRUCache) Set ¶
func (lru *LRUCache) Set(key interface{}, value interface{})
Set sets a value in the cache.
func (*LRUCache) SetAndGetRemoved ¶
func (lru *LRUCache) SetAndGetRemoved(key interface{}, value interface{}) (removedValueList []interface{})
SetAndGetRemoved sets a value in the cache and returns the removed value list
func (*LRUCache) 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 (*LRUCache) SetIfAbsent ¶
func (lru *LRUCache) SetIfAbsent(key interface{}, value interface{})
SetIfAbsent will set the value in the cache if not present. If the value exists in the cache, we don't set it.
type WideLRUCache ¶
type WideLRUCache struct {
// contains filtered or unexported fields
}
WideLRUCache use LruCache group array as a wide lru cache
func (*WideLRUCache) Delete ¶
func (w *WideLRUCache) Delete(key interface{}) bool
Delete removes an entry from the cache, and returns if the entry existed.
func (*WideLRUCache) Exist ¶
func (w *WideLRUCache) Exist(key interface{}) bool
Exist : return true if key in map
func (*WideLRUCache) Get ¶
func (w *WideLRUCache) Get(key interface{}) (v interface{}, ok bool)
Get returns a value from the cache, and marks the entry as most recently used.
func (*WideLRUCache) Peek ¶
func (w *WideLRUCache) Peek(key interface{}) (v interface{}, ok bool)
Peek returns a value from the cache without changing the LRU order.
func (*WideLRUCache) Set ¶
func (w *WideLRUCache) Set(key interface{}, value interface{})
Set sets a value in the cache.