{"id":17528476,"url":"https://github.com/begmaroman/go-ttlcache","last_synced_at":"2025-04-11T13:36:14.036Z","repository":{"id":47294924,"uuid":"515985849","full_name":"begmaroman/go-ttlcache","owner":"begmaroman","description":"An in-memory key =\u003e value store/cache library for Go built on generics","archived":false,"fork":false,"pushed_at":"2025-01-17T10:54:16.000Z","size":19,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T09:49:25.564Z","etag":null,"topics":["cache","cache-storage","generics","golang-library","golang-package","in-memory-caching","key-value","ttl","ttl-cache","ttlcache"],"latest_commit_sha":null,"homepage":"","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/begmaroman.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}},"created_at":"2022-07-20T13:06:36.000Z","updated_at":"2025-01-17T10:54:18.000Z","dependencies_parsed_at":"2022-09-18T17:22:13.686Z","dependency_job_id":null,"html_url":"https://github.com/begmaroman/go-ttlcache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/begmaroman%2Fgo-ttlcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/begmaroman%2Fgo-ttlcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/begmaroman%2Fgo-ttlcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/begmaroman%2Fgo-ttlcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/begmaroman","download_url":"https://codeload.github.com/begmaroman/go-ttlcache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248410152,"owners_count":21098772,"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","cache-storage","generics","golang-library","golang-package","in-memory-caching","key-value","ttl","ttl-cache","ttlcache"],"created_at":"2024-10-20T15:43:50.895Z","updated_at":"2025-04-11T13:36:14.010Z","avatar_url":"https://github.com/begmaroman.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-ttlcache\n\n`go-ttlcache` is a GoLang in-memory `key` =\u003e `value` store/cache that is\nsuitable for applications running on a single machine. Its major advantage is\nthat, being essentially a thread-safe and generically typed `map[K]Item[V]` with expiration\ntimes, it doesn't need to serialize or transmit its contents over the network.\n\nComparable key with any value can be stored, for a given duration or forever, \nand the cache can be safely used by multiple goroutines.\n\nAlthough `go-ttlcache` 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[K, V]()` to create a cache from a deserialized\none) to recover from downtime quickly. (See the docs for `NewFrom[K, V]()` for caveats.)\n\n### Requirements\n\n[GoLang](https://go.dev/) \u003e=1.18\n\n### Installation\n\n`go get github.com/begmaroman/go-ttlcache`\n\n### Usage\n\n```go\nimport (\n\t\"fmt\"\n\t\"time\"\n\t\n    \"github.com/begmaroman/go-ttlcache\"\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. Both key and value should be \"string\" type.\n\tc := ttlcache.New[string, string](5*time.Minute, 10*time.Minute)\n\t\n\t// There could be any comparable key type, and any value. For instance:\n\t// c := ttlcache.New[string, MyType](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\", ttlcache.DefaultExpiration)\n\n\t// Set the value of the key \"baz\" to \"vaz\", 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\", \"vaz\", ttlcache.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)\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 foo, found := c.Get(\"foo\"); found {\n\t\t// ...\n\t}\n}\n```\n\n### Reference\n\n`godoc` or [http://godoc.org/github.com/begmaroman/go-ttlcache](http://godoc.org/github.com/begmaroman/go-ttlcache)\n\nInspired by:\n- https://github.com/jellydator/ttlcache\n- https://github.com/patrickmn/go-cache","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbegmaroman%2Fgo-ttlcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbegmaroman%2Fgo-ttlcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbegmaroman%2Fgo-ttlcache/lists"}