Documentation
¶
Overview ¶
Package lru provides LRU cache based on hashicorp's LRU cache that uses size instead of count
The LRU cache is a simple LRU cache that has a memory size limit. You should know the sizes of objects you're adding to the cache, and you should not be adding objects that are larger than the limit. There are no limits on the number of items in the cache as long as the memory size limit is not exceeded.
This is forked from Hashicorp's LRU cache: https://github.com/hashicorp/golang-lru
Index ¶
- type Cache
- func (c *Cache) Add(key, value interface{}, size int) (evicted bool)
- func (c *Cache) Contains(key interface{}) bool
- func (c *Cache) ContainsOrAdd(key, value interface{}, size int) (ok, evicted bool)
- func (c *Cache) Get(key interface{}) (value interface{}, ok bool)
- func (c *Cache) Keys() []interface{}
- func (c *Cache) Len() int
- func (c *Cache) Peek(key interface{}) (value interface{}, ok bool)
- func (c *Cache) Purge()
- func (c *Cache) Remove(key interface{})
- func (c *Cache) RemoveOldest()
- func (c *Cache) Size() int
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 thread-safe fixed size LRU cache.
func NewWithEvict ¶
func NewWithEvict(size int, onEvicted func(key interface{}, value interface{}, size int)) (*Cache, error)
NewWithEvict constructs a fixed size cache with the given eviction callback.
func NewWithTTL ¶
NewWithTTL creates a LRU cache with the given size limit and a TTL for its elements
func NewWithTTLEvict ¶
func NewWithTTLEvict(sizeLimit int, ttl time.Duration, onEvicted func(key interface{}, value interface{}, size int)) (*Cache, error)
NewWithTTLEvict constructs a lru cache with given size limit, ttl for elements, and an onEvicted callback
func (*Cache) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*Cache) ContainsOrAdd ¶
ContainsOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.
func (*Cache) Keys ¶
func (c *Cache) Keys() []interface{}
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*Cache) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*Cache) Remove ¶
func (c *Cache) Remove(key interface{})
Remove removes the provided key from the cache.
func (*Cache) RemoveOldest ¶
func (c *Cache) RemoveOldest()
RemoveOldest removes the oldest item from the cache.