Documentation ¶
Index ¶
- Constants
- type Cache
- func (cache *Cache) Cleanup()
- func (cache *Cache) Filter(fn CacheFilter) map[string]interface{}
- func (cache *Cache) Get(name string) interface{}
- func (cache *Cache) GetAndRenew(name string) interface{}
- func (cache *Cache) GetAndRenewByIndex(index string, value string) (string, interface{})
- func (cache *Cache) GetByIndex(index string, value string) (string, interface{})
- func (cache *Cache) Index(name string, index string, value string)
- func (cache *Cache) Set(name string, value interface{})
- func (cache *Cache) SetWithTimeout(name string, value interface{}, timeout time.Duration)
- type CacheFilter
- type CacheIndexer
Constants ¶
const Forever time.Duration = -1
Forever can be used as an expiration timeout to make entries valid forever.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct { DefaultTimeout time.Duration // Default expiration timeout for new entries created with cache.Set(...), or a negative numbers to make entries valid forever; defaults to 30 seconds CleanupInterval time.Duration // Minimum interval for automatic cleanups (which will be triggered on any operation on the Cache); the default value 0 disables automatic cleanups Indexer CacheIndexer // Function that maps a name and value of an entry to its index alias values (map[index]value) // contains filtered or unexported fields }
Cache is an in-memory key-value store with expiration handling & automatic cleanups.
func (*Cache) Cleanup ¶
func (cache *Cache) Cleanup()
Cleanup removes expired entries from the cache. It will be called automatically on every cache operation if CleanupInterval is set no a non-zero value.
func (*Cache) Filter ¶
func (cache *Cache) Filter(fn CacheFilter) map[string]interface{}
Filter returns a set of entries from the cache, filtered by a function that returns true if the entry (consisting of name & value) should be included. Note that it can be really slow for large caches!
func (*Cache) Get ¶
Get retrieves an entry from the cache if it exists & is valid, otherwise it returns nil.
func (*Cache) GetAndRenew ¶
GetAndRenew retrieves an entry from the cache if it exists & is valid, otherwise it returns nil. Additionally, it renews the expiration of the entry to the current time + the entry's expiration timeout.
func (*Cache) GetAndRenewByIndex ¶
GetAndRenewByIndex retrieves an entry's name and value from the cache if it exists & is valid, using an index alias instead of the entry's name directly. If the index, the index value, or the entry doesn't exist, it returns nil as a value. Additionally, it renews the expiration of the entry to the current time + the entry's expiration timeout (analog to GetAndRenew).
func (*Cache) GetByIndex ¶
GetByIndex retrieves an entry's name and value from the cache if it exists & is valid, using an index alias instead of the entry's name directly. If the index, the index value, or the entry doesn't exist, it returns nil as a value.
func (*Cache) Index ¶
Index adds an index alias to an entry. If there's e.g. an entry with the name "example", and it has "email" set to "mail@example.org", you can use cache.Index("example","email","mail@example.org) to make it available later with cache.GetByIndex("email","mail@example.org"). Previous index aliases will be overwritten.
type CacheFilter ¶
CacheFilter is a function that returns true if the entry (consisting of name & value) should be included by Cache.Filter.
var All CacheFilter = func(string, interface{}) bool { return true }
All is a filter function that includes all entries.
type CacheIndexer ¶
CacheIndexer is a function that maps a name and value of an entry to its index alias values (map[index]value).