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
16### Installation
17
18`go get github.com/patrickmn/go-cache`
19
20### Usage
21
22```go
23 import (
24 "fmt"
25 "github.com/patrickmn/go-cache"
26 "time"
27 )
28
29 func main() {
30
31 // Create a cache with a default expiration time of 5 minutes, and which
32 // purges expired items every 30 seconds
33 c := cache.New(5*time.Minute, 30*time.Second)
34
35 // Set the value of the key "foo" to "bar", with the default expiration time
36 c.Set("foo", "bar", cache.DefaultExpiration)
37
38 // Set the value of the key "baz" to 42, with no expiration time
39 // (the item won't be removed until it is re-set, or removed using
40 // c.Delete("baz")
41 c.Set("baz", 42, cache.NoExpiration)
42
43 // Get the string associated with the key "foo" from the cache
44 foo, found := c.Get("foo")
45 if found {
46 fmt.Println(foo)
47 }
48
49 // Since Go is statically typed, and cache values can be anything, type
50 // assertion is needed when values are being passed to functions that don't
51 // take arbitrary types, (i.e. interface{}). The simplest way to do this for
52 // values which will only be used once--e.g. for passing to another
53 // function--is:
54 foo, found := c.Get("foo")
55 if found {
56 MyFunction(foo.(string))
57 }
58
59 // This gets tedious if the value is used several times in the same function.
60 // You might do either of the following instead:
61 if x, found := c.Get("foo"); found {
62 foo := x.(string)
63 // ...
64 }
65 // or
66 var foo string
67 if x, found := c.Get("foo"); found {
68 foo = x.(string)
69 }
70 // ...
71 // foo can then be passed around freely as a string
72
73 // Want performance? Store pointers!
74 c.Set("foo", &MyStruct, cache.DefaultExpiration)
75 if x, found := c.Get("foo"); found {
76 foo := x.(*MyStruct)
77 // ...
78 }
79
80 // If you store a reference type like a pointer, slice, map or channel, you
81 // do not need to run Set if you modify the underlying data. The cached
82 // reference points to the same memory, so if you modify a struct whose
83 // pointer you've stored in the cache, retrieving that pointer with Get will
84 // point you to the same data:
85 foo := &MyStruct{Num: 1}
86 c.Set("foo", foo, cache.DefaultExpiration)
87 // ...
88 x, _ := c.Get("foo")
89 foo := x.(*MyStruct)
90 fmt.Println(foo.Num)
91 // ...
92 foo.Num++
93 // ...
94 x, _ := c.Get("foo")
95 foo := x.(*MyStruct)
96 foo.Println(foo.Num)
97
98 // will print:
99 // 1
100 // 2
101
102 }
103```
104
105### Reference
106
107`godoc` or [http://godoc.org/github.com/patrickmn/go-cache](http://godoc.org/github.com/patrickmn/go-cache)
108