{"id":17086569,"url":"https://github.com/arl/fastlfu","last_synced_at":"2025-06-12T01:05:21.947Z","repository":{"id":42382630,"uuid":"424528365","full_name":"arl/fastlfu","owner":"arl","description":"LFU cache with O(1) runtime complexity for all operations: insertion, access and deletion (eviction). In Go.","archived":false,"fork":false,"pushed_at":"2022-04-08T06:03:38.000Z","size":270,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-12T01:05:06.530Z","etag":null,"topics":["cache","go","golang","lfu","lfu-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/arl.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-11-04T08:53:17.000Z","updated_at":"2022-05-18T05:26:29.000Z","dependencies_parsed_at":"2022-09-12T22:00:42.514Z","dependency_job_id":null,"html_url":"https://github.com/arl/fastlfu","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/arl/fastlfu","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arl%2Ffastlfu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arl%2Ffastlfu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arl%2Ffastlfu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arl%2Ffastlfu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arl","download_url":"https://codeload.github.com/arl/fastlfu/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arl%2Ffastlfu/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259374758,"owners_count":22847861,"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","lfu","lfu-cache"],"created_at":"2024-10-14T13:28:58.190Z","updated_at":"2025-06-12T01:05:21.921Z","avatar_url":"https://github.com/arl.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fastLFU cache\n\n\n:warning: `fastlfu` is still under developement, the API might not be stable yet, you should not use in production :exclamation:\n\nThis is a Go 1.18 implementation of the **fastLFU** (Least Frequently Used)\ncache eviction scheme, described in a 2021 [paper](https://arxiv.org/pdf/2110.11602v1.pdf)\nby Dhruv Matani, Ketan Shah and Anirban Mitra called _An O(1) algorithm for implementing the LFU cache eviction scheme_.\n\nHere's an extract from the paper introduction: \n\n\u003e The LFU algorithm has behaviour desirable by many real world workloads. However, in many places, the LRU algorithm is is preferred over the LFU algorithm because of its lower run time complexity of O(1) versus O(logn). We present here an LFU cache eviction algorithm that has a runtime complexity of O(1) for all of its operations, which include insertion, access and deletion(eviction).\n\n## Usage\n\nInstall the `fastlfu` module:\n\n```\ngo get github.com/arl/fastlfu@latest\n```\n\nLet's create a `Cache` object where keys are `uint64` and values are `string`s:\n\n```go\n    c := fastlfu.New[uint64, string]()\n```\n\nAdd a bunch of key-value pairs:\n\n```go\n    c.Insert(0, \"foo\")\n    c.Insert(1, \"bar\")\n    c.Insert(2, \"baz\")\n```\n\nEach time we fetch a value associated from a key, we increment the key\nfrequency:\n\n```go\n    v1, ok1 := c.Fetch(0)\n    // returns \"foo\", true\n\n    v2, ok1 := c.Fetch(1)\n    // returns \"bar\", true\n```\n\nIf we call `Fetch` with a key that is not present, we get the zero-value of the\nvalue type, and false:\n\n```go\n    v, ok := c.Fetch(12345678)\n    // returns \"\", false\n```\n\nAfter these calls, the `(0, \"foo\")` and `(1, \"bar\")` key-value pairs have been\nmore frequently used than `(2, \"baz\").`\n\nSo if we require the cache to evict the least frequently used value, it's going\nto be `\"baz\"`.  `Evict` also returns a boolean indicating if any eviction has\nbeen performed. \n\n```go\n    v, ok := c.Evict(0)\n    // returns \"baz\", true\n```\n\nNOTE: `Evict` is guaranteed to succeed unless the `Cache` is empty.\n\nIf we call `Evict` again, we can't predict they key-value pair that is going to\nbe evicted because the remaining pairs have the same frequency.\n\n```go\n    v, ok := c.Evict(0)\n    // returns either (\"foo\", true) or (\"bar\", true)\n```\n\n## Performance\n\nTODO\n\n## [MIT LICENSE](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farl%2Ffastlfu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farl%2Ffastlfu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farl%2Ffastlfu/lists"}