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 304 package(s)

  1. github.com/12foo/casablanca
  2. github.com/12foo/macaque
  3. github.com/Aptomi/aptomi/pkg/external/secrets
  4. github.com/Aptomi/aptomi/pkg/external/users
  5. github.com/Arukim/limitscounter
  6. github.com/Benzinga/npm-feed
  7. github.com/BrianIp/orchestrator/go/inst
  8. github.com/BrianIp/orchestrator/go/logic
  9. github.com/Bwooce/autoblock
  10. github.com/Cepave/open-falcon-backend/modules/query/cache
  11. github.com/Cepave/open-falcon-backend/modules/query/nqm
  12. github.com/Cepave/query/cache
  13. github.com/Cepave/query/nqm
  14. github.com/DataDog/datadog-agent/pkg/util/cache
  15. github.com/DavidLu1997/fast-shortener/shortener
  16. github.com/Djipyy/GoLolApi
  17. github.com/FauxFaux/dingo
  18. github.com/Hevienz/go-rediscache
  19. github.com/Hevienz/go-redisclustercache
  20. github.com/KarhuTeam/Karhu/ressources/collectd-proxy
  21. github.com/MetalMatze/Krautreporter-API/repository
  22. github.com/Moonlington/FloSelfbot
  23. github.com/Moonlington/FloSelfbot/commands
  24. github.com/NyaaPantsu/nyaa/utils/cache
  25. github.com/PuppyKhan/xenia/pkg/mask
  26. github.com/PuppyKhan/xenia/pkg/query
  27. github.com/PuppyKhan/xenia/pkg/regex
  28. github.com/Q42Philips/tollbooth/config
  29. github.com/Raggaer/castro/app
  30. github.com/Raggaer/castro/app/util
  31. github.com/RiftBit/ALS-Go
  32. github.com/RiftBit/go-cld2ch
  33. github.com/RiftBit/xvFavSync
  34. github.com/Riftbit/ALS-Go
  35. github.com/ServiceComb/go-chassis/core/registry
  36. github.com/ServiceComb/go-chassis/session
  37. github.com/Southclaws/cj
  38. github.com/TritonHo/softcache
  39. github.com/TwitchScience/spade
  40. github.com/TwitchScience/spade/deglobber
  41. github.com/WindomZ/go-develop-kit/cache/gocache
  42. github.com/Yanjunhui/chat
  43. github.com/ZeaLoVe/go-utils/wechat
  44. github.com/abeMedia/ira/models
  45. github.com/aelsabbahy/goss
  46. github.com/aksentyev/hubble/exportertools
  47. github.com/albertrdixon/goss
  48. github.com/alexedwards/scs/engine/memstore
  49. github.com/alexedwards/scs/stores/memstore
  50. github.com/animenotifier/notify.moe/middleware
  51. github.com/annchain/angine/trace
  52. github.com/artemnikitin/here-tele-bot/im
  53. github.com/arukim/limitscounter
  54. github.com/asticode/go-cache-manager/cachemanager
  55. github.com/bcho/dmm/proxy_server
  56. github.com/beppeben/m3m3/web
  57. github.com/blackss2/utility/hotserver
  58. github.com/bnadland/groundwork
  59. github.com/bradberger/gocache/drivers/gocache
  60. github.com/cafebazaar/bahram/datasource
  61. github.com/cgclever/go-client/redis
  62. github.com/chyeh/open-falcon-backend/modules/query/cache
  63. github.com/chyeh/open-falcon-backend/modules/query/nqm
  64. github.com/chyeh/query/cache
  65. github.com/chyeh/query/nqm
  66. github.com/cjellick/host-api/healthcheck
  67. github.com/clawio/lib/authenticationwebserviceclient
  68. github.com/clawio/lib/datawebserviceclient
  69. github.com/clawio/lib/metadatawebserviceclient
  70. github.com/clawio/lib/proxiedauthenticationwebservice
  71. github.com/clawio/lib/proxieddatawebservice
  72. github.com/clawio/lib/proxiedmetadatawebservice
  73. github.com/clawio/lib/proxiedocwebservice
  74. github.com/coalalib/coalago
  75. github.com/coalalib/coalago/pools/StoragePools
  76. github.com/cocotyty/httpclient/example
  77. github.com/codingsince1985/geo-golang/cached
  78. github.com/cogolabs/transcend
  79. github.com/coralproject/shelf/internal/xenia/mask
  80. github.com/coralproject/shelf/internal/xenia/query
  81. github.com/coralproject/shelf/internal/xenia/regex
  82. github.com/coralproject/shelf/internal/xenia/script
  83. github.com/coreos/jwtproxy/jwt/noncestorage/local
  84. github.com/crackcomm/googlebot
  85. github.com/crackcomm/renderer/storage
  86. github.com/crosserclaws/open-falcon-backend/modules/query/cache
  87. github.com/crosserclaws/open-falcon-backend/modules/query/nqm
  88. github.com/cyrrill/snap
  89. github.com/dasfoo/rover/auth
  90. github.com/databrary/scs/engine/memstore
  91. github.com/datastream/homekitbridge
  92. github.com/decafe09/scs/engine/memstore
  93. github.com/denderello/jwtproxy/jwt/noncestorage/local
  94. github.com/didip/tollbooth/limiter
  95. github.com/dionyself/golang-cms/core/lib/cache
  96. github.com/dionyself/golang-cms/utils
  97. github.com/divya2661/gossipdb
  98. github.com/donomii/hashare
  99. github.com/dvwallin/sendmeip
  100. github.com/dzen-it/ubm
  101. github.com/ednapiranha/leaves/authenticate
  102. github.com/elastic/apm-server/sourcemap
  103. github.com/empirefox/esecend/captchar
  104. github.com/empirefox/esecend/sec
  105. github.com/empirefox/esecend/sms
  106. github.com/enderlu/alfred-youdao
  107. github.com/errordeveloper/coredns/middleware/cache
  108. github.com/errordeveloper/coredns/middleware/dnssec
  109. github.com/faxal/idp/core
  110. github.com/fluxrad/puckfinder/api
  111. github.com/fnproject/fn/api/agent
  112. github.com/gabrielrussell/mongo-tools/mongoreplay
  113. github.com/ghophp/idp
  114. github.com/giganteous/unsee
  115. github.com/giskarda/songkick-codechallange
  116. github.com/github/orchestrator/go/db
  117. github.com/github/orchestrator/go/inst
  118. github.com/github/orchestrator/go/logic
  119. github.com/github/orchestrator/go/process
  120. github.com/github/orchestrator/go/raft
  121. github.com/goguardian/aws-xray-go/handlers
  122. github.com/google/skia-buildbot/golden/go/trybot
  123. github.com/grokify/alexa-skill-ringcentral-go-demo/bin
  124. github.com/grunmax/GinRedisApi/accs
  125. github.com/grunmax/GinRedisApi/ctrl
  126. github.com/grunmax/GinRedisApi/midd
  127. github.com/grunmax/ginredisapi
  128. github.com/grunmax/ginredisapi/accs
  129. github.com/grunmax/ginredisapi/ctrl
  130. github.com/grunmax/ginredisapi/midd
  131. github.com/gz-c/tollbooth/limiter
  132. github.com/hashicorp/vault/builtin/credential/aws
  133. github.com/hashicorp/vault/builtin/logical/totp
  134. github.com/helderfarias/gwn-proxy/gateway/middleware/limit
  135. github.com/henson/Answer/util
  136. github.com/hhsnopek/10kft
  137. github.com/hien/dingo
  138. github.com/homina/growl
  139. github.com/hopkinsth/gotraining/topics/profiling/project/search
  140. github.com/hverr/go-tcpip/ipv4
  141. github.com/ibuildthecloud/cli/monitor
  142. github.com/ihciah/tg_channel_bot/fetchers
  143. github.com/impactasaurus/server/cache
  144. github.com/inverse-inc/packetfence/go/api-frontend/aaa
  145. github.com/inverse-inc/packetfence/go/caddy/pfsso
  146. github.com/inverse-inc/packetfence/go/coredns/plugin/pfdns
  147. github.com/inverse-inc/packetfence/go/dhcp
  148. github.com/ipfans/echoscs/engine/memstore
  149. github.com/janekolszak/idp
  150. github.com/jbowes/go-client
  151. github.com/jdel/gosspks/cache
  152. github.com/jgeiger/gotraining/topics/profiling/project/search
  153. github.com/johnt337/goss
  154. github.com/joliva-ob/onebox-dynamic-pricing/dataservice
  155. github.com/jpmorganchase/quorum/private/constellation
  156. github.com/jsternberg/grafana/pkg/tsdb/mqe
  157. github.com/karasz/coredns/middleware/cache
  158. github.com/karasz/coredns/middleware/dnssec
  159. github.com/karteek/wdns
  160. github.com/kataras/brook
  161. github.com/keratin/authn-go/authn
  162. github.com/kgaughan/goss
  163. github.com/kpacha/gin-auth0-profile
  164. github.com/ksred/go-monitor
  165. github.com/larixsource/geo-golang/cached
  166. github.com/launchdarkly/go-client/redis
  167. github.com/leelynne/coredns/middleware/cache
  168. github.com/leelynne/coredns/middleware/dnssec
  169. github.com/liderman/text-generator
  170. github.com/linlexing/dbx/data
  171. github.com/linxGnu/goseaweedfs
  172. github.com/liuyanghejerry/brook
  173. github.com/ljagiello/grafana/pkg/tsdb/mqe
  174. github.com/ljagiello/vault/builtin/logical/totp
  175. github.com/lparis/gotraining/topics/profiling/project/search
  176. github.com/lucasem/servemd
  177. github.com/lukedmor/servemd
  178. github.com/mattes/domproxy
  179. github.com/meshwalker/mws/cluster
  180. github.com/metalmatze/krautreporter-api/repository
  181. github.com/mfallone/psiphon-tunnel-core/psiphon/server
  182. github.com/miekg/coredns/middleware/dnssec
  183. github.com/miekg/dingo
  184. github.com/mlabouardy/komiser
  185. github.com/mlabouardy/komiser/handlers
  186. github.com/moira-alert/moira/checker/worker
  187. github.com/moira-alert/moira/cmd/checker
  188. github.com/moira-alert/moira/database/redis
  189. github.com/mongodb/mongo-tools/mongoreplay
  190. github.com/mopsalarm/go-pr0gramm-categories
  191. github.com/moul/as-a-service
  192. github.com/mushorg/go-dpi/types
  193. github.com/myles-mcdonnell/go-monitor
  194. github.com/netsec-ethz/scion/go/cert_srv
  195. github.com/netsec-ethz/scion/go/lib/infra/dedupe
  196. github.com/netsec-ethz/scion/go/lib/sciond
  197. github.com/netxfly/x-crack/models
  198. github.com/netxfly/x-crack/vars
  199. github.com/netxfly/xsec-ip-database/models
  200. github.com/netxfly/xsec-ip-database/web/routers
  201. github.com/nikelmwann/dokku-api-server/dokku
  202. github.com/nikofil/go-dpi/types
  203. github.com/nlittlepoole/observatory/rover
  204. github.com/nmonterroso/lolchest.win/riotapi
  205. github.com/oklahomer/go-sarah
  206. github.com/olivere/dingo
  207. github.com/otaviof/godutch
  208. github.com/outbrain/orchestrator/go/inst
  209. github.com/outbrain/orchestrator/go/logic
  210. github.com/pace543/pokeapigo
  211. github.com/panyingyun/brook
  212. github.com/pavlik/mimozaflowers
  213. github.com/pengfei-xue/host-api/healthcheck
  214. github.com/pforemski/dingo
  215. github.com/pjh130/go/common/storage/cache
  216. github.com/poblahblahblah/resourced-master
  217. github.com/poblahblahblah/resourced-master/application
  218. github.com/poblahblahblah/resourced/libmap
  219. github.com/projectcalico/kube-controllers/pkg/cache
  220. github.com/prologic/pastebin
  221. github.com/raggaer/castro/app
  222. github.com/raggaer/castro/app/util
  223. github.com/rai-project/docker
  224. github.com/rai-project/downloadmanager
  225. github.com/rancher/agent/utilities/utils
  226. github.com/rancher/cli/monitor
  227. github.com/rancher/host-api/healthcheck
  228. github.com/rancher/ingress-controller/controller/rancherglb
  229. github.com/rancher/kubernetes-agent/hostlabels
  230. github.com/rancher/kubernetes-agent/hostwatch
  231. github.com/rancher/lb-controller/controller/rancherglb
  232. github.com/rancher/longhorn-manager-old/kvstore
  233. github.com/rancher/websocket-proxy/proxy
  234. github.com/rancherio/host-api/healthcheck
  235. github.com/rancherio/websocket-proxy/proxy
  236. github.com/rayrod2030/shazam
  237. github.com/resourced/resourced-master
  238. github.com/resourced/resourced-master/application
  239. github.com/resourced/resourced/agent
  240. github.com/resourced/resourced/libmap
  241. github.com/revolting/leaves/authenticate
  242. github.com/riftbit/ALS-Go
  243. github.com/riftbit/xvFavSync
  244. github.com/riking/homeapi/marvin/modules/logger
  245. github.com/riking/marvin/intra
  246. github.com/riking/marvin/modules/logger
  247. github.com/rkuris/mybot
  248. github.com/ruqqq/goprerender
  249. github.com/sapcc/maia/pkg/keystone
  250. github.com/scionproto/scion/go/cert_srv
  251. github.com/scionproto/scion/go/lib/sciond
  252. github.com/scottjab/catbot
  253. github.com/scottjab/catlady
  254. github.com/sdwolfe32/ovrstat/api
  255. github.com/sdwolfe32/slimhttp
  256. github.com/sdwolfe32/tinystat/api
  257. github.com/shashidharatd/coredns/middleware/dnssec
  258. github.com/shawnps/coredns/middleware/cache
  259. github.com/shawnps/coredns/middleware/dnssec
  260. github.com/shubhodeep9/go-MyVIT/api
  261. github.com/silasb/coredns/middleware/cache
  262. github.com/silasb/coredns/middleware/dnssec
  263. github.com/silenceper/qanswer/cache
  264. github.com/stuyspec/cli-uploader
  265. github.com/stuyspec/uploader
  266. github.com/swarmfund/swarm-api
  267. github.com/swarmfund/swarm-horizon/cache
  268. github.com/tankbusta/evex/cache
  269. github.com/tdi/goss
  270. github.com/trevorprater/coredns/middleware/cache
  271. github.com/trevorprater/coredns/middleware/dnssec
  272. github.com/tsileo/broxy
  273. github.com/twitchscience/spade
  274. github.com/twitchscience/spade/deglobber
  275. github.com/txthinking/brook
  276. github.com/txthinking/socks5
  277. github.com/tylerb/gotraining/topics/profiling/project/search
  278. github.com/ulule/gokvstores
  279. github.com/ulule/limiter
  280. github.com/unigornel/go-tcpip/ipv4
  281. github.com/vapor-ware/synse-sdk/sdk
  282. github.com/vibbix/go-shortener
  283. github.com/visemet/mongo/src/mongo/gotools/mongoreplay
  284. github.com/vjeantet/Convergence
  285. github.com/vjeantet/bitfan/core/memory
  286. github.com/vjeantet/bitfan/processors/testutils
  287. github.com/vkuznecovas/mouthful/api
  288. github.com/voidd/dingo
  289. github.com/wianvos/goss
  290. github.com/windmilleng/wm/data/users
  291. github.com/wppurking/limiter
  292. github.com/xeoncross/goworkqueue/example
  293. github.com/xiocode/brook
  294. github.com/yegle/dingo
  295. github.com/yulrizka/fam100
  296. github.com/zero-os/0-core/base/builtin
  297. github.com/zero-os/0-fs/meta
  298. github.com/zet4/FloSelfbot
  299. github.com/zwirec/TGChatScanner/requestHandler/appContext
  300. github.com/zwirec/TGChatScanner/service
  301. go.skia.org/infra/golden/go/trybot
  302. gopkg.in/launchdarkly/go-client.v1/redis
  303. gopkg.in/launchdarkly/go-client.v2
  304. gopkg.in/launchdarkly/go-client.v2/redis

Imported only in test by 1 package(s)

  1. github.com/cocotyty/cache