top
(README.md)
# go-cache

go-cache is an 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]interface{}` 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/patrickmn/go-cache`

### Usage

```go
	import (
		"fmt"
		"github.com/patrickmn/go-cache"
		"time"
	)

	func main() {

		// Create a cache with a default expiration time of 5 minutes, and which
		// purges expired items every 30 seconds
		c := cache.New(5*time.Minute, 30*time.Second)

		// 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 42, with no expiration time
		// (the item won't be removed until it is re-set, or removed using
		// c.Delete("baz")
		c.Set("baz", 42, cache.NoExpiration)

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

		// Since Go is statically typed, and cache values can be anything, type
		// assertion is needed when values are being passed to functions that don't
		// take arbitrary types, (i.e. interface{}). The simplest way to do this for
		// values which will only be used once--e.g. for passing to another
		// function--is:
		foo, found := c.Get("foo")
		if found {
			MyFunction(foo.(string))
		}

		// This gets tedious if the value is used several times in the same function.
		// You might do either of the following instead:
		if x, found := c.Get("foo"); found {
			foo := x.(string)
			// ...
		}
		// or
		var foo string
		if x, found := c.Get("foo"); found {
			foo = x.(string)
		}
		// ...
		// foo can then be passed around freely as a string

		// Want performance? Store pointers!
		c.Set("foo", &MyStruct, cache.DefaultExpiration)
		if x, found := c.Get("foo"); found {
			foo := x.(*MyStruct)
			// ...
		}

		// If you store a reference type like a pointer, slice, map or channel, you
		// do not need to run Set if you modify the underlying data. The cached
		// reference points to the same memory, so if you modify a struct whose
		// pointer you've stored in the cache, retrieving that pointer with Get will
		// point you to the same data:
		foo := &MyStruct{Num: 1}
		c.Set("foo", foo, cache.DefaultExpiration)
		// ...
		x, _ := c.Get("foo")
		foo := x.(*MyStruct)
		fmt.Println(foo.Num)
		// ...
		foo.Num++
		// ...
		x, _ := c.Get("foo")
		foo := x.(*MyStruct)
		foo.Println(foo.Num)

		// will print:
		// 1
		// 2

	}
```

### Reference

