Documentation
¶
Overview ¶
Package lru is a simple implementation of an LRU cache. LRU is enhanced by allowing to set a TTL for cached items. It also supports to fetch the values for non-existing keys, by providing a function to fetch these.
Index ¶
- Variables
- type Cache
- type Item
- type LRUCache
- func (l *LRUCache) Add(key interface{}, value interface{}, ttl int64) bool
- func (l *LRUCache) Contains(key interface{}) (ok bool)
- func (l *LRUCache) Expires(key interface{}) (expires time.Time, ok bool)
- func (l *LRUCache) Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error)
- func (l *LRUCache) Get(key interface{}) (value interface{}, ok bool)
- func (l *LRUCache) GetOldest() (key interface{}, value interface{}, ok bool)
- func (l *LRUCache) Keys() []interface{}
- func (l *LRUCache) Len() int
- func (l *LRUCache) Purge()
- func (l *LRUCache) Remove(key interface{}) (ok bool)
- func (l *LRUCache) RemoveOldest() (key interface{}, value interface{}, ok bool)
- type SimpleCache
- func (c *SimpleCache) Add(key interface{}, value interface{}, ttl int64) bool
- func (c *SimpleCache) Contains(key interface{}) (ok bool)
- func (c *SimpleCache) Expires(key interface{}) (expires time.Time, ok bool)
- func (c *SimpleCache) Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error)
- func (c *SimpleCache) Get(key interface{}) (value interface{}, ok bool)
- func (c *SimpleCache) GetOldest() (interface{}, interface{}, bool)
- func (c *SimpleCache) Keys() []interface{}
- func (c *SimpleCache) Len() int
- func (c *SimpleCache) Purge()
- func (c *SimpleCache) Remove(key interface{}) bool
- func (c *SimpleCache) RemoveOldest() (interface{}, interface{}, bool)
- type Sized
Constants ¶
This section is empty.
Variables ¶
var ErrNoPositiveSize = errors.New("lru: must provide a positive size")
ErrNoPositiveSize indicates that the size for the LRU cache must be a positive number.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { // Adds a value to the cache, or updates an item in the cache. // It returns true if an item needed to be removed for storing the new item. Add(key interface{}, value interface{}, ttl int64) bool // Returns the value of the provided key, and updates status of the item // in the cache. Get(key interface{}) (value interface{}, ok bool) // Check if a key exsists in the cache. Contains(key interface{}) (ok bool) // Expires returns the time of expiration. Expires(key interface{}) (expires time.Time, ok bool) // Fetches a value which has expired, or does not exits and fills the cache. Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error) // 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. 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 // Purge is purging the full cache. Purge() }
Cache is the interface for the LRU cache
type Item ¶
type Item struct {
// contains filtered or unexported fields
}
Item represents the internal presentation of a cache entry
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache represents the instance of an LRU cache.
func (*LRUCache) Add ¶
Add is adding a key and value with a TTL to the store. Setting the TTL to 0 signales that this key will not expire.
func (*LRUCache) Fetch ¶
func (l *LRUCache) Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error)
Fetch is fetching a value for key that does not exists or has expired. The fetching is done by a provided function.
func (*LRUCache) Keys ¶
func (l *LRUCache) Keys() []interface{}
Keys returning the keys of the current cache.
func (*LRUCache) RemoveOldest ¶
RemoveOldest removes the oldest item in the cache.
type SimpleCache ¶
cache is a thread-safe, fixed size LRU cache with TTL.
func (*SimpleCache) Add ¶
func (c *SimpleCache) Add(key interface{}, value interface{}, ttl int64) bool
Adds a value to the cache, or updates an item in the cache. It returns true if an item needed to be removed for storing the new item.
func (*SimpleCache) Contains ¶
func (c *SimpleCache) Contains(key interface{}) (ok bool)
Check if a key exsists in the cache.
func (*SimpleCache) Expires ¶
func (c *SimpleCache) Expires(key interface{}) (expires time.Time, ok bool)
Expires returns the time of expiration.
func (*SimpleCache) Fetch ¶
func (c *SimpleCache) Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error)
Fetches a value which has expired, or does not exits and fills the cache.
func (*SimpleCache) Get ¶
func (c *SimpleCache) Get(key interface{}) (value interface{}, ok bool)
Returns the value of the provided key, and updates status of the item in the cache.
func (*SimpleCache) GetOldest ¶
func (c *SimpleCache) GetOldest() (interface{}, interface{}, bool)
Returns the oldest entry from the cache.
func (*SimpleCache) Keys ¶
func (c *SimpleCache) Keys() []interface{}
Returns a slice of the keys in the cache, from oldest to newest.
func (*SimpleCache) Remove ¶
func (c *SimpleCache) Remove(key interface{}) bool
Removes a key from the cache.
func (*SimpleCache) RemoveOldest ¶
func (c *SimpleCache) RemoveOldest() (interface{}, interface{}, bool)
Removes the oldest entry from cache.