{"id":19658949,"url":"https://github.com/damianopetrungaro/go-cache","last_synced_at":"2025-10-04T21:17:01.105Z","repository":{"id":38188946,"uuid":"500614574","full_name":"damianopetrungaro/go-cache","owner":"damianopetrungaro","description":"GoCache is an opinionated cache package with simple APIs and provides multiple implementations.","archived":false,"fork":false,"pushed_at":"2022-06-09T18:00:52.000Z","size":62,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-28T20:45:50.534Z","etag":null,"topics":["cache","go","golang","inmemory","redis"],"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/damianopetrungaro.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-06-06T22:41:43.000Z","updated_at":"2023-11-14T14:51:21.000Z","dependencies_parsed_at":"2022-08-18T07:16:04.219Z","dependency_job_id":null,"html_url":"https://github.com/damianopetrungaro/go-cache","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/damianopetrungaro/go-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damianopetrungaro%2Fgo-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damianopetrungaro%2Fgo-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damianopetrungaro%2Fgo-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damianopetrungaro%2Fgo-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/damianopetrungaro","download_url":"https://codeload.github.com/damianopetrungaro/go-cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damianopetrungaro%2Fgo-cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278373516,"owners_count":25976151,"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","status":"online","status_checked_at":"2025-10-04T02:00:05.491Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["cache","go","golang","inmemory","redis"],"created_at":"2024-11-11T15:40:05.329Z","updated_at":"2025-10-04T21:17:01.055Z","avatar_url":"https://github.com/damianopetrungaro.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go Cache\n\n[![codecov](https://codecov.io/gh/damianopetrungaro/go-cache/branch/main/graph/badge.svg?token=5ESXFZo2j2)](https://codecov.io/gh/damianopetrungaro/go-cache)\n\nGoCache is an opinionated cache package\nwith simple APIs and provides multiple implementations.\n\n## Why another cache package?\n\nGoCache is designed with Go 1.18 and leverage the generics implementation,\nreducing the amount of casting needed to provide an efficient cache implementation.\n\nOn top of that, GoCache also requires a context argument allowing context propagation,\nwhich is not available in most of the cache package available today.\n\n## Examples\n\nUsage \n```go\nvar c Cache[string, string]\nvar ctx := context.Background()\n\nconst k = \"key\"\nconst val = \"value\"\n\n// Adds a never expiring item to the cache\nif err := c.Set(ctx, k, val, NoExpiration); err != nil {\n  return err\n}\n\n// Adds an item which will expire after 10 seconds\nif err := c.Set(ctx, k, val, 10*time.Second); err != nil {\n  return err\n}\n\n// Retrieves an item from the cache\nitem, err := c.Get(ctx, k)\nif err != nil {\n  return err\n}\n\n// Deletes an item from the cache\nif err := c.Delete(ctx, k); err != nil {\n  return err\n}\n```\n\nGoCache provides also some errors\n```go\nErrNotSet    = errors.New(\"could not set cache value\")\nErrNotGet    = errors.New(\"could not get cache value\")\nErrNotFound  = fmt.Errorf(\"%w: could not find cache value\", ErrNotGet)\nErrExpired   = fmt.Errorf(\"%w: could not get expired cache value\", ErrNotGet)\nErrNotDelete = errors.New(\"could not delete cache value\")\n```\n\n### In Memory\n\nCreate an InMemory implementation\n\n```go\n// the first generic is a comparable type used as key\n// the second generic is any type used as value\n// the first argument of the factory function represent the max item capacity of the cache\n// when the max capacity gets hit, then all the expired items get deleted and if none is expired \n// then the one closest to the expiry get deleted \ninmem := NewInMemory[string, int](100_000)\n```\n\n### Redis\n\n```go\nimport (\n    goRedis \"github.com/go-redis/redis/v9\"\n)\n\nvar redisClient *goRedis.Client\n\n// the first generic is a comparable type used as key\n// the second generic is any type used as value\n// the first argument of the factory function is the client used to communicate with the redis server\nredisCache := redis.New[string, int](redisClient)\n```\n\n```go\nimport (\n    goRedis \"github.com/go-redis/redis/v9\"\n)\n\nvar redisClient *goRedis.Client\n\n// the first generic is a comparable type used as key\n// the second generic is any type used as value\n// the first argument of the factory function is the client used to communicate with the redis server\nredisCache := redis.New[string, int](redisClient)\n\n// When complex types are passed as values, \n// you can pass an EncodeDecodeOption to specify how your type needs to be serialized\n// a default implementation is given as part of the library which relies on the encoding/json package.\nredisCache := redis.New[string, user](\n    redisClient,\n    EncodeDecodeOption[string, user](DefaultEncoder[user], DefaultDecoder[*user]),\n)\n```\n\n### Multi Level\n\n```go\nvar c1, c2, c3 cache.Cache\n\n// the first generic is a comparable type used as key\n// the second generic is any type used as value\n// the arguments passed are the cache levels\nmultilvl := NewMultiLevel[string, int](c1,c2, c3)\n\n// The behavior of the multilvel cache are documented in the method:\n\n// Get traverse all the caches, if all of them fail it returns a generic ErrNotGet\n// Set traverse all the caches, if all of them fail it returns a generic ErrNotSet\n// Delete traverse all the caches, if all of them fail it returns a generic ErrNotDelete\n```\n\n## Performances\n\nGoCache is a really fast caching solution,\nwith 0 allocations as well as crazy performances.\n\nBenchmarks comparing it to [dgraph/ristretto](https://github.com/dgraph-io/ristretto),\n[allegro/bigcache](https://github.com/allegro/bigcache),\nand [patrickmn/go-cache](https://github.com/patrickmn/go-cache)\n\n```text\n// dgraph/ristretto\n\ngoos: darwin\ngoarch: amd64\npkg: github.com/damianopetrungaro/go-cache/benchmarks/cache/dgraph\ncpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\nBenchmarkLogger/dgraph/ristretto.empty-12                4819352              1222 ns/op             240 B/op          6 allocs/op\nBenchmarkLogger/dgraph/ristretto.prefilled-12            4267911              1328 ns/op             240 B/op          6 allocs/op\nPASS\nok      github.com/damianopetrungaro/go-cache/benchmarks/cache/dgraph   14.753s\n```\n\n```\n// patrickmn/go-cache\n\ncd ./patrickmn \u0026\u0026 go1.17.11 test ./... -bench=. -benchmem -benchtime=5s\ngoos: darwin\ngoarch: amd64\npkg: github.com/damianopetrungaro/go-cache/benchmarks/cache/patrickmn\ncpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\nBenchmarkLogger/patrickmn/go-cache.empty-12             16924348               340.0 ns/op            24 B/op          1 allocs/op\nBenchmarkLogger/patrickmn/go-cache.prefilled-12         17133223               367.0 ns/op            24 B/op          1 allocs/op\nBenchmarkLogger/patrickmn/go-cache.prefilled_with_cleanup-12            16663183               366.7 ns/op            24 B/op          1 allocs/op\nPASS\nok      github.com/damianopetrungaro/go-cache/benchmarks/cache/patrickmn        19.469s\n```\n\n```\n// allegro/bigcache\n\ncd ./allegro \u0026\u0026 go1.17.11 test ./... -bench=. -benchmem -benchtime=5s\ngoos: darwin\ngoarch: amd64\npkg: github.com/damianopetrungaro/go-cache/benchmarks/cache/allegro\ncpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\nBenchmarkLogger/allegro/bigcache.empty-12               13124148               593.7 ns/op            91 B/op          0 allocs/op\nBenchmarkLogger/allegro/bigcache.prefilled-12           10756856               464.9 ns/op            55 B/op          0 allocs/op\nBenchmarkLogger/allegro/bigcache.prefilled_with_cleanup-12              14478454               425.3 ns/op            10 B/op          0 allocs/op\nPASS\nok      github.com/damianopetrungaro/go-cache/benchmarks/cache/allegro  22.240s\n\n```\n\n```\n// damianopetrungaro/go-cache\n\ncd ./damianopetrungaro \u0026\u0026 go1.18.3 test ./... -bench=. -benchmem -benchtime=5s\ngoos: darwin\ngoarch: amd64\npkg: github.com/damianopetrungaro/go-cache/benchmarks/cache\ncpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\nBenchmarkLogger/damianopetrungaro/go-cache.empty-12             22228510               270.5 ns/op             0 B/op          0 allocs/op\nBenchmarkLogger/damianopetrungaro/go-cache.prefilled-12         20715753               268.8 ns/op             0 B/op          0 allocs/op\nBenchmarkLogger/damianopetrungaro/go-cache.prefilled_with_cleanup-12            21777249               269.2 ns/op             0 B/op          0 allocs/op\nPASS\nok      github.com/damianopetrungaro/go-cache/benchmarks/cache  19.755s\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdamianopetrungaro%2Fgo-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdamianopetrungaro%2Fgo-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdamianopetrungaro%2Fgo-cache/lists"}