{"id":13713379,"url":"https://github.com/Code-Hex/go-generics-cache","last_synced_at":"2025-05-06T23:31:56.511Z","repository":{"id":42451738,"uuid":"428744086","full_name":"Code-Hex/go-generics-cache","owner":"Code-Hex","description":"A key:value store/cache library written in Go generics. LRU, LFU, FIFO, MRU, Clock support.","archived":false,"fork":false,"pushed_at":"2025-01-26T02:59:57.000Z","size":153,"stargazers_count":474,"open_issues_count":8,"forks_count":42,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-29T13:16:07.017Z","etag":null,"topics":["cache","clock","fifo","generics","go","golang","lfu","lru","mru","simple"],"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/Code-Hex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":"Code-Hex"}},"created_at":"2021-11-16T17:04:20.000Z","updated_at":"2025-04-26T05:08:13.000Z","dependencies_parsed_at":"2024-11-14T00:30:43.441Z","dependency_job_id":"dc60c4a2-d36e-41e1-a441-a6fb562c6da3","html_url":"https://github.com/Code-Hex/go-generics-cache","commit_stats":{"total_commits":90,"total_committers":9,"mean_commits":10.0,"dds":0.3555555555555555,"last_synced_commit":"2601bb0d3c67fc00cdbd5763cc2361d2a363f0ee"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgo-generics-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgo-generics-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgo-generics-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgo-generics-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Code-Hex","download_url":"https://codeload.github.com/Code-Hex/go-generics-cache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252787531,"owners_count":21804278,"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","clock","fifo","generics","go","golang","lfu","lru","mru","simple"],"created_at":"2024-08-02T23:01:34.289Z","updated_at":"2025-05-06T23:31:53.251Z","avatar_url":"https://github.com/Code-Hex.png","language":"Go","readme":"# go-generics-cache\n\n[![.github/workflows/test.yml](https://github.com/Code-Hex/go-generics-cache/actions/workflows/test.yml/badge.svg)](https://github.com/Code-Hex/go-generics-cache/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/Code-Hex/go-generics-cache/branch/main/graph/badge.svg?token=Wm7UEwgiZu)](https://codecov.io/gh/Code-Hex/go-generics-cache) [![Go Reference](https://pkg.go.dev/badge/github.com/Code-Hex/go-generics-cache.svg)](https://pkg.go.dev/github.com/Code-Hex/go-generics-cache)\n\ngo-generics-cache is an in-memory key:value store/cache that is suitable for applications running on a single machine. This in-memory cache uses [Go Generics](https://go.dev/blog/generics-proposal) which is introduced in 1.18.\n\n- a thread-safe\n- implemented with [Go Generics](https://go.dev/blog/generics-proposal)\n- TTL supported (with expiration times)\n- Simple cache is like `map[string]interface{}`\n  - See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/simple/example_test.go)\n- Cache replacement policies\n  - **Least recently used (LRU)**\n    - Discards the least recently used items first.\n    - See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/lru/example_test.go)\n  - **Least-frequently used (LFU)**\n    - Counts how often an item is needed. Those that are used least often are discarded first.\n    - [An O(1) algorithm for implementing the LFU cache eviction scheme](http://dhruvbird.com/lfu.pdf)\n    - See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/lfu/example_test.go)\n  - **First in first out (FIFO)**\n    - Using this algorithm the cache behaves in the same way as a [FIFO queue](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)).\n    - The cache evicts the blocks in the order they were added, without any regard to how often or how many times they were accessed before.\n\t- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/fifo/example_test.go)\n  - **Most recently used (MRU)**\n    - In contrast to Least Recently Used (LRU), MRU discards the most recently used items first.\n\t- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/mru/example_test.go)\n  - **Clock**\n    - Clock is a more efficient version of FIFO than Second-chance cache algorithm.\n\t- See [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/policy/clock/example_test.go)\n\n## Requirements\n\nGo 1.18 or later.\n\n## Install\n\n    $ go get github.com/Code-Hex/go-generics-cache\n\n## Usage\n\nSee also [examples](https://github.com/Code-Hex/go-generics-cache/blob/main/example_test.go) or [go playground](https://go.dev/play/p/kDs-6wpRAcX)\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"time\"\n\n\tcache \"github.com/Code-Hex/go-generics-cache\"\n)\n\nfunc main() {\n\tctx, cancel := context.WithCancel(context.Background())\n\tdefer cancel()\n\n\t// use simple cache algorithm without options.\n\tc := cache.NewContext[string, int](ctx)\n\tc.Set(\"a\", 1)\n\tgota, aok := c.Get(\"a\")\n\tgotb, bok := c.Get(\"b\")\n\tfmt.Println(gota, aok) // 1 true\n\tfmt.Println(gotb, bok) // 0 false\n\n\t// Create a cache for Number constraint. key as string, value as int.\n\tnc := cache.NewNumber[string, int]()\n\tnc.Set(\"age\", 26, cache.WithExpiration(time.Hour))\n\n\tincremented := nc.Increment(\"age\", 1)\n\tfmt.Println(incremented) // 27\n\n\tdecremented := nc.Decrement(\"age\", 1)\n\tfmt.Println(decremented) // 26\n}\n```\n\n## Articles\n\n- English: [Some tips and bothers for Go 1.18 Generics](https://dev.to/codehex/some-tips-and-bothers-for-go-118-generics-lc7)\n- Japanese: [Go 1.18 の Generics を使ったキャッシュライブラリを作った時に見つけた tips と微妙な点](https://zenn.dev/codehex/articles/3e6935ee6d853e)\n","funding_links":["https://github.com/sponsors/Code-Hex"],"categories":["Go","Repositories"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCode-Hex%2Fgo-generics-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCode-Hex%2Fgo-generics-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCode-Hex%2Fgo-generics-cache/lists"}