cache

package module
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2022 License: MIT Imports: 8 Imported by: 0

README

go-cache

test-and-lint codecov Go Report Card Go Reference

ℹ️ go-cache is a port of great go-cache library that was written by @patrickmn. The main defference is that it relies on generics instead of reflection.

go-cache is a in-memory key:value store/cache similar to memcached that is suitable for applications running on a single machine. Its major advantage is that, being essentially a thread-safe map[string][T] with expiration times, it doesn't need to serialize or transmit its contents over the network.

Any object can be stored, for a given duration or forever, and the cache can be safely used by multiple goroutines.

Although go-cache isn't meant to be used as a persistent datastore, the entire cache 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. (See the docs for NewFrom() for caveats.)

Installation

go get github.com/num30/go-cache

Usage
import (
	"fmt"
	"github.com/num30/go-cache"
	"time"
)

func main() {
	// Create a cache with a default expiration time of 5 minutes, and which
	// purges expired items every 10 minutes
	c := cache.New[string](5*time.Minute, 10*time.Minute)

	// Set the value of the key "foo" to "bar", with the default expiration time
	c.Set("foo", "bar", cache.DefaultExpiration)

	// Set the value of the key "baz" to "lightning", with no expiration time
	// (the item won't be removed until it is re-set, or removed using
	// c.Delete("baz")
	c.Set("baz", "lightning", cache.NoExpiration)

	// Get the string associated with the key "foo" from the cache
	foo, found := c.Get("foo")
	if found {
		fmt.Println(foo)
	}

	// Want performance? Store pointers!
	structCache := cache.New[MyStruct](5*time.Minute, 10*time.Minute)
	structCache.Set("foo", &MyStruct, cache.DefaultExpiration)
	if x, found := c.Get("foo"); found {
		fmt.Println(")
	}
}

Run this example with go run examples/main.go.

Performance Comparison

Comparison of performance with original go-cache implementation.

Spoiler alert! The difference is insignificant.

Test Non generic This version
BenchmarkCacheGetStringExpiring-8 29884011 41.45 ns/op 28318041 43.31 ns/op
BenchmarkCacheGetStringNotExpiring-8 91891774 14.20 ns/op 72259294 14.02 ns/op
BenchmarkCacheGetConcurrentExpiring-8 26299849 42.58 ns/op 30129078 39.53 ns/op
BenchmarkCacheGetConcurrentNotExpiring-8 28991383 41.25 ns/op 30760544 38.09 ns/op
BenchmarkCacheGetManyConcurrentExpiring-8 55589712 44.90 ns/op 56991110 38.86 ns/op
BenchmarkCacheGetManyConcurrentNotExpiring-8 30105078 43.98 ns/op 46270045 41.54 ns/op
BenchmarkCacheSetStringExpiring-8 18392893 63.41 ns/op 17788724 61.42 ns/op
BenchmarkCacheSetStringNotExpiring-8 46400654 28.45 ns/op 40226074 27.41 ns/op
BenchmarkCacheSetDelete-8 18703620 60.75 ns/op 18270448 59.90 ns/op
BenchmarkCacheSetDeleteSingleLock-8 32633755 39.34 ns/op 32415156 36.96 ns/op
BenchmarkCacheGetStructExpiring-8 30487856 41.60 ns/op 26925226 40.55 ns/op
BenchmarkCacheGetStructNotExpiring-8 91921044 13.94 ns/op 96379750 13.08 ns/op
BenchmarkCacheSetStructExpiring-8 13977464 86.44 ns/op 13364509 87.69 ns/op
BenchmarkCacheSetStructNotExpiring-8 22749384 54.14 ns/op 23207397 52.58 ns/op
BenchmarkCacheSetFatStructExpiring-8 11718718 103.3 ns/op 12051895 102.3 ns/op
BenchmarkCacheGetFatStructNotExpiring-8 88695709 13.92 ns/op 83220014 13.76 ns/op

Documentation

Index

Constants

View Source
const (
	// For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
	// For use with functions that take an 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[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](defaultExpiration, cleanupInterval time.Duration) *Cache[T]

Return 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[T any](defaultExpiration, cleanupInterval time.Duration, items map[string]Item[T]) *Cache[T]

Return 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().

NewFrom() also accepts an items map which 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.

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, x T, d time.Duration) error

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.

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

func (c Cache) DeleteExpired()

Delete all expired items from the cache.

func (Cache) Flush

func (c Cache) Flush()

Delete all items from the cache.

func (Cache) Get

func (c Cache) Get(k string) (T, bool)

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Cache) GetWithExpiration

func (c Cache) GetWithExpiration(k string) (T, 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) ItemCount

func (c Cache) ItemCount() int

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[T]

Copies all unexpired items in the cache into a new map and returns it.

func (Cache) Load

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

Add (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

Load and add 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) OnEvicted

func (c Cache) OnEvicted(f func(string, T))

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

func (c Cache) Replace(k string, x T, d time.Duration) error

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

func (Cache) Save

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

Write 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

Save 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, x T, d time.Duration)

Add an item to the cache, 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, x T)

Add an item to the cache, replacing any existing item, using the default expiration.

type Item

type Item[T any] struct {
	Object     T
	Expiration int64
}

func (Item[T]) Expired

func (item Item[T]) Expired() bool

Returns true if the item has expired.

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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