Documentation ¶
Index ¶
- type TimedCache
- func (tc *TimedCache) Add(key, value interface{}) (evicted bool)
- func (tc *TimedCache) Contains(key interface{}) bool
- func (tc *TimedCache) ContainsOrAdd(key, value interface{}) (ok, evicted bool)
- func (tc *TimedCache) Get(key interface{}) (value interface{}, ok bool)
- func (tc *TimedCache) GetOldest() (key, value interface{}, ok bool)
- func (tc *TimedCache) Keys() []interface{}
- func (tc *TimedCache) Len() int
- func (tc *TimedCache) Peek(key interface{}) (value interface{}, ok bool)
- func (tc *TimedCache) PeekOrAdd(key, value interface{}) (previous interface{}, ok, evicted bool)
- func (tc *TimedCache) Purge()
- func (tc *TimedCache) Remove(key interface{}) (present bool)
- func (tc *TimedCache) RemoveOldest() (key, value interface{}, ok bool)
- func (tc *TimedCache) Resize(size int) (evicted int)
- func (tc *TimedCache) Ttl() int64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TimedCache ¶
type TimedCache struct {
// contains filtered or unexported fields
}
TimedCache defines a new cache, where entries are removed after exceeding their ttl. The entry is not guaranteed to live this long (i.e. if it gets evicted when the cache fills up). Conversely, the entry also isn't guaranteed to expire at exactly the ttl time. The expiration mechanism is 'lazy', and will only remove expired objects at next access.
func New ¶
func New(size int, ttl int) (*TimedCache, error)
New creates a new cache with a given size and ttl. TTL defines the time in seconds an entry shall live, before being expired.
func NewWithEvict ¶
func NewWithEvict(size int, ttl int, onEvicted func(key, value interface{})) (*TimedCache, error)
NewWithEvict constructs a fixed size cache with the given ttl & eviction callback.
func (*TimedCache) Add ¶
func (tc *TimedCache) Add(key, value interface{}) (evicted bool)
Add adds a value to the cache. Returns true if an eviction occurred.
func (*TimedCache) Contains ¶
func (tc *TimedCache) Contains(key interface{}) bool
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*TimedCache) ContainsOrAdd ¶
func (tc *TimedCache) ContainsOrAdd(key, value interface{}) (ok, evicted bool)
ContainsOrAdd checks if a key is in the cache without updating the recent-ness, ttl, or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.
func (*TimedCache) Get ¶
func (tc *TimedCache) Get(key interface{}) (value interface{}, ok bool)
Get looks up a key's value from the cache, removing it if it has expired.
func (*TimedCache) GetOldest ¶
func (tc *TimedCache) GetOldest() (key, value interface{}, ok bool)
GetOldest returns the oldest entry
func (*TimedCache) Keys ¶
func (tc *TimedCache) Keys() []interface{}
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*TimedCache) Len ¶
func (tc *TimedCache) Len() int
Len returns the number of items in the cache.
func (*TimedCache) Peek ¶
func (tc *TimedCache) Peek(key interface{}) (value interface{}, ok bool)
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness or ttl of the key.
func (*TimedCache) PeekOrAdd ¶
func (tc *TimedCache) PeekOrAdd(key, value interface{}) (previous interface{}, ok, evicted bool)
PeekOrAdd checks if a key is in the cache without updating the recent-ness, ttl, or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.
func (*TimedCache) Purge ¶
func (tc *TimedCache) Purge()
Purge is used to completely clear the cache.
func (*TimedCache) Remove ¶
func (tc *TimedCache) Remove(key interface{}) (present bool)
Remove removes the provided key from the cache.
func (*TimedCache) RemoveOldest ¶
func (tc *TimedCache) RemoveOldest() (key, value interface{}, ok bool)
RemoveOldest removes the oldest item from the cache.
func (*TimedCache) Resize ¶
func (tc *TimedCache) Resize(size int) (evicted int)
Resize changes the cache size.
func (*TimedCache) Ttl ¶
func (tc *TimedCache) Ttl() int64
Ttl returns the number of seconds each item is allowed to live (except if evicted to free up space)