`godoc` or [http://godoc.org/github.com/patrickmn/go-cache](http://godoc.org/github.com/patrickmn/go-cache)

Imported by 136 package(s) ΒΆ

  1. github.com/12foo/casablanca
  2. github.com/Arukim/limitscounter
  3. github.com/BrianIp/orchestrator/go/inst
  4. github.com/BrianIp/orchestrator/go/logic
  5. github.com/Cepave/open-falcon-backend/modules/query/cache
  6. github.com/Cepave/open-falcon-backend/modules/query/nqm
  7. github.com/Cepave/query/cache
  8. github.com/Cepave/query/nqm
  9. github.com/FauxFaux/dingo
  10. github.com/Hevienz/go-rediscache
  11. github.com/Hevienz/go-redisclustercache
  12. github.com/MetalMatze/Krautreporter-API/repository
  13. github.com/PuppyKhan/xenia/pkg/mask
  14. github.com/PuppyKhan/xenia/pkg/query
  15. github.com/PuppyKhan/xenia/pkg/regex
  16. github.com/RiftBit/ALS-Go
  17. github.com/RiftBit/xvFavSync
  18. github.com/TwitchScience/spade
  19. github.com/TwitchScience/spade/deglobber
  20. github.com/WindomZ/go-develop-kit/cache/gocache
  21. github.com/aelsabbahy/goss
  22. github.com/aksentyev/hubble/exportertools
  23. github.com/alexedwards/scs/engine/memstore
  24. github.com/artemnikitin/here-tele-bot/im
  25. github.com/arukim/limitscounter
  26. github.com/asticode/go-cache-manager/cachemanager
  27. github.com/beppeben/m3m3/web
  28. github.com/blackss2/utility/hotserver
  29. github.com/cafebazaar/bahram/datasource
  30. github.com/cgclever/go-client/redis
  31. github.com/chyeh/open-falcon-backend/modules/query/cache
  32. github.com/chyeh/open-falcon-backend/modules/query/nqm
  33. github.com/chyeh/query/cache
  34. github.com/chyeh/query/nqm
  35. github.com/cjellick/host-api/healthcheck
  36. github.com/cocotyty/httpclient/example
  37. github.com/codingsince1985/geo-golang/cached
  38. github.com/coralproject/shelf/internal/xenia/mask
  39. github.com/coralproject/shelf/internal/xenia/query
  40. github.com/coralproject/shelf/internal/xenia/regex
  41. github.com/coralproject/shelf/internal/xenia/script
  42. github.com/coreos/jwtproxy/jwt/noncestorage/local
  43. github.com/crackcomm/googlebot
  44. github.com/crackcomm/renderer/storage
  45. github.com/crosserclaws/open-falcon-backend/modules/query/cache
  46. github.com/crosserclaws/open-falcon-backend/modules/query/nqm
  47. github.com/denderello/jwtproxy/jwt/noncestorage/local
  48. github.com/dionyself/golang-cms/utils
  49. github.com/ednapiranha/leaves/authenticate
  50. github.com/empirefox/esecend/captchar
  51. github.com/empirefox/esecend/sec
  52. github.com/empirefox/esecend/sms
  53. github.com/errordeveloper/coredns/middleware/cache
  54. github.com/errordeveloper/coredns/middleware/dnssec
  55. github.com/faxal/idp/core
  56. github.com/fluxrad/puckfinder/api
  57. github.com/gabrielrussell/mongo-tools/mongoreplay
  58. github.com/google/skia-buildbot/golden/go/trybot
  59. github.com/grunmax/GinRedisApi/accs
  60. github.com/grunmax/GinRedisApi/ctrl
  61. github.com/grunmax/GinRedisApi/midd
  62. github.com/grunmax/ginredisapi
  63. github.com/grunmax/ginredisapi/accs
  64. github.com/grunmax/ginredisapi/ctrl
  65. github.com/grunmax/ginredisapi/midd
  66. github.com/helderfarias/gwn-proxy/gateway/middleware/limit
  67. github.com/hhsnopek/10kft
  68. github.com/hien/dingo
  69. github.com/hverr/go-tcpip/ipv4
  70. github.com/ibuildthecloud/cli/monitor
  71. github.com/ipfans/echoscs/engine/memstore
  72. github.com/janekolszak/idp
  73. github.com/johnt337/goss
  74. github.com/joliva-ob/onebox-dynamic-pricing/dataservice
  75. github.com/karasz/coredns/middleware/cache
  76. github.com/karasz/coredns/middleware/dnssec
  77. github.com/karteek/wdns
  78. github.com/ksred/go-monitor
  79. github.com/larixsource/geo-golang/cached
  80. github.com/launchdarkly/go-client/redis
  81. github.com/leelynne/coredns/middleware/cache
  82. github.com/leelynne/coredns/middleware/dnssec
  83. github.com/liderman/text-generator
  84. github.com/lucasem/servemd
  85. github.com/lukedmor/servemd
  86. github.com/metalmatze/krautreporter-api/repository
  87. github.com/miekg/coredns/middleware/dnssec
  88. github.com/miekg/dingo
  89. github.com/mongodb/mongo-tools/mongoreplay
  90. github.com/mopsalarm/go-pr0gramm-categories
  91. github.com/moul/as-a-service
  92. github.com/nikelmwann/dokku-api-server/dokku
  93. github.com/nmonterroso/lolchest.win/riotapi
  94. github.com/otaviof/godutch
  95. github.com/outbrain/orchestrator/go/inst
  96. github.com/outbrain/orchestrator/go/logic
  97. github.com/pavlik/mimozaflowers
  98. github.com/pengfei-xue/host-api/healthcheck
  99. github.com/pforemski/dingo
  100. github.com/pjh130/go/common/storage/cache
  101. github.com/rancher/agent/utilities/utils
  102. github.com/rancher/cli/monitor
  103. github.com/rancher/host-api/healthcheck
  104. github.com/rancher/kubernetes-agent/hostlabels
  105. github.com/rancher/websocket-proxy/proxy
  106. github.com/rancherio/host-api/healthcheck
  107. github.com/rancherio/websocket-proxy/proxy
  108. github.com/resourced/resourced-master
  109. github.com/resourced/resourced-master/application
  110. github.com/resourced/resourced/agent
  111. github.com/resourced/resourced/libmap
  112. github.com/revolting/leaves/authenticate
  113. github.com/riking/homeapi/marvin/modules/logger
  114. github.com/rkuris/mybot
  115. github.com/scottjab/catbot
  116. github.com/scottjab/catlady
  117. github.com/shashidharatd/coredns/middleware/dnssec
  118. github.com/shawnps/coredns/middleware/cache
  119. github.com/shawnps/coredns/middleware/dnssec
  120. github.com/shubhodeep9/go-MyVIT/api
  121. github.com/silasb/coredns/middleware/cache
  122. github.com/silasb/coredns/middleware/dnssec
  123. github.com/tankbusta/evex/cache
  124. github.com/trevorprater/coredns/middleware/cache
  125. github.com/trevorprater/coredns/middleware/dnssec
  126. github.com/twitchscience/spade
  127. github.com/twitchscience/spade/deglobber
  128. github.com/ulule/limiter
  129. github.com/unigornel/go-tcpip/ipv4
  130. github.com/visemet/mongo/src/mongo/gotools/mongoreplay
  131. github.com/wppurking/limiter
  132. github.com/yulrizka/fam100
  133. go.skia.org/infra/golden/go/trybot
  134. gopkg.in/launchdarkly/go-client.v1/redis
  135. gopkg.in/launchdarkly/go-client.v2
  136. gopkg.in/launchdarkly/go-client.v2/redis