{"id":17766657,"url":"https://github.com/floatdrop/lru","last_synced_at":"2025-03-15T13:30:54.451Z","repository":{"id":41196966,"uuid":"473908305","full_name":"floatdrop/lru","owner":"floatdrop","description":"Thread safe GoLang LRU cache","archived":false,"fork":false,"pushed_at":"2022-04-01T14:07:16.000Z","size":43,"stargazers_count":20,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-14T01:31:42.517Z","etag":null,"topics":["cache","go","golang","lru","lru-cache"],"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/floatdrop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-25T07:21:54.000Z","updated_at":"2024-02-04T10:42:19.000Z","dependencies_parsed_at":"2022-07-14T10:21:51.842Z","dependency_job_id":null,"html_url":"https://github.com/floatdrop/lru","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Flru","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Flru/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Flru/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Flru/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/floatdrop","download_url":"https://codeload.github.com/floatdrop/lru/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243735797,"owners_count":20339530,"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","go","golang","lru","lru-cache"],"created_at":"2024-10-26T20:33:59.973Z","updated_at":"2025-03-15T13:30:54.445Z","avatar_url":"https://github.com/floatdrop.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lru\n[![Go Reference](https://pkg.go.dev/badge/github.com/floatdrop/lru.svg)](https://pkg.go.dev/github.com/floatdrop/lru)\n[![CI](https://github.com/floatdrop/lru/actions/workflows/ci.yml/badge.svg)](https://github.com/floatdrop/lru/actions/workflows/ci.yml)\n![Coverage](https://img.shields.io/badge/Coverage-100.0%25-brightgreen)\n[![Go Report Card](https://goreportcard.com/badge/github.com/floatdrop/lru)](https://goreportcard.com/report/github.com/floatdrop/lru)\n\nThread safe GoLang LRU cache.\n\n## Example\n\n```go\nimport (\n\t\"fmt\"\n\n\t\"github.com/floatdrop/lru\"\n)\n\nfunc main() {\n\tcache := lru.New[string, int](256)\n\n\tcache.Set(\"Hello\", 5)\n\n\tif e := cache.Get(\"Hello\"); e != nil {\n\t\tfmt.Println(*e)\n\t\t// Output: 5\n\t}\n}\n```\n\n## TTL\n\nYou can wrap values into `Expiring[T any]` struct to release memory on timer (or manually in `Valid` method).\n\n\u003cdetails\u003e\n    \u003csummary\u003eExample implementation\u003c/summary\u003e\n\n```go\nimport (\n    \"fmt\"\n    \"time\"\n\n    \"github.com/floatdrop/lru\"\n)\n\ntype Expiring[T any] struct {\n    value *T\n}\n\nfunc (E *Expiring[T]) Valid() *T {\n    if E == nil {\n        return nil\n    }\n\n    return E.value\n}\n\nfunc WithTTL[T any](value T, ttl time.Duration) Expiring[T] {\n    e := Expiring[T]{\n        value: \u0026value,\n    }\n\n    time.AfterFunc(ttl, func() {\n        e.value = nil // Release memory\n    })\n\n    return e\n}\n\nfunc main() {\n    l := lru.New[string, Expiring[string]](256)\n\n    l.Set(\"Hello\", WithTTL(\"Bye\", time.Hour))\n\n    if e := l.Get(\"Hello\").Valid(); e != nil {\n        fmt.Println(*e)\n    }\n}\n```\n\n**Note:** Althou this short implementation frees memory after ttl duration, it will not erase entry for key in cache. It can be a problem, if you do not check nillnes after getting element from cache and call `Set` afterwards.\n\u003c/details\u003e\n\n## Benchmarks\n\n```\nfloatdrop/lru:\n    BenchmarkLRU_Rand-8   \t 8802915\t       131.7 ns/op\t      24 B/op\t       1 allocs/op\n    BenchmarkLRU_Freq-8   \t 9392769\t       127.8 ns/op\t      24 B/op\t       1 allocs/op\n\nhashicorp/golang-lru:\n    BenchmarkLRU_Rand-8   \t 5992782\t       195.8 ns/op\t      76 B/op\t       3 allocs/op\n    BenchmarkLRU_Freq-8   \t 6355358\t       186.1 ns/op\t      71 B/op\t       3 allocs/op\n\njellydator/ttlcache:\n    BenchmarkLRU_Rand-8   \t 4447654\t       253.5 ns/op\t     144 B/op\t       2 allocs/op\n    BenchmarkLRU_Freq-8   \t 4837938\t       240.9 ns/op\t     137 B/op\t       2 allocs/op\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffloatdrop%2Flru","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffloatdrop%2Flru","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffloatdrop%2Flru/lists"}