cache

package
v0.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheStats added in v0.0.11

type CacheStats struct {
	// contains filtered or unexported fields
}

func (*CacheStats) Evict added in v0.0.11

func (c *CacheStats) Evict()

func (*CacheStats) GetStats added in v0.0.11

func (c *CacheStats) GetStats() (int32, int32, int32)

Return (hit, miss, evict) counts.

func (*CacheStats) Hit added in v0.0.11

func (c *CacheStats) Hit()

func (*CacheStats) Miss added in v0.0.11

func (c *CacheStats) Miss()

func (*CacheStats) Reset added in v0.0.11

func (c *CacheStats) Reset()

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) DisableStats added in v0.0.11

func (c *ConcurrentRingCache) DisableStats()

Unset all the cache objects.

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) EnableStats added in v0.0.11

func (c *ConcurrentRingCache) EnableStats()

Set all the cache objects.

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) GetStats added in v0.0.11

func (c *ConcurrentRingCache) GetStats() []*CacheStats

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) Remove added in v0.0.11

func (c *ConcurrentRingCache) Remove(key string) (interface{}, bool)

Atomically remove an item from the cache.

func (*ConcurrentRingCache) ResetStats added in v0.0.11

func (c *ConcurrentRingCache) ResetStats()

Set the stat objects with a new empty value.

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

	// If set to non-nil, cache stats will be collected.
	// Stats are not collected by default.
	Stats *CacheStats
}

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

func (c *LIFOCache) EvictNext() (string, interface{})

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

func (c *LIFOCache) EvictOlderThan(tm int64)

Evict items that are older than the given tm. That is the object's added time is less-than tm.

func (*LIFOCache) Get

func (c *LIFOCache) Get(key string) (interface{}, int64, bool)

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) Len

func (c *LIFOCache) Len() int

func (*LIFOCache) Less

func (c *LIFOCache) Less(i, j int) bool

func (*LIFOCache) MinItem

func (c *LIFOCache) MinItem() interface{}

Return next item to be returned by a call to EvictNext().

func (*LIFOCache) MinKey

func (c *LIFOCache) MinKey() string

Return the next key to be returned by a call to EvictNext().

func (*LIFOCache) MinTime

func (c *LIFOCache) MinTime() int64

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. It must be public so that the Heap interface is implemented.

func (*LIFOCache) Put

func (c *LIFOCache) Put(key string, item interface{}) (interface{}, bool)

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) Remove

func (c *LIFOCache) Remove(key string) (interface{}, bool)

Remove the given key from the cache.

func (*LIFOCache) SetAddedTime added in v0.0.2

func (c *LIFOCache) SetAddedTime(key string, tm int64)

Set the added time of an item and re-heap it.

func (*LIFOCache) Swap

func (c *LIFOCache) Swap(i, j int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL