{"id":13481250,"url":"https://github.com/patrickmn/go-cache","last_synced_at":"2025-12-18T07:39:06.652Z","repository":{"id":2143254,"uuid":"3087541","full_name":"patrickmn/go-cache","owner":"patrickmn","description":"An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.","archived":false,"fork":false,"pushed_at":"2023-11-20T05:12:16.000Z","size":126,"stargazers_count":8441,"open_issues_count":75,"forks_count":889,"subscribers_count":119,"default_branch":"master","last_synced_at":"2025-05-02T11:53:48.730Z","etag":null,"topics":["cache","go","library"],"latest_commit_sha":null,"homepage":"https://patrickmn.com/projects/go-cache/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patrickmn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-01-02T13:07:13.000Z","updated_at":"2025-05-02T06:39:32.000Z","dependencies_parsed_at":"2024-06-18T10:54:54.907Z","dependency_job_id":null,"html_url":"https://github.com/patrickmn/go-cache","commit_stats":{"total_commits":156,"total_committers":8,"mean_commits":19.5,"dds":0.2948717948717948,"last_synced_commit":"46f407853014144407b6c2ec7ccc76bf67958d93"},"previous_names":["pmylund/go-cache"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickmn%2Fgo-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickmn%2Fgo-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickmn%2Fgo-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickmn%2Fgo-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patrickmn","download_url":"https://codeload.github.com/patrickmn/go-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252590526,"owners_count":21772935,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","go","library"],"created_at":"2024-07-31T17:00:50.094Z","updated_at":"2025-12-18T07:39:06.571Z","avatar_url":"https://github.com/patrickmn.png","language":"Go","readme":"# go-cache\n\ngo-cache is an in-memory key:value store/cache similar to memcached that is\nsuitable for applications running on a single machine. Its major advantage is\nthat, being essentially a thread-safe `map[string]interface{}` with expiration\ntimes, it doesn't need to serialize or transmit its contents over the network.\n\nAny object can be stored, for a given duration or forever, and the cache can be\nsafely used by multiple goroutines.\n\nAlthough go-cache isn't meant to be used as a persistent datastore, the entire\ncache can be saved to and loaded from a file (using `c.Items()` to retrieve the\nitems map to serialize, and `NewFrom()` to create a cache from a deserialized\none) to recover from downtime quickly. (See the docs for `NewFrom()` for caveats.)\n\n### Installation\n\n`go get github.com/patrickmn/go-cache`\n\n### Usage\n\n```go\nimport (\n\t\"fmt\"\n\t\"github.com/patrickmn/go-cache\"\n\t\"time\"\n)\n\nfunc main() {\n\t// Create a cache with a default expiration time of 5 minutes, and which\n\t// purges expired items every 10 minutes\n\tc := cache.New(5*time.Minute, 10*time.Minute)\n\n\t// Set the value of the key \"foo\" to \"bar\", with the default expiration time\n\tc.Set(\"foo\", \"bar\", cache.DefaultExpiration)\n\n\t// Set the value of the key \"baz\" to 42, with no expiration time\n\t// (the item won't be removed until it is re-set, or removed using\n\t// c.Delete(\"baz\")\n\tc.Set(\"baz\", 42, cache.NoExpiration)\n\n\t// Get the string associated with the key \"foo\" from the cache\n\tfoo, found := c.Get(\"foo\")\n\tif found {\n\t\tfmt.Println(foo)\n\t}\n\n\t// Since Go is statically typed, and cache values can be anything, type\n\t// assertion is needed when values are being passed to functions that don't\n\t// take arbitrary types, (i.e. interface{}). The simplest way to do this for\n\t// values which will only be used once--e.g. for passing to another\n\t// function--is:\n\tfoo, found := c.Get(\"foo\")\n\tif found {\n\t\tMyFunction(foo.(string))\n\t}\n\n\t// This gets tedious if the value is used several times in the same function.\n\t// You might do either of the following instead:\n\tif x, found := c.Get(\"foo\"); found {\n\t\tfoo := x.(string)\n\t\t// ...\n\t}\n\t// or\n\tvar foo string\n\tif x, found := c.Get(\"foo\"); found {\n\t\tfoo = x.(string)\n\t}\n\t// ...\n\t// foo can then be passed around freely as a string\n\n\t// Want performance? Store pointers!\n\tc.Set(\"foo\", \u0026MyStruct, cache.DefaultExpiration)\n\tif x, found := c.Get(\"foo\"); found {\n\t\tfoo := x.(*MyStruct)\n\t\t\t// ...\n\t}\n}\n```\n\n### Reference\n\n`godoc` or [http://godoc.org/github.com/patrickmn/go-cache](http://godoc.org/github.com/patrickmn/go-cache)\n","funding_links":[],"categories":["开源类库","Key-Value Store","Go","Open source library","In-memory libraries (by language)","Repositories","library","Uncategorized"],"sub_categories":["缓存","Cache"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickmn%2Fgo-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatrickmn%2Fgo-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickmn%2Fgo-cache/lists"}