{"id":32948798,"url":"https://github.com/szyhf/go-gcache","last_synced_at":"2026-01-14T14:41:58.610Z","repository":{"id":61839426,"uuid":"553621281","full_name":"szyhf/go-gcache","owner":"szyhf","description":"An in-memory generic cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC.","archived":false,"fork":true,"pushed_at":"2022-12-28T09:08:43.000Z","size":119,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"v2","last_synced_at":"2024-06-20T06:28:40.965Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"bluele/gcache","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/szyhf.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-10-18T13:53:43.000Z","updated_at":"2024-03-15T09:14:29.000Z","dependencies_parsed_at":"2023-01-31T06:00:19.676Z","dependency_job_id":null,"html_url":"https://github.com/szyhf/go-gcache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/szyhf/go-gcache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szyhf%2Fgo-gcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szyhf%2Fgo-gcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szyhf%2Fgo-gcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szyhf%2Fgo-gcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/szyhf","download_url":"https://codeload.github.com/szyhf/go-gcache/tar.gz/refs/heads/v2","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szyhf%2Fgo-gcache/sbom","scorecard":{"id":864466,"data":{"date":"2025-08-11","repo":{"name":"github.com/szyhf/go-gcache","commit":"08bafd4289e468183f1bcb0698b407025803a982"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.4,"checks":[{"name":"Maintained","score":0,"reason":"1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/szyhf/go-gcache/test.yml/v2?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/szyhf/go-gcache/test.yml/v2?enable=pin","Info:   0 out of   2 GitHub-owned GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Code-Review","score":0,"reason":"Found 0/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/test.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'v2'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-24T02:17:56.729Z","repository_id":61839426,"created_at":"2025-08-24T02:17:56.730Z","updated_at":"2025-08-24T02:17:56.730Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28423971,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T13:30:50.153Z","status":"ssl_error","status_checked_at":"2026-01-14T13:29:08.907Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5: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":[],"created_at":"2025-11-12T20:00:35.661Z","updated_at":"2026-01-14T14:41:58.605Z","avatar_url":"https://github.com/szyhf.png","language":"Go","funding_links":[],"categories":["Database","数据库","Data Integration Frameworks"],"sub_categories":["Caches","缓存"],"readme":"# GCache\n\n![Test](https://github.com/szyhf/go-gcache/workflows/Test/badge.svg)\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/szyhf/go-gcache/v2)](https://pkg.go.dev/github.com/szyhf/go-gcache/v2?tab=doc)\n\nCache library for golang. It supports expirable Cache, LFU, LRU and ARC.\n\nIt's the generic version of [bluele/gcache](https://github.com/bluele/gcache).\n\n## Features\n\n* Supports generic.\n\n* Supports expirable Cache, LFU, LRU and ARC.\n\n* Goroutine safe.\n\n* Supports event handlers which evict, purge, and add entry. (Optional)\n\n* Automatically load cache if it doesn't exists. (Optional)\n\n## Install\n\n```\n$ go get github.com/szyhf/go-gcache/v2\n```\n\n## Example\n\n### Manually set a key-value pair.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/szyhf/go-gcache/v2\"\n)\n\nfunc main() {\n\tgc := gcache.New[string, string](20).\n\t\tLRU().\n\t\tBuild()\n\tgc.Set(\"key\", \"ok\")\n\tvalue, err := gc.Get(\"key\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(\"Get:\", value)\n}\n```\n\n```\nGet: ok\n```\n\n### Manually set a key-value pair, with an expiration time.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/szyhf/go-gcache/v2\"\n)\n\nfunc main() {\n\tgc := gcache.New[string, string](20).\n\t\tLRU().\n\t\tBuild()\n\tgc.SetWithExpire(\"key\", \"ok\", time.Second*10)\n\tvalue, _ := gc.Get(\"key\")\n\tfmt.Println(\"Get:\", value)\n\n\t// Wait for value to expire\n\ttime.Sleep(time.Second * 10)\n\n\tvalue, err := gc.Get(\"key\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(\"Get:\", value)\n}\n```\n\n```\nGet: ok\n// 10 seconds later, new attempt:\npanic: ErrKeyNotFound\n```\n\n\n### Automatically load value\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\tgcache \"github.com/szyhf/go-gcache/v2\"\n)\n\nfunc main() {\n\tgc := gcache.New[string, string](20).\n\t\tLRU().\n\t\tLoaderFunc(func(key string) (string, error) {\n\t\t\treturn \"ok\", nil\n\t\t}).\n\t\tBuild()\n\tvalue, err := gc.Get(\"key\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(\"Get:\", value)\n}\n```\n\n```\nGet: ok\n```\n\n### Automatically load value with expiration\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"time\"\n\n  \"github.com/szyhf/go-gcache/v2\"\n)\n\nfunc main() {\n  var evictCounter, loaderCounter, purgeCounter int\n  gc := gcache.New(20).\n    LRU().\n    LoaderExpireFunc(func(key any) (any, *time.Duration, error) {\n      loaderCounter++\n      expire := 1 * time.Second\n      return \"ok\", \u0026expire, nil\n    }).\n    EvictedFunc(func(key, value any) {\n      evictCounter++\n      fmt.Println(\"evicted key:\", key)\n    }).\n    PurgeVisitorFunc(func(key, value any) {\n      purgeCounter++\n      fmt.Println(\"purged key:\", key)\n    }).\n    Build()\n  value, err := gc.Get(\"key\")\n  if err != nil {\n    panic(err)\n  }\n  fmt.Println(\"Get:\", value)\n  time.Sleep(1 * time.Second)\n  value, err = gc.Get(\"key\")\n  if err != nil {\n    panic(err)\n  }\n  fmt.Println(\"Get:\", value)\n  gc.Purge()\n  if loaderCounter != evictCounter+purgeCounter {\n    panic(\"bad\")\n  }\n}\n```\n\n```\nGet: ok\nevicted key: key\nGet: ok\npurged key: key\n```\n\n\n## Cache Algorithm\n\n  * Least-Frequently Used (LFU)\n\n  Discards the least frequently used items first.\n\n  ```go\n  func main() {\n  \t// size: 10\n  \tgc := gcache.New[string, string](10).\n  \t\tLFU().\n  \t\tBuild()\n  \tgc.Set(\"key\", \"value\")\n  }\n  ```\n\n  * Least Recently Used (LRU)\n\n  Discards the least recently used items first.\n\n  ```go\n  func main() {\n    // size: 10\n    gc := gcache.New[string, string](10).\n      LRU().\n      Build()\n    gc.Set(\"key\", \"value\")\n  }\n  ```\n\n  * Adaptive Replacement Cache (ARC)\n\n  Constantly balances between LRU and LFU, to improve the combined result.\n\n  detail: http://en.wikipedia.org/wiki/Adaptive_replacement_cache\n\n  ```go\n  func main() {\n    // size: 10\n    gc := gcache.New[string, string](10).\n      ARC().\n      Build()\n    gc.Set(\"key\", \"value\")\n  }\n  ```\n\n  * SimpleCache (Default)\n\n  SimpleCache has no clear priority for evict cache. It depends on key-value map order.\n\n  ```go\n  func main() {\n      // size: 10\n      gc := gcache.New[string, string](10).Build()\n      gc.Set(\"key\", \"value\")\n      v, err := gc.Get(\"key\")\n      if err != nil {\n      \tpanic(err)\n      }\n      // output: \"value\"\n      fmt.Println(v)\n  }\n  ```\n\n## Loading Cache\n\nIf specified `LoaderFunc`, values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated.\n\n```go\nfunc main() {\n\tgc := gcache.New[string, string](10).\n\t\tLRU().\n\t\tLoaderFunc(func(key string) (string, error) {\n\t\t\treturn \"value\", nil\n\t\t}).\n\t\tBuild()\n\tv, _ := gc.Get(\"key\")\n\t// output: \"value\"\n\tfmt.Println(v)\n}\n```\n\nGCache coordinates cache fills such that only one load in one process of an entire replicated set of processes populates the cache, then multiplexes the loaded value to all callers.\n\n## Expirable cache\n\n```go\nfunc main() {\n\t// LRU cache, size: 10, expiration: after a hour\n\tgc := gcache.New[int, int](10).\n\t\tLRU().\n\t\tExpiration(time.Hour).\n\t\tBuild()\n\tfor i := 0; i \u003c 3; i++ {\n\t\tgc.Set(i, i*i)\n\t}\n}\n```\n\n## Event handlers\n\n### Evicted handler\n\nEvent handler for evict the entry.\n\n```go\nfunc main() {\n\tgc := gcache.New[int, int](2).\n\t\tEvictedFunc(func(key, value int) {\n\t\t\tfmt.Println(\"evicted key:\", key)\n\t\t}).\n\t\tBuild()\n\tfor i := 0; i \u003c 3; i++ {\n\t\tgc.Set(i, i*i)\n\t}\n}\n```\n\n```\nevicted key: 0\n```\n\n### Added handler\n\nEvent handler for add the entry.\n\n```go\nfunc main() {\n\tgc := gcache.New[int, int](2).\n\t\tAddedFunc(func(key, value int) {\n\t\t\tfmt.Println(\"added key:\", key)\n\t\t}).\n\t\tBuild()\n\tfor i := 0; i \u003c 3; i++ {\n\t\tgc.Set(i, i*i)\n\t}\n}\n```\n\n```\nadded key: 0\nadded key: 1\nadded key: 2\n```\n\n# Origin Author\n\n**Jun Kimura**\n\n* \u003chttp://github.com/bluele\u003e\n* \u003cjunkxdev@gmail.com\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszyhf%2Fgo-gcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fszyhf%2Fgo-gcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszyhf%2Fgo-gcache/lists"}