Documentation ¶
Index ¶
- Variables
- type CacheItem
- func (item *CacheItem) AccessCount() int64
- func (item *CacheItem) AccessedOn() time.Time
- func (item *CacheItem) CreatedOn() time.Time
- func (item *CacheItem) Data() interface{}
- func (item *CacheItem) KeepAlive()
- func (item *CacheItem) Key() interface{}
- func (item *CacheItem) LifeSpan() time.Duration
- func (item *CacheItem) SetAboutToExpireCallback(f func(interface{}))
- type CacheItemPair
- type CacheItemPairList
- type CacheTable
- func (table *CacheTable) Add(key interface{}, lifeSpan time.Duration, data interface{}) *CacheItem
- func (table *CacheTable) Count() int
- func (table *CacheTable) Delete(key interface{}) (*CacheItem, error)
- func (table *CacheTable) Exists(key interface{}) bool
- func (table *CacheTable) Flush()
- func (table *CacheTable) Foreach(trans func(key interface{}, item *CacheItem))
- func (table *CacheTable) MostAccessed(count int64) []*CacheItem
- func (table *CacheTable) NotFoundAdd(key interface{}, lifeSpan time.Duration, data interface{}) bool
- func (table *CacheTable) SetAboutToDeleteItemCallback(f func(*CacheItem))
- func (table *CacheTable) SetAddedItemCallback(f func(*CacheItem))
- func (table *CacheTable) SetDataLoader(f func(interface{}, ...interface{}) *CacheItem)
- func (table *CacheTable) SetLogger(logger *log.Logger)
- func (table *CacheTable) Value(key interface{}, args ...interface{}) (*CacheItem, error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrKeyNotFound = errors.New("Key not found in cache") ErrKeyNotFoundOrLoadable = errors.New("Key not found and could not be loaded into cache") )
Functions ¶
This section is empty.
Types ¶
type CacheItem ¶
Structure of an item in the cache. Parameter data contains the user-set value in the cache.
func CreateCacheItem ¶
Returns a newly created CacheItem. Parameter key is the item's cache-key. Parameter lifeSpan determines after which time period without an access the item will get removed from the cache. Parameter data is the item's value.
func (*CacheItem) AccessCount ¶
Returns how often this item has been accessed.
func (*CacheItem) AccessedOn ¶
Returns when this item was last accessed.
func (*CacheItem) Data ¶
func (item *CacheItem) Data() interface{}
Returns the value of this cached item.
func (*CacheItem) KeepAlive ¶
func (item *CacheItem) KeepAlive()
Mark item to be kept for another expireDuration period.
func (*CacheItem) Key ¶
func (item *CacheItem) Key() interface{}
Returns the key of this cached item.
func (*CacheItem) SetAboutToExpireCallback ¶
func (item *CacheItem) SetAboutToExpireCallback(f func(interface{}))
Configures a callback, which will be called right before the item is about to be removed from the cache.
type CacheItemPair ¶
type CacheItemPair struct { Key interface{} AccessCount int64 }
type CacheItemPairList ¶
type CacheItemPairList []CacheItemPair
A slice of CacheIemPairs that implements sort. Interface to sort by AccessCount.
func (CacheItemPairList) Len ¶
func (p CacheItemPairList) Len() int
func (CacheItemPairList) Less ¶
func (p CacheItemPairList) Less(i, j int) bool
func (CacheItemPairList) Swap ¶
func (p CacheItemPairList) Swap(i, j int)
type CacheTable ¶
Structure of a table with items in the cache.
func Cache ¶
func Cache(table string) *CacheTable
Returns the existing cache table with given name or creates a new one if the table does not exist yet.
func (*CacheTable) Add ¶
func (table *CacheTable) Add(key interface{}, lifeSpan time.Duration, data interface{}) *CacheItem
Adds a key/value pair to the cache. Parameter key is the item's cache-key. Parameter lifeSpan determines after which time period without an access the item will get removed from the cache. Parameter data is the item's value.
func (*CacheTable) Count ¶
func (table *CacheTable) Count() int
Returns how many items are currently stored in the cache.
func (*CacheTable) Delete ¶
func (table *CacheTable) Delete(key interface{}) (*CacheItem, error)
Delete an item from the cache.
func (*CacheTable) Exists ¶
func (table *CacheTable) Exists(key interface{}) bool
Test whether an item exists in the cache. Unlike the Value method Exists neither tries to fetch data via the loadData callback nor does it keep the item alive in the cache.
func (*CacheTable) Foreach ¶
func (table *CacheTable) Foreach(trans func(key interface{}, item *CacheItem))
foreach all items
func (*CacheTable) MostAccessed ¶
func (table *CacheTable) MostAccessed(count int64) []*CacheItem
func (*CacheTable) NotFoundAdd ¶
func (table *CacheTable) NotFoundAdd(key interface{}, lifeSpan time.Duration, data interface{}) bool
Test whether an item not found in the cache. Unlike the Exists method NotExistsAdd also add data if not found.
func (*CacheTable) SetAboutToDeleteItemCallback ¶
func (table *CacheTable) SetAboutToDeleteItemCallback(f func(*CacheItem))
Configures a callback, which will be called every time an item is about to be removed from the cache.
func (*CacheTable) SetAddedItemCallback ¶
func (table *CacheTable) SetAddedItemCallback(f func(*CacheItem))
Configures a callback, which will be called every time a new item is added to the cache.
func (*CacheTable) SetDataLoader ¶
func (table *CacheTable) SetDataLoader(f func(interface{}, ...interface{}) *CacheItem)
Configures a data-loader callback, which will be called when trying to access a non-existing key. The key and 0...n additional arguments are passed to the callback function.
func (*CacheTable) SetLogger ¶
func (table *CacheTable) SetLogger(logger *log.Logger)
Sets the logger to be used by this cache table.
func (*CacheTable) Value ¶
func (table *CacheTable) Value(key interface{}, args ...interface{}) (*CacheItem, error)
Get an item from the cache and mark it to be kept alive. You can pass additional arguments to your DataLoader callback function.