zcache

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: MIT Imports: 7 Imported by: 11

README

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.

The canonical import path is zgo.at/zcache, and reference docs are at https://godocs.io/zgo.at/zcache

This is a fork of https://github.com/patrickmn/go-cache – which no longer seems actively maintained. There are two versions of zcache:

  • v1 is intended to be 100% compatible with co-cache and a drop-in replacement with various enhancements.
  • v2 makes various incompatible changes to the API: various functions calls are improved. This uses generics and requires Go 1.18.

This README documents v2; see README.v1.md for the v1 README. Both versions are maintained. See the "changes" section below for a list of changes.

Usage

Some examples from example_test.go:

func ExampleSimple() {
	// 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
}

func ExampleStruct() {
	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 ExampleAny() {
	// 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
}

func ExampleProxy() {
	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
}

Changes

Incompatible changes in v2

  • Use type parameters instead of map[string]interface{}; you can get the same as before with zcache.New[string, any](..), but if you know you will only store MyStruct you can use zcache.New[string, *MyStruct](..) for additional type safety.

  • Remove Save(), SaveFile(), Load(), LoadFile(); you can still persist stuff to disk by using Items() and NewFrom(). These methods were already deprecated.

  • Rename Set() to SetWithExpire(), and rename SetDefault() to Set(). Most of the time you want to use the default expiry time, so make that the easier path.

  • The Increment* and Decrement* functions have been removed; you can replace them with Modify():

    cache := New[string, int](DefaultExpiration, 0)
    cache.Set("one", 1)
    cache.Modify("one", func(v int) int { return v + 1 })
    

    The performance of this is roughly the same as the old Increment, and this is a more generic method that can also be used for other things like appending to a slice.

  • Rename Flush() to Reset(); I think that more clearly conveys what it's intended for as Flush() is typically used to flush a buffer or the like.

Compatible changes from go-cache

All these changes are in both v1 and v2:

  • Add Keys() to list all keys.
  • Add Touch() to update the expiry on an item.
  • Add GetStale() to get items even after they've expired.
  • Add Pop() to get an item and delete it.
  • Add Modify() to atomically modify existing cache entries (e.g. lists, maps).
  • Add DeleteAll() to remove all items from the cache with onEvicted call.
  • Add DeleteFunc() to remove specific items from the cache atomically.
  • Add Rename() to rename keys, retaining the value and expiry.
  • Add Proxy type, to access cache items under a different key.
  • Various small internal and documentation improvements.

See issue-list.markdown for a complete run-down of the PRs/issues for go-cache and what was and wasn't included.

FAQ

How can I limit the size of the cache? Is there an option for this?

Not really; zcache is intended as a thread-safe map with time-based eviction. This keeps it nice and simple. Adding something like a LRU eviction mechanism not only makes the code more complex, it also makes the library worse for cases where you just want a map since it requires additional memory and makes some operations more expensive (unless a new API is added which make the API worse for those use cases).

So unless I or someone else comes up with a way to do this which doesn't detract anything from the simple map use case, I'd rather not add it. Perhaps wrapping zcache.Cache and overriding some methods could work, but I haven't looked at it.

tl;dr: this isn't designed to solve every caching use case. That's a feature.

Documentation

Index

Constants

View Source
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 struct {
	// contains filtered or unexported fields
}

func New

func New(defaultExpiration, cleanupInterval time.Duration) *Cache

New creates a new cache with a given default expiration duration and cleanup interval.

If the expiration duration is less than one (or NoExpiration), the items in the cache never expire (by default), and must be deleted manually.

If the cleanup interval is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().

func NewFrom

func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]Item) *Cache

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() (subject to the same caveat.)

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 string, v interface{}, d time.Duration) 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 exists.

func (Cache) Decrement

func (c Cache) Decrement(k string, n int64) error

Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to decrement it by n. To retrieve the decremented value, use one of the specialized methods, e.g. DecrementInt64.

func (Cache) DecrementFloat

func (c Cache) DecrementFloat(k string, n float64) error

Decrement an item of type float32 or float64 by n. Returns an error if the item's value is not floating point, if it was not found, or if it is not possible to decrement it by n. To retrieve the decremented value, use one of the specialized methods, e.g. DecrementFloat64.

func (Cache) DecrementFloat32

func (c Cache) DecrementFloat32(k string, n float32) (float32, error)

Decrement an item of type float32 by n. Returns an error if the item's value is not an float32, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementFloat64

func (c Cache) DecrementFloat64(k string, n float64) (float64, error)

Decrement an item of type float64 by n. Returns an error if the item's value is not an float64, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementInt

func (c Cache) DecrementInt(k string, n int) (int, error)

Decrement an item of type int by n. Returns an error if the item's value is not an int, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementInt16

func (c Cache) DecrementInt16(k string, n int16) (int16, error)

