{"id":18802950,"url":"https://github.com/hadv/ringcache","last_synced_at":"2026-01-05T10:30:17.414Z","repository":{"id":242061804,"uuid":"808585276","full_name":"hadv/ringcache","owner":"hadv","description":"a non thread-safe circular cache ","archived":false,"fork":false,"pushed_at":"2024-06-21T05:42:48.000Z","size":42,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-29T20:20:12.483Z","etag":null,"topics":["cache","circular-buffer","go","golang","ring-buffer"],"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/hadv.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-31T11:26:12.000Z","updated_at":"2024-06-21T05:42:51.000Z","dependencies_parsed_at":"2024-06-10T02:38:48.311Z","dependency_job_id":"6cb44c15-9902-4761-b746-10393b04c352","html_url":"https://github.com/hadv/ringcache","commit_stats":null,"previous_names":["hadv/ringcache"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadv%2Fringcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadv%2Fringcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadv%2Fringcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hadv%2Fringcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hadv","download_url":"https://codeload.github.com/hadv/ringcache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239735262,"owners_count":19688262,"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","circular-buffer","go","golang","ring-buffer"],"created_at":"2024-11-07T22:32:02.938Z","updated_at":"2026-01-05T10:30:17.359Z","avatar_url":"https://github.com/hadv.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Go](https://github.com/hadv/ringcache/actions/workflows/go.yml/badge.svg)](https://github.com/hadv/ringcache/actions/workflows/go.yml)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Go Report Card](https://goreportcard.com/badge/github.com/hadv/ringcache)](https://goreportcard.com/report/github.com/hadv/ringcache)\n[![codecov](https://codecov.io/gh/hadv/ringcache/graph/badge.svg?token=0I6U0894IC)](https://codecov.io/gh/hadv/ringcache)\n\nA non-thread-safe ring cache, also known as a circular buffer, is a fixed-size data structure designed to efficiently handle a continuous stream of data by overwriting the oldest entries when new data is added and the buffer is full. This implementation ensures constant memory usage and provides O(1) time complexity for both adding and retrieving elements. However, it is not safe for concurrent use and should be used in single-threaded environments where predictable performance and efficient memory management are required.\n\nHere's a complete usage example:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"ringcache\"\n)\n\n// evictionHandler is a callback function that gets called when an item is evicted from the cache.\nfunc evictionHandler(key interface{}, value interface{}) {\n\tfmt.Printf(\"Evicted key: %v, value: %v\\n\", key, value)\n}\n\nfunc main() {\n\t// Create a new ring cache with a maximum size of 3 and an eviction callback\n\tcache, err := ringcache.NewWithEvict(3, evictionHandler)\n\tif err != nil {\n\t\tfmt.Println(\"Error creating cache:\", err)\n\t\treturn\n\t}\n\n\t// Add some items to the cache\n\tcache.Add(\"key1\", \"value1\")\n\tcache.Add(\"key2\", \"value2\")\n\tcache.Add(\"key3\", \"value3\")\n\n\t// Print the current cache contents\n\tfmt.Println(\"Cache after adding 3 items:\")\n\tfor _, key := range []string{\"key1\", \"key2\", \"key3\"} {\n\t\tif value, ok := cache.Get(key); ok {\n\t\t\tfmt.Printf(\"%s: %v\\n\", key, value)\n\t\t} else {\n\t\t\tfmt.Printf(\"%s not found\\n\", key)\n\t\t}\n\t}\n\n\t// Add another item, causing the first item to be evicted\n\tcache.Add(\"key4\", \"value4\")\n\n\t// Print the current cache contents again\n\tfmt.Println(\"\\nCache after adding 4th item (key1 should be evicted):\")\n\tfor _, key := range []string{\"key1\", \"key2\", \"key3\", \"key4\"} {\n\t\tif value, ok := cache.Get(key); ok {\n\t\t\tfmt.Printf(\"%s: %v\\n\", key, value)\n\t\t} else {\n\t\t\tfmt.Printf(\"%s not found\\n\", key)\n\t\t}\n\t}\n\n\t// Remove an item from the cache\n\tcache.Remove(\"key3\")\n\tfmt.Println(\"\\nCache after removing key3:\")\n\tfor _, key := range []string{\"key2\", \"key3\", \"key4\"} {\n\t\tif value, ok := cache.Get(key); ok {\n\t\t\tfmt.Printf(\"%s: %v\\n\", key, value)\n\t\t} else {\n\t\t\tfmt.Printf(\"%s not found\\n\", key)\n\t\t}\n\t}\n\n\t// Clear the cache\n\tcache.Purge()\n\tfmt.Println(\"\\nCache after purge:\")\n\tfor _, key := range []string{\"key2\", \"key3\", \"key4\"} {\n\t\tif value, ok := cache.Get(key); ok {\n\t\t\tfmt.Printf(\"%s: %v\\n\", key, value)\n\t\t} else {\n\t\t\tfmt.Printf(\"%s not found\\n\", key)\n\t\t}\n\t}\n}\n```\n\nOutput:\n```\nCache after adding 3 items:\nkey1: value1\nkey2: value2\nkey3: value3\n\nEvicted key: key1, value: value1\nCache after adding 4th item (key1 should be evicted):\nkey1 not found\nkey2: value2\nkey3: value3\nkey4: value4\n\nEvicted key: key3, value: value3\nCache after removing key3:\nkey2: value2\nkey3 not found\nkey4: value4\n\nEvicted key: key2, value: value2\nEvicted key: key4, value: value4\nCache after purge:\nkey2 not found\nkey3 not found\nkey4 not found\n```\n\nLicense\n-------\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\nContributing\n------------\nContributions are welcome! Please feel free to submit a pull request or open an issue if you encounter any problems or have any suggestions for improvements.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhadv%2Fringcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhadv%2Fringcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhadv%2Fringcache/lists"}