Documentation ¶
Index ¶
- type ConcurrentRingCache
- func (c *ConcurrentRingCache) EachSubCache(f func(*LIFOCache))
- func (c *ConcurrentRingCache) EnforceSizeLimit()
- func (c *ConcurrentRingCache) EvictOrderThan(tm int64)
- func (c *ConcurrentRingCache) Get(key string) (interface{}, bool)
- func (c *ConcurrentRingCache) Put(key string, item interface{})
- func (c *ConcurrentRingCache) PutWithHandler(key string, item interface{}, evictionHandler func(string, interface{}))
- func (c *ConcurrentRingCache) SetTimeFunction(timeFunction func() int64)
- func (c *ConcurrentRingCache) Size() int
- type LIFOCache
- func (c *LIFOCache) EvictNext() (string, interface{})
- func (c *LIFOCache) EvictOlderThan(tm int64)
- func (c *LIFOCache) Get(key string) (interface{}, int64, bool)
- func (c *LIFOCache) Len() int
- func (c *LIFOCache) Less(i, j int) bool
- func (c *LIFOCache) MinItem() interface{}
- func (c *LIFOCache) MinKey() string
- func (c *LIFOCache) MinTime() int64
- func (c *LIFOCache) Pop() interface{}
- func (c *LIFOCache) Push(x interface{})
- func (c *LIFOCache) Put(key string, item interface{}) (interface{}, bool)
- func (c *LIFOCache) PutWithHandler(key string, item interface{}, evictionhandler func(string, interface{})) (interface{}, bool)
- func (c *LIFOCache) Remove(key string) (interface{}, bool)
- func (c *LIFOCache) SetAddedTime(key string, tm int64)
- func (c *LIFOCache) Swap(i, j int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConcurrentRingCache ¶
type ConcurrentRingCache struct { SizeLimit int RingSize int AgeLimit int64 Caches []*LIFOCache Locks []*sync.RWMutex // A function that hashes a string into one of the caches in the Caches array. // The ringSize is the length of the Cache and Locks arrays. KeyHash func(key string, ringSize int) int }
A cache that is comprised other caches in a ring.
Each cache is protected with a Mutex that is locked for write operations.
Keys are hashed into the ring and added to their respective caches.
func NewConcurrentRingCache ¶
func NewConcurrentRingCache(ringSize int, cacheSize int, ageLimit int64) *ConcurrentRingCache
Create a new concurrent ring cache. ringSize is how many independent caches will be created. cacheSize is how large each individual cache may be. ageLimit is how old an item may be if it may be returned.
If an item is fetched that is older than the ageLimit, it will not be returned and the cache it resides in will be updated to expire all older items. If this is less than 0, no limit is applied.
func (*ConcurrentRingCache) EachSubCache ¶ added in v0.0.5
func (c *ConcurrentRingCache) EachSubCache(f func(*LIFOCache))
Lock each sub-cache and pass it to the handler function.
Each cache is locked as writable first.
func (*ConcurrentRingCache) EnforceSizeLimit ¶ added in v0.0.2
func (c *ConcurrentRingCache) EnforceSizeLimit()
Evict items from every sub-cache until they contain the ceiling of 1/N items where N is the size limit for this entire cache.
func (*ConcurrentRingCache) EvictOrderThan ¶ added in v0.0.2
func (c *ConcurrentRingCache) EvictOrderThan(tm int64)
func (*ConcurrentRingCache) Get ¶
func (c *ConcurrentRingCache) Get(key string) (interface{}, bool)
Get an item from the sub-cache that holds items for the given key.
If the key is not found in the sub-cache, (nil, false) is returned.
If the key is found in the sub-cache but it is expired, (item, false) is returned where the item is the the expired data. The sub-cache is also cleaned so that no item older than the AgeLimit remains.
If the key is found in the sub-cache and it is not expired, (item, true) is returned where the item is the user's data.
func (*ConcurrentRingCache) Put ¶
func (c *ConcurrentRingCache) Put(key string, item interface{})
Add an item and enforce the cache size limit.
func (*ConcurrentRingCache) PutWithHandler ¶
func (c *ConcurrentRingCache) PutWithHandler(key string, item interface{}, evictionHandler func(string, interface{}))
func (*ConcurrentRingCache) SetTimeFunction ¶ added in v0.0.9
func (c *ConcurrentRingCache) SetTimeFunction(timeFunction func() int64)
Set the time function that each cache in the ring of caches will use.
func (*ConcurrentRingCache) Size ¶
func (c *ConcurrentRingCache) Size() int
type LIFOCache ¶
type LIFOCache struct { // The heap of strings. Keys []string // The heap of satellite data. Items []interface{} // The heap of insertion values. AddedTime []int64 // The map of strings to their indexes in the arrays. Indexes map[string]int // When a string is removed, the eviction handler is called and given the // evicted string and associated data. // // This allows users of this class to have it drive eviction of other resources. // // The eviction handler is not called when a string is refreshed / re-added. EvictionHandlers []func(s string, data interface{}) // A function that returns the "time" an element is added. // // This is used to sort the elements for removal where the // smallest int64 value is considered first. // // These integers need not be time, but it is convenient to think of them // that way. TimeFunction func() int64 }
A heap that expires the first added string first.
This also allows for a string to be refreshed. That is, be put at the back of the line.
This cache implementation does not enforce a size limit. It only orders items for eviction. The user must evict them to reach a desired Len() (size).
func NewLIFOCache ¶
func NewLIFOCache() *LIFOCache
Construct a new LIFOCache that uses the system clock in seconds to order the strings added.
func (*LIFOCache) EvictNext ¶
Evict the next item, returning the key and value.
If the cache is empty "" and nil are returned.
func (*LIFOCache) EvictOlderThan ¶ added in v0.0.2
Evict items that are older than the given tm. That is the object's added time is less-than tm.
func (*LIFOCache) Get ¶
Get the user data and the time it was added. If the last boolean returned is false means that the key was not found in the cache.
if item, addTime, ok := lifoCache.Get("key"); ok { ... }
func (*LIFOCache) MinItem ¶
func (c *LIFOCache) MinItem() interface{}
Return next item to be returned by a call to EvictNext().
func (*LIFOCache) MinTime ¶
Return the time of the next key and item to be returned by a call to EvictNext().
func (*LIFOCache) Pop ¶
func (c *LIFOCache) Pop() interface{}
Return the last user-added object and call the Eviction function.
*Do not call this directly.* This is an internal API.
func (*LIFOCache) Push ¶
func (c *LIFOCache) Push(x interface{})
Push a string key into this cache.
*Do not call this directly.* This is an internal API.
func (*LIFOCache) PutWithHandler ¶
func (c *LIFOCache) PutWithHandler(key string, item interface{}, evictionhandler func(string, interface{})) (interface{}, bool)
Add a key to this cache with a given eviction function.
If the key already exists, the object is updated, the AddedTime is updated and a 2-tuple with the previous object and true is returned.
If the key does not already exist, the object is added under that key and (nil, false) is returned.
func (*LIFOCache) SetAddedTime ¶ added in v0.0.2
Set the added time of an item and re-heap it.