• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D27-Sep-202115

CONTRIBUTORSH A D27-Sep-2021474

LICENSEH A D27-Sep-20211.1 KiB

README.mdH A D27-Sep-20213.1 KiB

cache.goH A D27-Sep-202138.3 KiB

cache_test.goH A D27-Sep-202141.4 KiB

go.modH A D27-Sep-202144

sharded.goH A D27-Sep-20214.5 KiB

sharded_test.goH A D27-Sep-20211.6 KiB

README.md

1# go-cache
2
3go-cache is an in-memory key:value store/cache similar to memcached that is
4suitable for applications running on a single machine. Its major advantage is
5that, being essentially a thread-safe `map[string]interface{}` with expiration
6times, it doesn't need to serialize or transmit its contents over the network.
7
8Any object can be stored, for a given duration or forever, and the cache can be
9safely used by multiple goroutines.
10
11Although go-cache isn't meant to be used as a persistent datastore, the entire
12cache can be saved to and loaded from a file (using `c.Items()` to retrieve the
13items map to serialize, and `NewFrom()` to create a cache from a deserialized
14one) to recover from downtime quickly. (See the docs for `NewFrom()` for caveats.)
15
16When creating a cache object using `NewWithLRU()`, if you set the maxItems value
17above 0, the LRU functionality is enabled. The cache automatically updates a
18timestamp every time a given item is retrieved. In the background, the janitor takes
19care of deleting items that have expired because of staleness, or are
20least-recently-used when the cache is under pressure. Whatever you set your purge
21interval to controls when the item will actually be removed from the cache. If you
22don't want to use the janitor, and wish to manually purge LRU items, then
23`c.DeleteLRU(n)` where `n` is the number of items you'd like to purge.
24
25### Installation
26
27`go get github.com/0xERR0R/go-cache`
28
29### Usage
30
31```go
32import (
33	"fmt"
34	"github.com/0xERR0R/go-cache"
35	"time"
36)
37
38func main() {
39	// Create a cache with a default expiration time of 5 minutes, and which
40	// purges expired items every 10 minutes
41	c := cache.New(5*time.Minute, 10*time.Minute)
42
43	// Set the value of the key "foo" to "bar", with the default expiration time
44	c.Set("foo", "bar", cache.DefaultExpiration)
45
46	// Set the value of the key "baz" to 42, with no expiration time
47	// (the item won't be removed until it is re-set, or removed using
48	// c.Delete("baz")
49	c.Set("baz", 42, cache.NoExpiration)
50
51	// Get the string associated with the key "foo" from the cache
52	foo, found := c.Get("foo")
53	if found {
54		fmt.Println(foo)
55	}
56
57	// Since Go is statically typed, and cache values can be anything, type
58	// assertion is needed when values are being passed to functions that don't
59	// take arbitrary types, (i.e. interface{}). The simplest way to do this for
60	// values which will only be used once--e.g. for passing to another
61	// function--is:
62	foo, found := c.Get("foo")
63	if found {
64		MyFunction(foo.(string))
65	}
66
67	// This gets tedious if the value is used several times in the same function.
68	// You might do either of the following instead:
69	if x, found := c.Get("foo"); found {
70		foo := x.(string)
71		// ...
72	}
73	// or
74	var foo string
75	if x, found := c.Get("foo"); found {
76		foo = x.(string)
77	}
78	// ...
79	// foo can then be passed around freely as a string
80
81	// Want performance? Store pointers!
82	c.Set("foo", &MyStruct, cache.DefaultExpiration)
83	if x, found := c.Get("foo"); found {
84		foo := x.(*MyStruct)
85			// ...
86	}
87}
88```
89
90### Reference
91
92`godoc` or [http://godoc.org/github.com/0xERR0R/go-cache](http://godoc.org/github.com/0xERR0R/go-cache)
93