{"id":19637849,"url":"https://github.com/n-r-w/sfcache","last_synced_at":"2026-06-12T12:31:16.199Z","repository":{"id":224320269,"uuid":"762989819","full_name":"n-r-w/sfcache","owner":"n-r-w","description":"Golang Singleflight group with internal LRU cache","archived":false,"fork":false,"pushed_at":"2024-11-25T01:42:11.000Z","size":32,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-26T22:17:05.988Z","etag":null,"topics":["cache","go","singleflight"],"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/n-r-w.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}},"created_at":"2024-02-25T09:02:37.000Z","updated_at":"2024-04-18T09:59:35.000Z","dependencies_parsed_at":"2024-03-04T10:44:32.007Z","dependency_job_id":null,"html_url":"https://github.com/n-r-w/sfcache","commit_stats":null,"previous_names":["n-r-w/sfcache"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/n-r-w/sfcache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n-r-w%2Fsfcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n-r-w%2Fsfcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n-r-w%2Fsfcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n-r-w%2Fsfcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/n-r-w","download_url":"https://codeload.github.com/n-r-w/sfcache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n-r-w%2Fsfcache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34245217,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-12T02:00:06.859Z","response_time":109,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","singleflight"],"created_at":"2024-11-11T12:36:30.649Z","updated_at":"2026-06-12T12:31:16.150Z","avatar_url":"https://github.com/n-r-w.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Go Reference](https://pkg.go.dev/badge/github.com/n-r-w/sfcache.svg)](https://pkg.go.dev/github.com/n-r-w/sfcache)\n[![Go Coverage](https://github.com/n-r-w/sfcache/wiki/coverage.svg)](https://raw.githack.com/wiki/n-r-w/sfcache/coverage.html)\n![CI Status](https://github.com/n-r-w/sfcache/actions/workflows/go.yml/badge.svg)\n[![Stability](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)\n[![Go Report](https://goreportcard.com/badge/github.com/n-r-w/sfcache)](https://goreportcard.com/badge/github.com/n-r-w/sfcache)\n\n# sfcache\n\nSingleflight group with internal LRU cache\n\n## Usage\n\nSingleflight is a concurrency method to prevent duplicate work from being executed due to multiple calls for the same resource.\nThe internal LRU cache is used to store the result of the function call and bypass the singleflight call if the result is already in the cache.\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"log\"\n    \"time\"\n\n    \"github.com/n-r-w/sfcache\"\n)\n\nfunc main() {\n    const (\n    // The number of elements in the cache\n    cacheSize = 100\n    // TTL for the cache\n    cacheTTL = 10 * time.Minute\n    useCache = true\n    )\n\n    // evictCallback can be nil if you don't want to do anything when an item is evicted\n    cache := sfcache.New[int, string](cacheSize, evictCallback, cacheTTL)\n\n    // make come call\n    key := 1\n    val, shared, err := cache.Do(context.Background(), key, useCache, func(ctx context.Context) (string, error) {\n        return worker(ctx, key)\n    })\n    if err != nil {\n        panic(err)\n    }\n    log.Println(\"Value:\", val, \"Shared:\", shared)\n\n    // make some other call with the same key. This time it should be cached and worker should not be called.\n    val, shared, err = cache.Do(context.Background(), key, useCache, func(ctx context.Context) (string, error) {\n        return worker(ctx, key)\n    })\n    if err != nil {\n        panic(err)\n    }\n    log.Println(\"Value:\", val, \"Shared:\", shared)\n}\n\nfunc evictCallback(key int, value string) {\n    log.Println(\"Evicting\", key, value)\n}\n\nfunc worker(_ context.Context, key int) (string, error) {\n    log.Println(\"Doing some work for\", key)\n    return \"value\", nil\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn-r-w%2Fsfcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fn-r-w%2Fsfcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn-r-w%2Fsfcache/lists"}