Documentation ¶
Index ¶
- Constants
- type Cache
- func (cache *Cache) Close() error
- func (cache *Cache) Count() int
- func (cache *Cache) Get(key string) (interface{}, error)
- func (cache *Cache) GetByLoader(key string, customLoaderFunction LoaderFunction) (interface{}, time.Duration, error)
- func (cache *Cache) GetKeys() []string
- func (cache *Cache) GetMetrics() Metrics
- func (cache *Cache) GetWithTTL(key string) (interface{}, time.Duration, error)
- func (cache *Cache) Purge() error
- func (cache *Cache) Remove(key string) error
- func (cache *Cache) Set(key string, data interface{}) error
- func (cache *Cache) SetCacheSizeLimit(limit int)
- func (cache *Cache) SetCheckExpirationCallback(callback CheckExpireCallback)
- func (cache *Cache) SetExpirationCallback(callback ExpireCallback)
- func (cache *Cache) SetExpirationReasonCallback(callback ExpireReasonCallback)
- func (cache *Cache) SetLoaderFunction(loader LoaderFunction)
- func (cache *Cache) SetNewItemCallback(callback ExpireCallback)
- func (cache *Cache) SetTTL(ttl time.Duration) error
- func (cache *Cache) SetWithTTL(key string, data interface{}, ttl time.Duration) error
- func (cache *Cache) SkipTTLExtensionOnHit(value bool)
- func (cache *Cache) Touch(key string) error
- type CheckExpireCallback
- type EvictionReason
- type ExpirationHeap
- func (h *ExpirationHeap) Add(entry ExpirationHeapEntry)
- func (h *ExpirationHeap) First() ExpirationHeapEntry
- func (h ExpirationHeap) Len() int
- func (h ExpirationHeap) Less(i, j int) bool
- func (h *ExpirationHeap) NextExpiration() time.Time
- func (h *ExpirationHeap) Peek() ExpirationHeapEntry
- func (h *ExpirationHeap) Pop() interface{}
- func (h *ExpirationHeap) Push(x interface{})
- func (h *ExpirationHeap) Remove(entry ExpirationHeapEntry)
- func (h ExpirationHeap) Swap(i, j int)
- func (h *ExpirationHeap) Update(entry ExpirationHeapEntry)
- type ExpirationHeapEntry
- type ExpireCallback
- type ExpireReasonCallback
- type LoaderFunction
- type Metrics
- type SimpleCache
Constants ¶
const ( // ErrClosed is raised when operating on a cache where Close() has already been called. ErrClosed = constError("cache already closed") // ErrNotFound indicates that the requested key is not present in the cache ErrNotFound = constError("key not found") )
const ( // ItemNotExpire Will avoid the item being expired by TTL, but can still be exired by callback etc. ItemNotExpire time.Duration = -1 // ItemExpireWithGlobalTTL will use the global TTL when set. ItemExpireWithGlobalTTL time.Duration = 0 )
const EntryNotIndexed = -1
EntryNotIndexed is the index value assigned to an entry that was removed from the heap
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a synchronized map of items that can auto-expire once stale
func (*Cache) Close ¶
Close calls Purge after stopping the goroutine that does ttl checking, for a clean shutdown. The cache is no longer cleaning up after the first call to Close, repeated calls are safe and return ErrClosed.
func (*Cache) Count ¶
Count returns the number of items in the cache. Returns zero when the cache has been closed.
func (*Cache) Get ¶
Get is a thread-safe way to lookup items Every lookup, also touches the item, hence extending it's life
func (*Cache) GetByLoader ¶
func (cache *Cache) GetByLoader(key string, customLoaderFunction LoaderFunction) (interface{}, time.Duration, error)
GetByLoader can take a per key loader function (ie. to propagate context)
func (*Cache) GetKeys ¶
GetKeys returns all keys of items in the cache. Returns nil when the cache has been closed.
func (*Cache) GetMetrics ¶
GetMetrics exposes the metrics of the cache. This is a snapshot copy of the metrics.
func (*Cache) GetWithTTL ¶
GetWithTTL has exactly the same behaviour as Get but also returns the remaining TTL for an specific item at the moment it its retrieved
func (*Cache) Remove ¶
Remove removes an item from the cache if it exists, triggers expiration callback when set. Can return ErrNotFound if the entry was not present.
func (*Cache) SetCacheSizeLimit ¶
SetCacheSizeLimit sets a limit to the amount of cached items. If a new item is getting cached, the closes item to being timed out will be replaced Set to 0 to turn off
func (*Cache) SetCheckExpirationCallback ¶
func (cache *Cache) SetCheckExpirationCallback(callback CheckExpireCallback)
SetCheckExpirationCallback sets a callback that will be called when an item is about to expire in order to allow external code to decide whether the item expires or remains for another TTL cycle
func (*Cache) SetExpirationCallback ¶
func (cache *Cache) SetExpirationCallback(callback ExpireCallback)
SetExpirationCallback sets a callback that will be called when an item expires
func (*Cache) SetExpirationReasonCallback ¶
func (cache *Cache) SetExpirationReasonCallback(callback ExpireReasonCallback)
SetExpirationReasonCallback sets a callback that will be called when an item expires, includes reason of expiry
func (*Cache) SetLoaderFunction ¶
func (cache *Cache) SetLoaderFunction(loader LoaderFunction)
SetLoaderFunction allows you to set a function to retrieve cache misses. The signature matches that of the Get function. Additional Get calls on the same key block while fetching is in progress (groupcache style).
func (*Cache) SetNewItemCallback ¶
func (cache *Cache) SetNewItemCallback(callback ExpireCallback)
SetNewItemCallback sets a callback that will be called when a new item is added to the cache
func (*Cache) SetTTL ¶
SetTTL sets the global TTL value for items in the cache, which can be overridden at the item level.
func (*Cache) SetWithTTL ¶
SetWithTTL is a thread-safe way to add new items to the map with individual ttl.
func (*Cache) SkipTTLExtensionOnHit ¶
SkipTTLExtensionOnHit allows the user to change the cache behaviour. When this flag is set to true it will no longer extend TTL of items when they are retrieved using Get, or when their expiration condition is evaluated using SetCheckExpirationCallback.
type CheckExpireCallback ¶
CheckExpireCallback is used as a callback for an external check on item expiration
type EvictionReason ¶
type EvictionReason int
EvictionReason is an enum that explains why an item was evicted
const ( // Removed : explicitly removed from cache via API call Removed EvictionReason = iota // EvictedSize : evicted due to exceeding the cache size EvictedSize // Expired : the time to live is zero and therefore the item is removed Expired // Closed : the cache was closed Closed )
func EvictionReasonString ¶
func EvictionReasonString(s string) (EvictionReason, error)
EvictionReasonString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
func EvictionReasonValues ¶
func EvictionReasonValues() []EvictionReason
EvictionReasonValues returns all values of the enum
func (EvictionReason) IsAEvictionReason ¶
func (i EvictionReason) IsAEvictionReason() bool
IsAEvictionReason returns "true" if the value is listed in the enum definition. "false" otherwise
func (EvictionReason) String ¶
func (i EvictionReason) String() string
type ExpirationHeap ¶
type ExpirationHeap struct { //A channel used to notify when the first element (index=0) //in the heap has been modified NotifyCh chan struct{} // contains filtered or unexported fields }
ExpirationHeap is the struct used in container/heap
func NewExpirationHeap ¶
func NewExpirationHeap() *ExpirationHeap
NewExpirationHeap creates a new ExpirationHeap
func (*ExpirationHeap) Add ¶
func (h *ExpirationHeap) Add(entry ExpirationHeapEntry)
Add a new entry in the heap
func (*ExpirationHeap) First ¶
func (h *ExpirationHeap) First() ExpirationHeapEntry
First get the first element in the heap (ie: the one with the lowest ttl)
func (ExpirationHeap) Len ¶
func (h ExpirationHeap) Len() int
Len meets the container/heap interface and returns the number of items in the queue
func (ExpirationHeap) Less ¶
func (h ExpirationHeap) Less(i, j int) bool
Less meets the container/heap interface and compare to item within the queue Dont call this directly!
func (*ExpirationHeap) NextExpiration ¶
func (h *ExpirationHeap) NextExpiration() time.Time
NextExpiration gets the lower ttl in the heap. The ttl from the element with index 0
func (*ExpirationHeap) Peek ¶
func (h *ExpirationHeap) Peek() ExpirationHeapEntry
Peek get the first element in the heap without removing it
func (*ExpirationHeap) Pop ¶
func (h *ExpirationHeap) Pop() interface{}
Pop meets the container/heap interface and removes the first item in the heap Don not call this directly!
func (*ExpirationHeap) Push ¶
func (h *ExpirationHeap) Push(x interface{})
Push meets the container/heap interface and inserts an item in the heap Dont call this directly!
func (*ExpirationHeap) Remove ¶
func (h *ExpirationHeap) Remove(entry ExpirationHeapEntry)
Remove removes an entry from the heap. Note that this just try to remove the entry acording to the index it has. It wont check if the object is really the same we are sending.
func (ExpirationHeap) Swap ¶
func (h ExpirationHeap) Swap(i, j int)
Swap meets the container/heap interface and change the position of 2 elements in the queue Dont call this directly!
func (*ExpirationHeap) Update ¶
func (h *ExpirationHeap) Update(entry ExpirationHeapEntry)
Update uptade an entry in the heap
type ExpirationHeapEntry ¶
ExpirationHeapEntry is the interface that any entry we want to insert in the heap should meet. The ExpirationHeap will need to set and get the index used and to know when the entry will expire. Since we are not keeping the index ourself you must be sure than you dont modify the index value in your own entry implementation!! Use this with caution. We are relying in the Set/Get Index methods to keep the objects in the underlying heap. Do not change those ones in your entry object.
type ExpireCallback ¶
type ExpireCallback func(key string, value interface{})
ExpireCallback is used as a callback on item expiration or when notifying of an item new to the cache Note that ExpireReasonCallback will be the succesor of this function in the next major release.
type ExpireReasonCallback ¶
type ExpireReasonCallback func(key string, reason EvictionReason, value interface{})
ExpireReasonCallback is used as a callback on item expiration with extra information why the item expired.
type LoaderFunction ¶
LoaderFunction can be supplied to retrieve an item where a cache miss occurs. Supply an item specific ttl or Duration.Zero
type Metrics ¶
type Metrics struct { // successful inserts Inserted int64 // retrieval attempts Retrievals int64 // all get calls that were in the cache (excludes loader invocations) Hits int64 // entries not in cache (includes loader invocations) Misses int64 // items removed from the cache due to a full size EvictedFull int64 // items removed from the cache due to expiration EvictedExpired int64 // items removed from the cache due to a close call EvictedClosed int64 }
Metrics contains common cache metrics so you can calculate hit and miss rates
type SimpleCache ¶
type SimpleCache interface { Get(key string) (interface{}, error) GetWithTTL(key string) (interface{}, time.Duration, error) Set(key string, data interface{}) error SetTTL(ttl time.Duration) error SetWithTTL(key string, data interface{}, ttl time.Duration) error Remove(key string) error Close() error Purge() error }
SimpleCache interface enables a quick-start. Interface for basic usage.