Documentation ¶
Overview ¶
Package simplelru provides simple LRU implementation based on build-in container/list.
Index ¶
- type EvictCallback
- type ExpirableLRU
- func (c *ExpirableLRU) Add(key, value interface{}) (evicted bool)
- func (c *ExpirableLRU) Close()
- func (c *ExpirableLRU) Contains(key interface{}) (ok bool)
- func (c *ExpirableLRU) DeleteExpired()
- func (c *ExpirableLRU) Get(key interface{}) (interface{}, bool)
- func (c *ExpirableLRU) GetOldest() (key, value interface{}, ok bool)
- func (c *ExpirableLRU) Keys() []interface{}
- func (c *ExpirableLRU) Len() int
- func (c *ExpirableLRU) Peek(key interface{}) (interface{}, bool)
- func (c *ExpirableLRU) Purge()
- func (c *ExpirableLRU) Remove(key interface{}) bool
- func (c *ExpirableLRU) RemoveOldest() (key, value interface{}, ok bool)
- func (c *ExpirableLRU) Resize(size int) (evicted int)
- type LRU
- func (c *LRU) Add(key, value interface{}) (evicted bool)
- func (c *LRU) Close()
- func (c *LRU) Contains(key interface{}) (ok bool)
- func (c *LRU) Get(key interface{}) (value interface{}, ok bool)
- func (c *LRU) GetOldest() (key, value interface{}, ok bool)
- func (c *LRU) Keys() []interface{}
- func (c *LRU) Len() int
- func (c *LRU) Peek(key interface{}) (value interface{}, ok bool)
- func (c *LRU) Purge()
- func (c *LRU) Remove(key interface{}) (present bool)
- func (c *LRU) RemoveOldest() (key, value interface{}, ok bool)
- func (c *LRU) Resize(size int) (evicted int)
- type LRUCache
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EvictCallback ¶
type EvictCallback func(key interface{}, value interface{})
EvictCallback is used to get a callback when a cache entry is evicted
type ExpirableLRU ¶
ExpirableLRU implements a thread safe LRU with expirable entries.
Example ¶
// make cache with short TTL and 3 max keys, purgeEvery time.Millisecond * 10 cache := NewExpirableLRU(3, nil, time.Millisecond*5, time.Millisecond*10) // expirable cache need to be closed after used defer cache.Close() // set value under key1. cache.Add("key1", "val1") // get value under key1 r, ok := cache.Get("key1") // check for OK value, because otherwise return would be nil and // type conversion will panic if ok { rstr := r.(string) // convert cached value from interface{} to real type fmt.Printf("value before expiration is found: %v, value: %v\n", ok, rstr) } time.Sleep(time.Millisecond * 11) // get value under key1 after key expiration r, ok = cache.Get("key1") // don't convert to string as with ok == false value would be nil fmt.Printf("value after expiration is found: %v, value: %v\n", ok, r) // set value under key2, would evict old entry because it is already expired. cache.Add("key2", "val2") fmt.Printf("Cache len: %d", cache.Len())
Output: value before expiration is found: true, value: val1 value after expiration is found: false, value: <nil> Cache len: 1
func NewExpirableLRU ¶
func NewExpirableLRU(size int, onEvict EvictCallback, ttl, purgeEvery time.Duration) *ExpirableLRU
NewExpirableLRU returns a new cache with expirable entries.
Size parameter set to 0 makes cache of unlimited size.
Providing 0 TTL turns expiring off.
Activates deleteExpired by purgeEvery duration. If MaxKeys and TTL are defined and PurgeEvery is zero, PurgeEvery will be set to 5 minutes.
func (*ExpirableLRU) Add ¶
func (c *ExpirableLRU) Add(key, value interface{}) (evicted bool)
Add key
func (*ExpirableLRU) Close ¶
func (c *ExpirableLRU) Close()
Close cleans the cache and destroys running goroutines
func (*ExpirableLRU) Contains ¶
func (c *ExpirableLRU) Contains(key interface{}) (ok bool)
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*ExpirableLRU) DeleteExpired ¶
func (c *ExpirableLRU) DeleteExpired()
DeleteExpired clears cache of expired items
func (*ExpirableLRU) Get ¶
func (c *ExpirableLRU) Get(key interface{}) (interface{}, bool)
Get returns the key value
func (*ExpirableLRU) GetOldest ¶
func (c *ExpirableLRU) GetOldest() (key, value interface{}, ok bool)
GetOldest returns the oldest entry
func (*ExpirableLRU) Keys ¶
func (c *ExpirableLRU) Keys() []interface{}
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*ExpirableLRU) Peek ¶
func (c *ExpirableLRU) Peek(key interface{}) (interface{}, bool)
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*ExpirableLRU) Remove ¶
func (c *ExpirableLRU) Remove(key interface{}) bool
Remove key from the cache
func (*ExpirableLRU) RemoveOldest ¶
func (c *ExpirableLRU) RemoveOldest() (key, value interface{}, ok bool)
RemoveOldest removes the oldest item from the cache.
func (*ExpirableLRU) Resize ¶
func (c *ExpirableLRU) Resize(size int) (evicted int)
Resize changes the cache size. size 0 doesn't resize the cache, as it means unlimited.
type LRU ¶
type LRU struct {
// contains filtered or unexported fields
}
LRU implements a non-thread safe fixed size LRU cache
func NewLRU ¶
func NewLRU(size int, onEvict EvictCallback) (*LRU, error)
NewLRU constructs an LRU of the given size
func (*LRU) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*LRU) Keys ¶
func (c *LRU) Keys() []interface{}
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*LRU) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*LRU) Remove ¶
Remove removes the provided key from the cache, returning if the key was contained.
func (*LRU) RemoveOldest ¶
RemoveOldest removes the oldest item from the cache.
type LRUCache ¶
type LRUCache interface { // Adds a value to the cache, returns true if an eviction occurred and // updates the "recently used"-ness of the key. Add(key, value interface{}) bool // Returns key's value from the cache and // updates the "recently used"-ness of the key. #value, isFound Get(key interface{}) (value interface{}, ok bool) // Checks if a key exists in cache without updating the recent-ness. Contains(key interface{}) (ok bool) // Returns key's value without updating the "recently used"-ness of the key. Peek(key interface{}) (value interface{}, ok bool) // Removes a key from the cache. Remove(key interface{}) bool // Removes the oldest entry from cache. RemoveOldest() (interface{}, interface{}, bool) // Returns the oldest entry from the cache. #key, value, isFound GetOldest() (interface{}, interface{}, bool) // Returns a slice of the keys in the cache, from oldest to newest. Keys() []interface{} // Returns the number of items in the cache. Len() int // Clears all cache entries. Purge() // Closes all hanging goroutines. Close() // Resizes cache, returning number evicted Resize(int) int }
LRUCache is the interface for simple LRU cache.