{"id":27880869,"url":"https://github.com/atomicgo/cache","last_synced_at":"2026-04-26T18:01:51.625Z","repository":{"id":63775581,"uuid":"570571564","full_name":"atomicgo/cache","owner":"atomicgo","description":"🧠 A generic, thread-safe cache implementation in Go for improved performance!","archived":false,"fork":false,"pushed_at":"2026-04-25T19:21:11.000Z","size":56,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-04-25T19:22:20.001Z","etag":null,"topics":["atomicgo","cache","go","golang","golang-library"],"latest_commit_sha":null,"homepage":"https://atomicgo.dev","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/atomicgo.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,"zenodo":null},"funding":{"github":["MarvinJWendt"]}},"created_at":"2022-11-25T14:09:04.000Z","updated_at":"2026-04-25T19:21:16.000Z","dependencies_parsed_at":"2025-05-05T04:39:10.787Z","dependency_job_id":"5ab83c99-7691-4dc8-b930-03b6b359a342","html_url":"https://github.com/atomicgo/cache","commit_stats":{"total_commits":12,"total_committers":1,"mean_commits":12.0,"dds":0.0,"last_synced_commit":"362b405424446863a31f9450820c4af93a488208"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":"atomicgo/template","purl":"pkg:github/atomicgo/cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/atomicgo","download_url":"https://codeload.github.com/atomicgo/cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Fcache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32307015,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T17:23:19.671Z","status":"ssl_error","status_checked_at":"2026-04-26T17:23:19.195Z","response_time":129,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["atomicgo","cache","go","golang","golang-library"],"created_at":"2025-05-05T04:39:01.613Z","updated_at":"2026-04-26T18:01:51.619Z","avatar_url":"https://github.com/atomicgo.png","language":"Go","funding_links":["https://github.com/sponsors/MarvinJWendt"],"categories":[],"sub_categories":[],"readme":"\u003c!--\n\n\n\n┌───────────────────────────────────────────────────────────────────┐\n│                                                                   │\n│                          IMPORTANT NOTE                           │\n│                                                                   │\n│               This file is automatically generated                │\n│           All manual modifications will be overwritten            │\n│                                                                   │\n└───────────────────────────────────────────────────────────────────┘\n\n\n\n--\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/atomicgo/atomicgo/main/assets/header.png\" alt=\"AtomicGo\"\u003e\n\u003c/p\u003e\n\n\u003c/br\u003e\n\u003c/br\u003e\n\n\u003ch1 align=\"center\"\u003eAtomicGo | cache\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://img.shields.io/endpoint?url=https%3A%2F%2Fatomicgo.dev%2Fapi%2Fshields%2Fcache\u0026style=flat-square\" alt=\"Downloads\"\u003e\n\n\u003ca href=\"https://github.com/atomicgo/cache/releases\"\u003e\n\u003cimg src=\"https://img.shields.io/github/v/release/atomicgo/cache?style=flat-square\" alt=\"Latest Release\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://codecov.io/gh/atomicgo/cache\" target=\"_blank\"\u003e\n\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/atomicgo/cache/go.yml?style=flat-square\" alt=\"Tests\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://codecov.io/gh/atomicgo/cache\" target=\"_blank\"\u003e\n\u003cimg src=\"https://img.shields.io/codecov/c/gh/atomicgo/cache?color=magenta\u0026logo=codecov\u0026style=flat-square\" alt=\"Coverage\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://codecov.io/gh/atomicgo/cache\"\u003e\n\u003c!-- unittestcount:start --\u003e\u003cimg src=\"https://img.shields.io/badge/Unit_Tests-37-magenta?style=flat-square\" alt=\"Unit test count\"\u003e\u003c!-- unittestcount:end --\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://opensource.org/licenses/MIT\" target=\"_blank\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square\" alt=\"License: MIT\"\u003e\n\u003c/a\u003e\n  \n\u003ca href=\"https://goreportcard.com/report/github.com/atomicgo/cache\" target=\"_blank\"\u003e\n\u003cimg src=\"https://goreportcard.com/badge/github.com/atomicgo/cache?style=flat-square\" alt=\"Go report\"\u003e\n\u003c/a\u003e   \n\n\u003c/p\u003e\n\n---\n\n\u003cp align=\"center\"\u003e\n\u003cstrong\u003e\u003ca href=\"https://pkg.go.dev/atomicgo.dev/cache#section-documentation\" target=\"_blank\"\u003eDocumentation\u003c/a\u003e\u003c/strong\u003e\n|\n\u003cstrong\u003e\u003ca href=\"https://github.com/atomicgo/atomicgo/blob/main/CONTRIBUTING.md\" target=\"_blank\"\u003eContributing\u003c/a\u003e\u003c/strong\u003e\n|\n\u003cstrong\u003e\u003ca href=\"https://github.com/atomicgo/atomicgo/blob/main/CODE_OF_CONDUCT.md\" target=\"_blank\"\u003eCode of Conduct\u003c/a\u003e\u003c/strong\u003e\n\u003c/p\u003e\n\n---\n\n\u003cp align=\"center\"\u003e\n\u003ctable\u003e\n\u003ctbody\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/p\u003e\n\u003ch3  align=\"center\"\u003e\u003cpre\u003ego get atomicgo.dev/cache\u003c/pre\u003e\u003c/h3\u003e\n\u003cp align=\"center\"\u003e\n\u003ctable\u003e\n\u003ctbody\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/p\u003e\n\n\u003c!-- gomarkdoc:embed:start --\u003e\n\n\u003c!-- Code generated by gomarkdoc. DO NOT EDIT --\u003e\n\n# cache\n\n```go\nimport \"atomicgo.dev/cache\"\n```\n\nPackage cache is a generic, fast and thread\\-safe cache implementation to improve performance of your Go applications.\n\n## Index\n\n- [type Cache](\u003c#Cache\u003e)\n  - [func New\\[T any\\]\\(options ...Options\\) \\*Cache\\[T\\]](\u003c#New\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) Close\\(\\)](\u003c#Cache[T].Close\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) Contains\\(key string\\) bool](\u003c#Cache[T].Contains\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) Delete\\(key string\\)](\u003c#Cache[T].Delete\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) EnableAutoPurge\\(purgeInterval ...time.Duration\\) \\*Cache\\[T\\]](\u003c#Cache[T].EnableAutoPurge\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) Expired\\(key string\\) bool](\u003c#Cache[T].Expired\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) Get\\(key string\\) T](\u003c#Cache[T].Get\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) GetEntry\\(key string\\) \\*Entry\\[T\\]](\u003c#Cache[T].GetEntry\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) GetExpiration\\(key string\\) time.Duration](\u003c#Cache[T].GetExpiration\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) GetOrSet\\(key string, callback func\\(\\) T, expiration ...time.Duration\\) T](\u003c#Cache[T].GetOrSet\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) Keys\\(\\) \\[\\]string](\u003c#Cache[T].Keys\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) Purge\\(\\)](\u003c#Cache[T].Purge\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) PurgeExpired\\(\\)](\u003c#Cache[T].PurgeExpired\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) Set\\(key string, value T, expiration ...time.Duration\\)](\u003c#Cache[T].Set\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) SetExpiration\\(expiration time.Duration\\)](\u003c#Cache[T].SetExpiration\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) Size\\(\\) int](\u003c#Cache[T].Size\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) StopAutoPurge\\(\\)](\u003c#Cache[T].StopAutoPurge\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) ValidKeys\\(\\) \\[\\]string](\u003c#Cache[T].ValidKeys\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) ValidSize\\(\\) int](\u003c#Cache[T].ValidSize\u003e)\n  - [func \\(c \\*Cache\\[T\\]\\) Values\\(\\) \\[\\]T](\u003c#Cache[T].Values\u003e)\n- [type Entry](\u003c#Entry\u003e)\n  - [func \\(e Entry\\[T\\]\\) Expired\\(\\) bool](\u003c#Entry[T].Expired\u003e)\n  - [func \\(e \\*Entry\\[T\\]\\) SetExpiration\\(expiration time.Duration\\)](\u003c#Entry[T].SetExpiration\u003e)\n- [type Options](\u003c#Options\u003e)\n\n\n\u003ca name=\"Cache\"\u003e\u003c/a\u003e\n## type [Cache](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L9-L16\u003e)\n\nCache is a fast and thread\\-safe cache implementation with expiration.\n\n```go\ntype Cache[T any] struct {\n    Options Options\n    // contains filtered or unexported fields\n}\n```\n\n\u003ca name=\"New\"\u003e\u003c/a\u003e\n### func [New](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L26\u003e)\n\n```go\nfunc New[T any](options ...Options) *Cache[T]\n```\n\nNew returns a new Cache. The default Expiration time is 0, which means no Expiration.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\t// Create a cache for string values, without any options.\n\tcache.New[string]()\n\n\t// Create a cache for string values, with options.\n\tcache.New[string](cache.Options{\n\t\tDefaultExpiration: time.Second * 10,\n\t})\n\n\t// Create a cache for int values, without any options.\n\tcache.New[int]()\n\n\t// Create a cache for int values, with options.\n\tcache.New[int](cache.Options{\n\t\tDefaultExpiration: time.Second * 10,\n\t})\n\n\t// Create a cache for any values, without any options.\n\tcache.New[any]()\n\n\t// Create a cache for any values, with options.\n\tcache.New[any](cache.Options{\n\t\tDefaultExpiration: time.Second * 10,\n\t})\n}\n```\n\n\n\n\u003ca name=\"Cache[T].Close\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [Close](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L321\u003e)\n\n```go\nfunc (c *Cache[T]) Close()\n```\n\nClose purges the cache and stops the auto purge goroutine, if active.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\")\n\n\t// Close the cache.\n\tc.Close()\n\n\t// Get the size.\n\tfmt.Println(c.Size())\n\n}\n```\n\n#### Output\n\n```\n0\n```\n\n\n\n\u003ca name=\"Cache[T].Contains\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [Contains](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L168\u003e)\n\n```go\nfunc (c *Cache[T]) Contains(key string) bool\n```\n\nContains returns true if the key is in the cache, and the key is not expired.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\")\n\n\t// Check if the cache contains a key.\n\tfmt.Println(c.Contains(\"1\"))\n\tfmt.Println(c.Contains(\"4\"))\n\n}\n```\n\n#### Output\n\n```\ntrue\nfalse\n```\n\n\n\n\u003ca name=\"Cache[T].Delete\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [Delete](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L201\u003e)\n\n```go\nfunc (c *Cache[T]) Delete(key string)\n```\n\nDelete removes the key from the cache.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\")\n\n\t// Delete a key.\n\tc.Delete(\"3\")\n\n\t// Get the size.\n\tfmt.Println(c.Size())\n\n}\n```\n\n#### Output\n\n```\n2\n```\n\n\n\n\u003ca name=\"Cache[T].EnableAutoPurge\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [EnableAutoPurge](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L42\u003e)\n\n```go\nfunc (c *Cache[T]) EnableAutoPurge(purgeInterval ...time.Duration) *Cache[T]\n```\n\nEnableAutoPurge starts a goroutine that purges expired keys from the cache. The interval is the time between purges. If the interval is 0, the default interval of the cache options is used. If the cache options do not specify a default interval, the default interval is 1 minute.\n\n\u003ca name=\"Cache[T].Expired\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [Expired](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L158\u003e)\n\n```go\nfunc (c *Cache[T]) Expired(key string) bool\n```\n\nExpired returns true if the key is expired.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string](cache.Options{\n\t\tDefaultExpiration: time.Millisecond * 10,\n\t})\n\n\t// Set a value for a key.\n\tc.Set(\"1\", \"one\")\n\n\t// Check if the key is expired.\n\tfmt.Println(c.Expired(\"1\"))\n\n\t// Sleep for 10ms to let the key expire.\n\ttime.Sleep(time.Millisecond * 10)\n\n\t// Check if the key is expired.\n\tfmt.Println(c.Expired(\"1\"))\n\n}\n```\n\n#### Output\n\n```\nfalse\ntrue\n```\n\n\n\n\u003ca name=\"Cache[T].Get\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [Get](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L106\u003e)\n\n```go\nfunc (c *Cache[T]) Get(key string) T\n```\n\nGet returns the value for a key. If the key does not exist, nil is returned. If the key is expired, the zero value is returned and the key is deleted.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Set a value for a key.\n\tc.Set(\"1\", \"one\")\n\n\t// Get the value for a key.\n\tfmt.Println(c.Get(\"1\"))\n\n}\n```\n\n#### Output\n\n```\none\n```\n\n\n\n\u003ca name=\"Cache[T].GetEntry\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [GetEntry](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L124\u003e)\n\n```go\nfunc (c *Cache[T]) GetEntry(key string) *Entry[T]\n```\n\nGetEntry returns the Entry for a key. If the key does not exist, nil is returned.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string](cache.Options{\n\t\tDefaultExpiration: time.Second * 10,\n\t})\n\n\t// Set a value for a key.\n\tc.Set(\"1\", \"one\")\n\n\t// Get the entry for a key.\n\tentry := c.GetEntry(\"1\")\n\tfmt.Println(entry.Value)\n\tfmt.Println(entry.Expiration)\n\n}\n```\n\n#### Output\n\n```\none\n10s\n```\n\n\n\n\u003ca name=\"Cache[T].GetExpiration\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [GetExpiration](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L188\u003e)\n\n```go\nfunc (c *Cache[T]) GetExpiration(key string) time.Duration\n```\n\nGetExpiration returns the Expiration time for a key.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string](cache.Options{\n\t\tDefaultExpiration: time.Second * 10,\n\t})\n\n\t// Set a value for a key.\n\tc.Set(\"1\", \"one\")\n\n\t// Get the expiration for a key.\n\tfmt.Println(c.GetExpiration(\"1\"))\n\n}\n```\n\n#### Output\n\n```\n10s\n```\n\n\n\n\u003ca name=\"Cache[T].GetOrSet\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [GetOrSet](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L134\u003e)\n\n```go\nfunc (c *Cache[T]) GetOrSet(key string, callback func() T, expiration ...time.Duration) T\n```\n\nGetOrSet returns the value for a key. If the key does not exist, the value is set to the result of the callback function and returned. If the key is expired, the value is set to the result of the callback function and returned.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Try to get or set a value for a key.\n\tc.GetOrSet(\"1\", func() string {\n\t\treturn \"one\"\n\t})\n\n\tfmt.Println(c.Get(\"1\"))\n\n\t// try to get or set a value for an existing key.\n\tc.GetOrSet(\"1\", func() string {\n\t\treturn \"something else\"\n\t})\n\tfmt.Println(c.Get(\"1\"))\n\n\t// delete the key\n\tc.Delete(\"1\")\n\n\t// try to get or set a value for a non-existing key.\n\tc.GetOrSet(\"1\", func() string {\n\t\treturn \"something else\"\n\t})\n\tfmt.Println(c.Get(\"1\"))\n\n}\n```\n\n#### Output\n\n```\none\none\nsomething else\n```\n\n\n\n\u003ca name=\"Cache[T].Keys\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [Keys](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L253\u003e)\n\n```go\nfunc (c *Cache[T]) Keys() []string\n```\n\nKeys returns a slice of all keys in the cache, expired or not.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\")\n\n\t// Get the keys.\n\tkeys := c.Keys()\n\tsort.Strings(keys)\n\n\tfmt.Println(keys)\n\n}\n```\n\n#### Output\n\n```\n[1 2 3]\n```\n\n\n\n\u003ca name=\"Cache[T].Purge\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [Purge](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L209\u003e)\n\n```go\nfunc (c *Cache[T]) Purge()\n```\n\nPurge removes all keys from the cache.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\")\n\n\t// Purge the cache.\n\tc.Purge()\n\n\t// Check if the cache is empty.\n\tfmt.Println(c.Size())\n\n}\n```\n\n#### Output\n\n```\n0\n```\n\n\n\n\u003ca name=\"Cache[T].PurgeExpired\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [PurgeExpired](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L217\u003e)\n\n```go\nfunc (c *Cache[T]) PurgeExpired()\n```\n\nPurgeExpired removes all expired keys from the cache.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string](cache.Options{\n\t\tDefaultExpiration: time.Second,\n\t})\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\", time.Millisecond*10)\n\tc.Set(\"2\", \"two\", time.Millisecond*10)\n\tc.Set(\"3\", \"three\")\n\n\t// Purge the expired keys.\n\tc.PurgeExpired()\n\tfmt.Println(c.Size())\n\n\t// Sleep for 10ms to let the first two keys expire.\n\ttime.Sleep(time.Millisecond * 10)\n\n\t// Purge the expired keys.\n\tc.PurgeExpired()\n\tfmt.Println(c.Size())\n\n}\n```\n\n#### Output\n\n```\n3\n1\n```\n\n\n\n\u003ca name=\"Cache[T].Set\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [Set](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L87\u003e)\n\n```go\nfunc (c *Cache[T]) Set(key string, value T, expiration ...time.Duration)\n```\n\nSet sets the value for a key. If the key already exists, the value is overwritten. If the Expiration time is 0, the default Expiration time is used.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Set a value for a key.\n\tc.Set(\"1\", \"one\")\n\n\t// Set a value for a key with a custom expiration.\n\tc.Set(\"2\", \"two\", time.Second*10)\n}\n```\n\n\n\n\u003ca name=\"Cache[T].SetExpiration\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [SetExpiration](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L178\u003e)\n\n```go\nfunc (c *Cache[T]) SetExpiration(expiration time.Duration)\n```\n\nSetExpiration sets the Expiration time all keys in the cache.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string](cache.Options{\n\t\tDefaultExpiration: time.Second * 10,\n\t})\n\n\t// Set a value for a key.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\", time.Second*1337)\n\n\t// Set the expiration for a key.\n\tc.SetExpiration(time.Second * 20)\n\n\t// Get the expiration for a key.\n\tfmt.Println(c.GetExpiration(\"1\"))\n\tfmt.Println(c.GetExpiration(\"2\"))\n\tfmt.Println(c.GetExpiration(\"3\"))\n\n}\n```\n\n#### Output\n\n```\n20s\n20s\n20s\n```\n\n\n\n\u003ca name=\"Cache[T].Size\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [Size](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L229\u003e)\n\n```go\nfunc (c *Cache[T]) Size() int\n```\n\nSize returns the number of keys in the cache, expired or not.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\")\n\n\t// Get the size.\n\tfmt.Println(c.Size())\n\n}\n```\n\n#### Output\n\n```\n3\n```\n\n\n\n\u003ca name=\"Cache[T].StopAutoPurge\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [StopAutoPurge](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L301\u003e)\n\n```go\nfunc (c *Cache[T]) StopAutoPurge()\n```\n\nStopAutoPurge stops the auto purge goroutine.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string](cache.Options{\n\t\tAutoPurgeInterval: time.Millisecond * 10,\n\t})\n\tc.EnableAutoPurge()\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\")\n\n\t// Stop the auto purge.\n\tc.StopAutoPurge()\n\n\t// Sleep for 10 milliseconds to let the auto purge run.\n\ttime.Sleep(time.Millisecond * 10)\n\n\t// Get the size.\n\tfmt.Println(c.Size())\n\n}\n```\n\n#### Output\n\n```\n3\n```\n\n\n\n\u003ca name=\"Cache[T].ValidKeys\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [ValidKeys](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L269\u003e)\n\n```go\nfunc (c *Cache[T]) ValidKeys() []string\n```\n\nValidKeys returns a slice of all valid keys in the cache.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\")\n\tc.Set(\"expiresSoon\", \"data\", time.Millisecond*10)\n\n\tvalidKeys := c.ValidKeys()\n\tsort.Strings(validKeys)\n\n\tfmt.Println(validKeys)\n\n\t// Sleep for 10ms to let the \"expiresSoon\" key expire.\n\ttime.Sleep(time.Millisecond * 10)\n\n\t// Get the valid keys.\n\tvalidKeys = c.ValidKeys()\n\tsort.Strings(validKeys)\n\tfmt.Println(validKeys)\n\n}\n```\n\n#### Output\n\n```\n[1 2 3 expiresSoon]\n[1 2 3]\n```\n\n\n\n\u003ca name=\"Cache[T].ValidSize\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [ValidSize](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L237\u003e)\n\n```go\nfunc (c *Cache[T]) ValidSize() int\n```\n\nValidSize returns the number of keys in the cache that are not expired.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\")\n\tc.Set(\"expiresSoon\", \"data\", time.Millisecond*10)\n\n\tfmt.Println(c.ValidSize())\n\n\t// Sleep for 10ms to let the \"expiresSoon\" key expire.\n\ttime.Sleep(time.Millisecond * 10)\n\n\t// Get the valid size.\n\tfmt.Println(c.ValidSize())\n\n}\n```\n\n#### Output\n\n```\n4\n3\n```\n\n\n\n\u003ca name=\"Cache[T].Values\"\u003e\u003c/a\u003e\n### func \\(\\*Cache\\[T\\]\\) [Values](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L285\u003e)\n\n```go\nfunc (c *Cache[T]) Values() []T\n```\n\nValues returns a slice of all values in the cache.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string]()\n\n\t// Fill the cache with some values.\n\tc.Set(\"1\", \"one\")\n\tc.Set(\"2\", \"two\")\n\tc.Set(\"3\", \"three\")\n\n\t// Get the values.\n\tvalues := c.Values()\n\tsort.Strings(values)\n\n\tfmt.Println(values)\n\n}\n```\n\n#### Output\n\n```\n[one three two]\n```\n\n\n\n\u003ca name=\"Entry\"\u003e\u003c/a\u003e\n## type [Entry](\u003chttps://github.com/atomicgo/cache/blob/main/cache-entry.go#L6-L10\u003e)\n\nEntry is a cache entry.\n\n```go\ntype Entry[T any] struct {\n    Value      T\n    CachedAt   time.Time\n    Expiration time.Duration\n}\n```\n\n\u003ca name=\"Entry[T].Expired\"\u003e\u003c/a\u003e\n### func \\(Entry\\[T\\]\\) [Expired](\u003chttps://github.com/atomicgo/cache/blob/main/cache-entry.go#L13\u003e)\n\n```go\nfunc (e Entry[T]) Expired() bool\n```\n\nExpired returns if the Entry is expired.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string](cache.Options{\n\t\tDefaultExpiration: time.Millisecond * 10,\n\t})\n\n\t// Set a value for a key.\n\tc.Set(\"1\", \"one\")\n\n\t// Get the entry for a key.\n\tentry := c.GetEntry(\"1\")\n\n\t// Check if the entry is expired.\n\tfmt.Println(entry.Expired())\n\n\t// Sleep for 10ms to let the entry expire.\n\ttime.Sleep(time.Millisecond * 10)\n\n\t// Check if the entry is expired.\n\tfmt.Println(entry.Expired())\n\n}\n```\n\n#### Output\n\n```\nfalse\ntrue\n```\n\n\n\n\u003ca name=\"Entry[T].SetExpiration\"\u003e\u003c/a\u003e\n### func \\(\\*Entry\\[T\\]\\) [SetExpiration](\u003chttps://github.com/atomicgo/cache/blob/main/cache-entry.go#L22\u003e)\n\n```go\nfunc (e *Entry[T]) SetExpiration(expiration time.Duration)\n```\n\nSetExpiration sets the Expiration time for the Entry.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"atomicgo.dev/cache\"\n)\n\nfunc main() {\n\tc := cache.New[string](cache.Options{\n\t\tDefaultExpiration: time.Second * 10,\n\t})\n\n\t// Set a value for a key.\n\tc.Set(\"1\", \"one\")\n\n\t// Get the entry for a key.\n\tentry := c.GetEntry(\"1\")\n\n\t// Set the expiration for the entry.\n\tentry.SetExpiration(time.Second * 20)\n\n\t// Get the expiration for a key.\n\tfmt.Println(c.GetExpiration(\"1\"))\n\n}\n```\n\n#### Output\n\n```\n20s\n```\n\n\n\n\u003ca name=\"Options\"\u003e\u003c/a\u003e\n## type [Options](\u003chttps://github.com/atomicgo/cache/blob/main/cache.go#L19-L22\u003e)\n\nOptions can be passed to New to configure the cache.\n\n```go\ntype Options struct {\n    DefaultExpiration time.Duration\n    AutoPurgeInterval time.Duration\n}\n```\n\nGenerated by [gomarkdoc](\u003chttps://github.com/princjef/gomarkdoc\u003e)\n\n\n\u003c!-- gomarkdoc:embed:end --\u003e\n\n---\n\n\u003e [AtomicGo.dev](https://atomicgo.dev) \u0026nbsp;\u0026middot;\u0026nbsp;\n\u003e with ❤️ by [@MarvinJWendt](https://github.com/MarvinJWendt) |\n\u003e [mjw.dev](https://mjw.dev)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatomicgo%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatomicgo%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatomicgo%2Fcache/lists"}