cache

package
v0.31.10 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cleanuper

type Cleanuper interface {
	Cleanup()
}

Cleanuper is an item that gets notified that it has been removed from memory, but not the cache.

type LruCache

type LruCache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

LruCache is a kind of LRU cache. Objects that are too old are removed, and if the cache is full, the oldest item(s) will be removed. When an item is set more than once, it is pushed to the end so its last to be removed. Limits are approximate, as garbage collecting will randomly happen. Also, in order to prevent memory thrashing, strict order is not preserved, but items will fall out more or less in LRU order. Someday we may offer a backing store version to extend the size of the cache to disk or some other kind of storage If the item has a "Removed" function, that function will be called when the item falls out of the cache. If the item has a "Cleanup" function, that function will be called when the item is removed from memory. If the cache has a backing store, it may be removed from memory, but still in the disk-based cache.

func NewLruCache

func NewLruCache(maxItemCount int, ttlSeconds int64) *LruCache

NewLruCache creates and returns a new cache. maxItemCount is the maximum number of items the cache can hold. ttl is the age in seconds past when items will be removed.

func (*LruCache) Get

func (o *LruCache) Get(key string) interface{}

Get returns the item based on its id, and updates its access time. If not found, it will return nil.

func (*LruCache) Has

func (o *LruCache) Has(key string) (exists bool)

Has tests for the existence of the key. It does not update the access time though.

func (*LruCache) Set

func (o *LruCache) Set(key string, v interface{})

Set puts the item into the cache, and updates its access time

func (*LruCache) SetGCInterval added in v0.19.0

func (o *LruCache) SetGCInterval(key string, v interface{})

type Remover

type Remover interface {
	Removed()
}

Remover is an item that gets notified that it has been removed from the cache.

Jump to

Keyboard shortcuts

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