Decrement an item of type int16 by n. Returns an error if the item's value is not an int16, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementInt32

func (c Cache) DecrementInt32(k string, n int32) (int32, error)

Decrement an item of type int32 by n. Returns an error if the item's value is not an int32, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementInt64

func (c Cache) DecrementInt64(k string, n int64) (int64, error)

Decrement an item of type int64 by n. Returns an error if the item's value is not an int64, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementInt8

func (c Cache) DecrementInt8(k string, n int8) (int8, error)

Decrement an item of type int8 by n. Returns an error if the item's value is not an int8, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementUint

func (c Cache) DecrementUint(k string, n uint) (uint, error)

Decrement an item of type uint by n. Returns an error if the item's value is not an uint, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementUint16

func (c Cache) DecrementUint16(k string, n uint16) (uint16, error)

Decrement an item of type uint16 by n. Returns an error if the item's value is not an uint16, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementUint32

func (c Cache) DecrementUint32(k string, n uint32) (uint32, error)

Decrement an item of type uint32 by n. Returns an error if the item's value is not an uint32, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementUint64

func (c Cache) DecrementUint64(k string, n uint64) (uint64, error)

Decrement an item of type uint64 by n. Returns an error if the item's value is not an uint64, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementUint8

func (c Cache) DecrementUint8(k string, n uint8) (uint8, error)

Decrement an item of type uint8 by n. Returns an error if the item's value is not an uint8, or if it was not found. If there is no error, the new value is returned.

func (Cache) DecrementUintptr

func (c Cache) DecrementUintptr(k string, n uintptr) (uintptr, error)

Decrement an item of type uintptr by n. Returns an error if the item's value is not an uintptr, or if it was not found. If there is no error, the new value is returned.

func (Cache) Delete

func (c Cache) Delete(k string)

Delete an item from the cache. Does nothing if the key is not in the cache.

func (Cache) DeleteAll

func (c Cache) DeleteAll() map[string]Item

DeleteAll deletes all items from the cache and returns them.

Note that onEvicted is called on returned items.

func (Cache) DeleteExpired

func (c Cache) DeleteExpired()

DeleteExpired deletes all expired items from the cache.

func (Cache) DeleteFunc

func (c Cache) DeleteFunc(fn Filter) map[string]Item

DeleteFunc deletes and returns filtered items from the cache.

The item will be deleted if the callback's first return argument is true. All deleted items are passed to onEvict and are returned.

The loop will stop if the second return argument is true.

func (Cache) Flush

func (c Cache) Flush()

Flush deletes all items from the cache without calling onEvicted.

This is a way to reset the cache to its original state.

func (Cache) Get

func (c Cache) Get(k string) (interface{}, bool)

Get an item from the cache.

Returns the item or nil and a bool indicating whether the key is set.

func (Cache) GetStale

func (c Cache) GetStale(k string) (v interface{}, expired bool, ok bool)

GetStale gets an item from the cache without checking if it's expired.

Returns the item or nil, a bool indicating that the item is expired, and a bool indicating whether the key was found.

func (Cache) GetWithExpiration

func (c Cache) GetWithExpiration(k string) (interface{}, time.Time, bool)

GetWithExpiration returns an item and its expiration time from the cache. It returns the item or nil, 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 found.

func (Cache) Increment

func (c Cache) Increment(k string, n int64) error

Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to increment it by n. To retrieve the incremented value, use one of the specialized methods, e.g. IncrementInt64.

func (Cache) IncrementFloat

func (c Cache) IncrementFloat(k string, n float64) error

Increment an item of type float32 or float64 by n. Returns an error if the item's value is not floating point, if it was not found, or if it is not possible to increment it by n. To retrieve the incremented value, use one of the specialized methods, e.g. IncrementFloat64.

func (Cache) IncrementFloat32

func (c Cache) IncrementFloat32(k string, n float32) (float32, error)

Increment an item of type float32 by n. Returns an error if the item's value is not an float32, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementFloat64

func (c Cache) IncrementFloat64(k string, n float64) (float64, error)

Increment an item of type float64 by n. Returns an error if the item's value is not an float64, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementInt

func (c Cache) IncrementInt(k string, n int) (int, error)

Increment an item of type int by n. Returns an error if the item's value is not an int, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementInt16

func (c Cache) IncrementInt16(k string, n int16) (int16, error)

Increment an item of type int16 by n. Returns an error if the item's value is not an int16, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementInt32

func (c Cache) IncrementInt32(k string, n int32) (int32, error)

Increment an item of type int32 by n. Returns an error if the item's value is not an int32, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementInt64

func (c Cache) IncrementInt64(k string, n int64) (int64, error)

Increment an item of type int64 by n. Returns an error if the item's value is not an int64, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementInt8

func (c Cache) IncrementInt8(k string, n int8) (int8, error)

