{"id":13676869,"url":"https://github.com/sima-land/go-local-cache","last_synced_at":"2025-04-22T23:23:03.017Z","repository":{"id":57496528,"uuid":"71210536","full_name":"sima-land/go-local-cache","owner":"sima-land","description":"Simple cache keeps data in memory and prevents dog pile effect","archived":false,"fork":false,"pushed_at":"2017-07-10T11:14:22.000Z","size":11,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-17T14:38:37.365Z","etag":null,"topics":["cache","dogpile","golang","lru","lru-cache","singleflight"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sima-land.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":"2016-10-18T05:00:17.000Z","updated_at":"2022-03-03T14:41:30.000Z","dependencies_parsed_at":"2022-08-28T15:13:32.533Z","dependency_job_id":null,"html_url":"https://github.com/sima-land/go-local-cache","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sima-land%2Fgo-local-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sima-land%2Fgo-local-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sima-land%2Fgo-local-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sima-land%2Fgo-local-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sima-land","download_url":"https://codeload.github.com/sima-land/go-local-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250338618,"owners_count":21414216,"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","dogpile","golang","lru","lru-cache","singleflight"],"created_at":"2024-08-02T13:00:34.103Z","updated_at":"2025-04-22T23:23:02.991Z","avatar_url":"https://github.com/sima-land.png","language":"Go","funding_links":[],"categories":["Data structures"],"sub_categories":[],"readme":"# Golang local cache\n\nImplementation of simple local cache.\n\n[![Build Status](https://travis-ci.org/sima-land/go-local-cache.svg?branch=master)](https://travis-ci.org/sima-land/go-local-cache)\n[![Go Report Card](https://goreportcard.com/badge/github.com/sima-land/go-local-cache)](https://goreportcard.com/report/github.com/sima-land/go-local-cache)\n\nFeatures \u0026 todos:\n- [x] prevents \"dogpile effect\"\n- [x] check data expiration time\n- [x] lru policy\n- [ ] cache hit and enviction stats\n\nCache limitations:\n\n- keys are always int\n- get function returns multiple values \n- set function sets multiple keys at once\n- to prevent dog pile effect queueKey should be manually set\n\n## How to use\n\nCreate cache instance\n\n```golang\nimport \"github.com/sima-land/go-local-cache\"\nc := cache.New()\n```\n\nCreate getter function. Its preferred way to fill the cache because in case of \"dog pile\" effect getter will be \ncalled once. \n\n```golang\ngetter := func(keys Keys) (Result, error) {\n\tr := Result{}\n\tfor _, k := range keys {\n\t\t// haavy call to database\n\t\tr[k] = db.GetByID(k)\t\t\n\t}\n\treturn r, nil\n}\n```\n\nObtain data from cache. If there is no data in the cache getter will be called.\n\nNote:\n1. Next call is concurrently safe\n2. If too many go-routines in our application are trying to call this getter with same queue key (user-1) \n   then only first call will actually be done\n\n```golang\nresult, err := c.Get(int[]{1}, getter, \"user-1\")\n```\n\n## QueueKey param explain\n\nYou can simple use entity name and id as queueKey param of Get function. Eg:\n\n```golang\nresult, err := c.Get(int[]{1}, userGetter, \"user-1\")\nresult, err := c.Get(int[]{1}, productGetter, \"product-1\")\n```\n\nOr if you need to get a page of product's block whole page. If keys 1,2 are in cache but other keys are not then:\n\n1. Keys 1 and 2 will be get from cache\n2. Getter will be called for keys 3,4,5 and 6\n3. If another getter with same queue key \"product-1\" was called before and first one will be used instead\n\n```golang\nresult, err := c.Get(int[]{1,2,3,4,5,6}, userGetter, \"product-page-1\")\n```\n\n## Options\n\nCache has a variety of configuration options:\n\n``` golang\nc := cache.New(cache.Options{\n\t\tMaxEntries: 1000000, // Specify max number items in cache\n\t\tTTL: time.Hour, // Time to live for cache entries\n\t})\n```\n\n## Test\n\nMacBook 2,7 GHz Intel Core i5\n\n```\nBenchmarkCache_Get-4      500000              2274 ns/op\nBenchmarkCache_Get-4      500000              2335 ns/op\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsima-land%2Fgo-local-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsima-land%2Fgo-local-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsima-land%2Fgo-local-cache/lists"}