{"id":28268679,"url":"https://github.com/alexssd7/cache","last_synced_at":"2025-09-17T23:19:04.943Z","repository":{"id":57659368,"uuid":"441667101","full_name":"AlexSSD7/cache","owner":"AlexSSD7","description":"CDN-like in-memory cache with shielding and Go 1.18 Generics","archived":false,"fork":false,"pushed_at":"2022-04-24T10:37:12.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-12T03:46:53.212Z","etag":null,"topics":["golang","golang-generics"],"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/AlexSSD7.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":"2021-12-25T11:40:49.000Z","updated_at":"2023-08-25T19:19:13.000Z","dependencies_parsed_at":"2022-09-08T00:10:27.373Z","dependency_job_id":null,"html_url":"https://github.com/AlexSSD7/cache","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/AlexSSD7/cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexSSD7%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexSSD7%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexSSD7%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexSSD7%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexSSD7","download_url":"https://codeload.github.com/AlexSSD7/cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexSSD7%2Fcache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275680615,"owners_count":25508611,"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-09-17T02:00:09.119Z","response_time":84,"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":["golang","golang-generics"],"created_at":"2025-05-20T15:13:14.594Z","updated_at":"2025-09-17T23:19:04.936Z","avatar_url":"https://github.com/AlexSSD7.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cache\n\nCDN-like, middleware memory cache for Go applications with integrated shielding and Go 1.18 Generics.\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/AlexSSD7/cache\"\n)\n\ntype Person struct {\n\tName string\n\tAge  uint32\n}\n\nfunc fetchPerson(name string) (*Person, error) {\n\t// Heavy task here, like a DB call.\n\t// Shielding makes the fetch functions\n\t// mutually exclusive, to prevent\n\t// unncecessary duplicate calls\n\t// if there is no cache in memory.\n\n\t// For the purpose of this example though,\n\t// we will just return sample objects.\n\n\tswitch name {\n\tcase \"John\":\n\t\treturn \u0026Person{Name: \"John\", Age: 27}, nil\n\tcase \"Steve\":\n\t\treturn \u0026Person{Name: \"Steve\", Age: 32}, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unknown person with name '%v'\", name)\n\t}\n}\n\nfunc main() {\n\t// Create a new ShieldedCache instance with 5-second GC interval.\n\tcache := cache.NewShieldedCache[*Person](time.Second * 5)\n\n\tcacheCtx, cacheCtxCancel := context.WithCancel(context.Background())\n\tdefer cacheCtxCancel()\n\n\t// Worker is a separate goroutine running in background.\n\t// It is responsible for evicting old, expired objects.\n\tcache.StartWorker(cacheCtx)\n\n\t// If there is \"person_john\" object in cache, return it. Otherwise, execute fetchPerson(\"John\").\n\tret, hit, err := cache.Fetch(\"person_john\", time.Minute, func() (*Person, error) {\n\t\treturn fetchPerson(\"John\")\n\t})\n\n\tfmt.Printf(\"result: %+v, hit: %v, err: %v\\n\", ret.Data, hit, err)\n\t// result: \u0026{Name:John Age:27}, hit: false, err: \u003cnil\u003e\n\n\tret, hit, err = cache.Fetch(\"person_john\", time.Minute, func() (*Person, error) {\n\t\treturn fetchPerson(\"John\")\n\t})\n\n\tfmt.Printf(\"result: %+v, hit: %v, err: %v\\n\", ret.Data, hit, err)\n\t// result: \u0026{Name:John Age:27}, hit: true, err: \u003cnil\u003e\n\n\tret, hit, err = cache.Fetch(\"person_steve\", time.Minute, func() (*Person, error) {\n\t\treturn fetchPerson(\"Steve\")\n\t})\n\n\tfmt.Printf(\"result: %+v, hit: %v, err: %v\\n\", ret.Data, hit, err)\n\t// result: \u0026{Name:Steve Age:32}, hit: false, err: \u003cnil\u003e\n\n\tret, hit, err = cache.Fetch(\"person_alice\", time.Minute, func() (*Person, error) {\n\t\treturn fetchPerson(\"Alice\")\n\t})\n\n\tfmt.Printf(\"result: %+v, hit: %v, err: %v\\n\", ret, hit, err)\n\t// result: \u003cnil\u003e, hit: false, err: unknown person with name 'Alice'\n}\n```\n\n# License\n\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexssd7%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexssd7%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexssd7%2Fcache/lists"}