Increment an item of type int8 by n. Returns an error if the item's value is not an int8, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementUint

func (c Cache) IncrementUint(k string, n uint) (uint, error)

Increment an item of type uint by n. Returns an error if the item's value is not an uint, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementUint16

func (c Cache) IncrementUint16(k string, n uint16) (uint16, error)

Increment an item of type uint16 by n. Returns an error if the item's value is not an uint16, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementUint32

func (c Cache) IncrementUint32(k string, n uint32) (uint32, error)

Increment an item of type uint32 by n. Returns an error if the item's value is not an uint32, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementUint64

func (c Cache) IncrementUint64(k string, n uint64) (uint64, error)

Increment an item of type uint64 by n. Returns an error if the item's value is not an uint64, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementUint8

func (c Cache) IncrementUint8(k string, n uint8) (uint8, error)

Increment an item of type uint8 by n. Returns an error if the item's value is not an uint8, or if it was not found. If there is no error, the new value is returned.

func (Cache) IncrementUintptr

func (c Cache) IncrementUintptr(k string, n uintptr) (uintptr, error)

Increment an item of type uintptr by n. Returns an error if the item's value is not an uintptr, or if it was not found. If there is no error, the new value is returned.

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[string]Item

Items returns a copy of all unexpired items in the cache.

func (Cache) Keys

func (c Cache) Keys() []string

Keys gets a list of all keys, in no particular order.

func (Cache) Load

func (c Cache) Load(r io.Reader) error

Load (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) LoadFile

func (c Cache) LoadFile(fname string) error

LoadFile reads cache items from the given filename, excluding any items with keys that already exist in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) Modify

func (c Cache) Modify(k string, f func(interface{}) interface{}) bool

Modify the value of an existing key; this can be used for appending to a list or setting map keys:

zcache.Modify("key", func(v interface{}) interface{} {
      vv = v.(map[string]string)
      vv["k"] = "v"
      return vv
})

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(string, interface{}))

OnEvicted sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.

func (Cache) Pop

func (c Cache) Pop(k string) (interface{}, bool)

Pop gets an item from the cache and deletes it.

func (Cache) Rename added in v1.2.0

func (c Cache) Rename(src, dst string) 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 string, v interface{}, d time.Duration) 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) Save

func (c Cache) Save(w io.Writer) (err error)

Save the cache's items (using Gob) to an io.Writer.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) SaveFile

func (c Cache) SaveFile(fname string) error

SaveFile writes the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) Set

func (c Cache) Set(k string, v interface{}, d time.Duration)

Set 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) SetDefault

func (c Cache) SetDefault(k string, v interface{})

SetDefault calls Set() with the default expiration for this cache.

func (Cache) Touch

func (c Cache) Touch(k string, d time.Duration) (interface{}, bool)

Touch replaces the expiry of a key and returns the current value, if any.

type Filter

type Filter func(key string, item Item) (del bool, stop bool)

Filter is the function definition of the DeleteFunc method parameter.

type Item

type Item struct {
	Object     interface{}
	Expiration int64
}

Item stored in the cache; it holds the value and the expiration time as timestamp.

func (Item) Expired

func (item Item) Expired() bool

Expired reports if this item has expired.

type Proxy added in v1.1.0

type Proxy 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.

Proxies cache entries 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 Flush().

func NewProxy added in v1.1.0

func NewProxy(c *Cache) *Proxy

NewProxy creates a new proxied cache.

func (*Proxy) Cache added in v1.1.0

func (p *Proxy) Cache() *Cache

Cache gets the associated cache.

func (*Proxy) Delete added in v1.1.0

func (p *Proxy) Delete(proxyKey string)

Delete stops proxying "proxyKey" to "mainKey".

This only removes the proxy link, not the entry from the main cache.

func (*Proxy) Flush added in v1.1.0

func (p *Proxy) Flush()

Flush removes all proxied keys.

func (*Proxy) Get added in v1.1.0

func (p *Proxy) Get(proxyKey string) (interface{}, bool)

Get a proxied cache item with zcache.Cache.Get()

func (*Proxy) Items added in v1.1.0

func (p *Proxy) Items() map[string]string

Items gets all items in this proxy, as proxyKey → mainKey

func (*Proxy) Key added in v1.1.0

func (p *Proxy) Key(proxyKey string) (string, bool)

Key gets the main key for this proxied entry, if it exist.

func (*Proxy) Proxy added in v1.1.0

func (p *Proxy) Proxy(mainKey, proxyKey string)

Proxy items from "proxyKey" to "mainKey".

func (*Proxy) Set added in v1.1.0

func (p *Proxy) Set(mainKey, proxyKey string, v interface{})

Set a new item in the main cache with the key mainKey, and proxy to that with proxyKey.

This behaves like zcache.Cache.SetDefault() otherwise.

Jump to

Keyboard shortcuts

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