Documentation ¶
Overview ¶
Package zcache is an in-memory key:value store/cache with time-based evictions.
It is suitable for applications running on a single machine. Its major advantage is that it's essentially a thread-safe map with expiration times. Any object can be stored, for a given duration or forever, and the cache can be safely used by multiple goroutines.
Although zcache isn't meant to be used as a persistent datastore, the contents can be saved to and loaded from a file (using `c.Items()` to retrieve the items map to serialize, and `NewFrom()` to create a cache from a deserialized one) to recover from downtime quickly.
Index ¶
- Constants
- type Cache
- func (c Cache) Add(k K, v V) error
- func (c Cache) AddWithExpire(k K, v V, d time.Duration) error
- func (c Cache) Delete(k K)
- func (c Cache) DeleteAll() map[K]Item[V]
- func (c Cache) DeleteExpired()
- func (c Cache) DeleteFunc(filter func(key K, item Item[V]) (del, stop bool)) map[K]Item[V]
- func (c Cache) Get(k K) (V, bool)
- func (c Cache) GetStale(k K) (v V, expired bool, ok bool)
- func (c Cache) GetWithExpire(k K) (V, time.Time, bool)
- func (c Cache) ItemCount() int
- func (c Cache) Items() map[K]Item[V]
- func (c Cache) Keys() []K
- func (c Cache) Modify(k K, f func(V) V) (V, bool)
- func (c Cache) OnEvicted(f func(K, V))
- func (c Cache) Pop(k K) (V, bool)
- func (c Cache) Rename(src, dst K) bool
- func (c Cache) Replace(k K, v V) error
- func (c Cache) ReplaceWithExpire(k K, v V, d time.Duration) error
- func (c Cache) Reset()
- func (c Cache) Set(k K, v V)
- func (c Cache) SetWithExpire(k K, v V, d time.Duration)
- func (c Cache) Touch(k K) (V, bool)
- func (c Cache) TouchWithExpire(k K, d time.Duration) (V, bool)
- type Item
- type Proxy
- func (p *Proxy[ProxyK, MainK, V]) Cache() *Cache[MainK, V]
- func (p *Proxy[ProxyK, MainK, V]) Delete(proxyKey ProxyK)
- func (p *Proxy[ProxyK, MainK, V]) Get(proxyKey ProxyK) (V, bool)
- func (p *Proxy[ProxyK, MainK, V]) Items() map[ProxyK]MainK
- func (p *Proxy[ProxyK, MainK, V]) Key(proxyKey ProxyK) (MainK, bool)
- func (p *Proxy[ProxyK, MainK, V]) Proxy(mainKey MainK, proxyKey ProxyK)
- func (p *Proxy[ProxyK, MainK, V]) Reset()
- func (p *Proxy[ProxyK, MainK, V]) Set(mainKey MainK, proxyKey ProxyK, v V)
Examples ¶
Constants ¶
const ( // NoExpiration indicates a cache item never expires. NoExpiration time.Duration = -1 // DefaultExpiration indicates to use the cache default expiration time. // Equivalent to passing in the same expiration duration as was given to // New() or NewFrom() when the cache was created (e.g. 5 minutes.) DefaultExpiration time.Duration = 0 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache is a thread-safe in-memory key/value store.
Example (Any) ¶
// Create a new cache that stores any value, behaving similar to zcache v1 // or go-cache. c := zcache.New[string, any](zcache.NoExpiration, zcache.NoExpiration) c.Set("a", "value 1") c.Set("b", 42) a, _ := c.Get("a") b, _ := c.Get("b") // This needs type assertions. p := func(a string, b int) { fmt.Println(a, b) } p(a.(string), b.(int))
Output: value 1 42
Example (Simple) ¶
// Create a cache with a default expiration time of 5 minutes, and which // purges expired items every 10 minutes. // // This creates a cache with string keys and values, with Go 1.18 type // parameters. c := zcache.New[string, string](5*time.Minute, 10*time.Minute) // Set the value of the key "foo" to "bar", with the default expiration. c.Set("foo", "bar") // Set the value of the key "baz" to "never", with no expiration time. The // item won't be removed until it's removed with c.Delete("baz"). c.SetWithExpire("baz", "never", zcache.NoExpiration) // Get the value associated with the key "foo" from the cache; due to the // use of type parameters this is a string, and no type assertions are // needed. foo, ok := c.Get("foo") if ok { fmt.Println(foo) }
Output: bar
Example (Struct) ¶
type MyStruct struct{ Value string } // Create a new cache that stores a specific struct. c := zcache.New[string, *MyStruct](zcache.NoExpiration, zcache.NoExpiration) c.Set("cache", &MyStruct{Value: "value"}) v, _ := c.Get("cache") fmt.Printf("%#v\n", v)
Output: &zcache_test.MyStruct{Value:"value"}
func New ¶
func New[K comparable, V any](defaultExpiration, cleanupInterval time.Duration) *Cache[K, V]
New creates a new cache with a given expiration duration and cleanup interval.
If the expiration duration is less than 1 (or NoExpiration) the items in the cache never expire (by default) and must be deleted manually.
If the cleanup interval is less than 1 expired items are not deleted from the cache before calling c.DeleteExpired().
func NewFrom ¶
func NewFrom[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, items map[K]Item[V]) *Cache[K, V]
NewFrom creates a new cache like New() and populates the cache with the given items.
The passed map will serve as the underlying map for the cache. This is useful for starting from a deserialized cache (serialized using e.g. gob.Encode() on c.Items()), or passing in e.g. make(map[string]Item, 500) to improve startup performance when the cache is expected to reach a certain minimum size.
The map is *not* copied and only the cache's methods synchronize access to this map, so it is not recommended to keep any references to the map around after creating a cache. If need be, the map can be accessed at a later point using c.Items() (which creates a copy of the map).
Note regarding serialization: When using e.g. gob, make sure to gob.Register() the individual types stored in the cache before encoding a map retrieved with c.Items() and to register those same types before decoding a blob containing an items map.
func (Cache) Add ¶
func (c Cache) Add(k K, v V) error
Add an item to the cache only if it doesn't exist yet or if it has expired.
It will return an error if the cache key already exists.
func (Cache) AddWithExpire ¶
AddWithExpire adds an item to the cache only if it doesn't exist yet, or if it has expired.
It will return an error if the cache key already exists. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.
func (Cache) Delete ¶
func (c Cache) Delete(k K)
Delete an item from the cache. Does nothing if the key is not in the cache.
func (Cache) DeleteAll ¶
func (c Cache) DeleteAll() map[K]Item[V]
DeleteAll deletes all items from the cache and returns them.
This calls OnEvicted for returned items.
func (Cache) DeleteExpired ¶
func (c Cache) DeleteExpired()
DeleteExpired deletes all expired items from the cache.
func (Cache) DeleteFunc ¶
func (c Cache) DeleteFunc(filter func(key K, item Item[V]) (del, stop bool)) map[K]Item[V]
DeleteFunc deletes and returns cache items matched by the filter function.
The item will be deleted if the callback's first return argument is true. The loop will stop if the second return argument is true.
OnEvicted is called for deleted items.
func (Cache) Get ¶
func (c Cache) Get(k K) (V, bool)
Get an item from the cache.
Returns the item or the zero value and a bool indicating whether the key is set.
func (Cache) GetStale ¶
GetStale gets an item from the cache without checking if it's expired.
Returns the item or the zero value and a bool indicating whether the key was expired and a bool indicating whether the key was set.
func (Cache) GetWithExpire ¶
GetWithExpire returns an item and its expiration time from the cache.
It returns the item or the zero value, the expiration time if one is set (if the item never expires a zero value for time.Time is returned), and a bool indicating whether the key was set.
func (Cache) ItemCount ¶
func (c Cache) ItemCount() int
ItemCount returns the number of items in the cache.
This may include items that have expired but have not yet been cleaned up.
func (Cache) Items ¶
func (c Cache) Items() map[K]Item[V]
Items returns a copy of all unexpired items in the cache.
func (Cache) Modify ¶
func (c Cache) Modify(k K, f func(V) V) (V, bool)
Modify the value of an existing key.
This is thread-safe; for example to increment a number:
cache.Modify("one", func(v int) int { return v + 1 })
Or setting a map key:
cache.Modify("key", func(v map[string]string) map[string]string { v["k"] = "v" return v })
This is thread-safe and can be safely run by multiple goroutines modifying the same key. If you would use Get() + Set() then two goroutines may Get() the same value and the modification of one of them will be lost.
This is not run for keys that are not set yet; the boolean return indicates if the key was set and if the function was applied.
func (Cache) OnEvicted ¶
func (c Cache) OnEvicted(f func(K, V))
OnEvicted sets an function to call when an item is evicted from the cache.
The function is run with the key and value. This is also run when a cache item is is deleted manually, but *not* when it is overwritten.
Can be set to nil to disable it (the default).
func (Cache) Pop ¶
func (c Cache) Pop(k K) (V, bool)
Pop gets an item from the cache and deletes it.
The bool return indicates if the item was set.
func (Cache) Rename ¶ added in v2.1.0
func (c Cache) Rename(src, dst K) bool
Rename a key; the value and expiry will be left untouched; onEvicted will not be called.
Existing keys will be overwritten; returns false is the src key doesn't exist.
func (Cache) Replace ¶
func (c Cache) Replace(k K, v V) error
Replace sets a new value for the key only if it already exists and isn't expired.
It will return an error if the cache key doesn't exist.
func (Cache) ReplaceWithExpire ¶
ReplaceWithExpire sets a new value for the key only if it already exists and isn't expired.
It will return an error if the cache key doesn't exist. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.
func (Cache) Reset ¶
func (c Cache) Reset()
Reset deletes all items from the cache without calling OnEvicted.
func (Cache) SetWithExpire ¶
SetWithExpire sets a cache item, replacing any existing item.
If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.
func (Cache) Touch ¶
func (c Cache) Touch(k K) (V, bool)
Touch replaces the expiry of a key with the default expiration and returns the current value, if any.
The boolean return value indicates if this item was set.
func (Cache) TouchWithExpire ¶
TouchWithExpire replaces the expiry of a key and returns the current value, if any.
The boolean return value indicates if this item was set. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.
type Proxy ¶
type Proxy[ProxyK, MainK comparable, V any] struct { // contains filtered or unexported fields }
Proxy a cache, allowing access to the same cache entries with different keys.
This is useful if you want to keep a cache which may be accessed by different keys in various different code paths. For example, a "site" may be accessed by ID or by CNAME. Proxy keys can have a different type than cache keys.
Proxy keys don't have an expiry and are never automatically deleted, the logic being that the same "proxy → key" mapping should always be valid. The items in the underlying cache can still be expired or deleted, and you can still manually call Delete() or Reset().
Example ¶
type Site struct { ID int Hostname string } site := &Site{ ID: 42, Hostname: "example.com", } // Create a new site which caches by site ID (int), and a "proxy" which // caches by the hostname (string). c := zcache.New[int, *Site](zcache.NoExpiration, zcache.NoExpiration) p := zcache.NewProxy[string, int, *Site](c) p.Set(42, "example.com", site) siteByID, ok := c.Get(42) fmt.Printf("%v %v\n", ok, siteByID) siteByHost, ok := p.Get("example.com") fmt.Printf("%v %v\n", ok, siteByHost) // They're both the same object/pointer. fmt.Printf("%v\n", siteByID == siteByHost)
Output: true &{42 example.com} true &{42 example.com} true
func NewProxy ¶
func NewProxy[ProxyK, MainK comparable, V any](c *Cache[MainK, V]) *Proxy[ProxyK, MainK, V]
NewProxy creates a new proxied cache.
func (*Proxy[ProxyK, MainK, V]) Delete ¶
func (p *Proxy[ProxyK, MainK, V]) Delete(proxyKey ProxyK)
Delete stops proxying "proxyKey" to "mainKey".
This only removes the proxy link, not the entry from the main cache.
func (*Proxy[ProxyK, MainK, V]) Items ¶
func (p *Proxy[ProxyK, MainK, V]) Items() map[ProxyK]MainK
Items gets all items in this proxy, as proxyKey → mainKey
func (*Proxy[ProxyK, MainK, V]) Key ¶
Key gets the main key for this proxied entry, if it exist.
The boolean value indicates if this proxy key is set.
func (*Proxy[ProxyK, MainK, V]) Proxy ¶
func (p *Proxy[ProxyK, MainK, V]) Proxy(mainKey MainK, proxyKey ProxyK)
Proxy items from "proxyKey" to "mainKey".