{"id":13451102,"url":"https://github.com/viant/scache","last_synced_at":"2025-08-03T13:31:03.130Z","repository":{"id":57533630,"uuid":"280753629","full_name":"viant/scache","owner":"viant","description":"Cache in go","archived":false,"fork":false,"pushed_at":"2024-10-14T22:27:45.000Z","size":62,"stargazers_count":5,"open_issues_count":3,"forks_count":1,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-10-28T18:14:34.365Z","etag":null,"topics":["cache","golang"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/viant.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2020-07-18T22:59:39.000Z","updated_at":"2024-10-14T22:27:49.000Z","dependencies_parsed_at":"2022-09-26T18:21:11.525Z","dependency_job_id":null,"html_url":"https://github.com/viant/scache","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viant%2Fscache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viant%2Fscache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viant%2Fscache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/viant%2Fscache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/viant","download_url":"https://codeload.github.com/viant/scache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228547486,"owners_count":17935089,"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","golang"],"created_at":"2024-07-31T07:00:48.037Z","updated_at":"2024-12-07T01:26:17.757Z","avatar_url":"https://github.com/viant.png","language":"Go","readme":"# Segmented Cache (scache) \n\n[![GoReportCard](https://goreportcard.com/badge/github.com/viant/scache)](https://goreportcard.com/report/github.com/viant/scache)\n[![GoDoc](https://godoc.org/github.com/viant/scache?status.svg)](https://godoc.org/github.com/viant/scache)\n\nThis library is compatible with Go 1.11+\n\nPlease refer to [`CHANGELOG.md`](CHANGELOG.md) if you encounter breaking changes.\n\n- [Motivation](#motivation)\n- [Introduction](#introduction)\n- [Contibution](#contributing-to-bqtail)\n- [License](#license)\n\n## Motivation\n\nThe goal of this cache is to provide low latency, zero allocation cache that is able to retain recently used entries that can work \nwith memory or memory mapped files. \n\n\n## Introduction\n\nSegmented Cache is operationally zero allocation as it preallocate memory during initialization.\nIt consists two segments each taking half of the allocated memory:\n - Primary:  read/write active\n - Secondary: read only active\n \nIn case of entry miss in the active segment, service attempt to locate entry in the secondary segment to rewrite it to active one. \n\nOnce active segment reaches limit of allocated memory, or optionally max entries, the secondary segment is promoted to the active, \nand the active is demoted to the secondary. \n\n\nThis approach double effective memory, but does not require housekeeping on LRU algorithm overhead.\nTo boost write performance, every Set operation append data to the data pool, and old address is invalidated.   \n\nThis cache has been inspired by [BigCache](https://github.com/allegro/bigcache) and uses map[uint64]uint32 for key hash to data address mapping.\nUsing non pointers in the map makes GC ommit map content. \n\nSee also: [How BigCache avoids expensive GC cycles and speeds up concurrent access in Go](https://dev.to/douglasmakey/how-bigcache-avoids-expensive-gc-cycles-and-speeds-up-concurrent-access-in-go-12bb)\n\n## Usage\n\n```go\n\nfunc CacheUsage() {\n\tvar cacheUsageType int\n\tvar cache *scache.Cache\n\tvar err error\n\tswitch cacheUsageType {\n\tcase InMemoryExample:\n\t\tcache, err = scache.New(\u0026scache.Config{SizeMb: 256})\n\t\t//or \n\t\tcache, err = scache.NewMemCache(256, 0, 0)\n\tcase MemoryMappedFileExample:\n\t\tcache, err = scache.New(\u0026scache.Config{SizeMb: 256, Location: \"/tmp/data.sch\"})\n\t\t//or \n\t\tcache, err = scache.NewMmapCache(\"/tmp/data.sch\", 256, 0, 0)\n\tcase InMemoryEntriesExample:\n\t\tcache, err = scache.New(\u0026scache.Config{MaxEntries: 5000000, EntrySize: 128})\n\t}\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\terr = cache.Set(\"keyX\", []byte(\"some value\"))\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tvalue, err := cache.Get(\"keyX\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tlog.Printf(\"value : %s\\n\", value)\n\terr = cache.Delete(\"keyX\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n}\n```\n\n### Benchmark \n\nBenchmark with 256 payload on OSX (2.4 GHz 8-Core Intel Core i9), SSD\n\n```bash\nBenchmarkService_MemGet-16             \t 4182728\t       303 ns/op\t       7 B/op\t       0 allocs/op\nBenchmarkService_MMapGet-16            \t 4107274\t       287 ns/op\t       7 B/op\t       0 allocs/op\nBenchmarkService_MMapGetParallel-16    \t28591705\t       43.0 ns/op\t       7 B/op\t       0 allocs/op\nBenchmarkService_MemGetParallel-16     \t33848409\t       39.0 ns/op\t       7 B/op\t       0 allocs/op\nBenchmarkService_MemSet-16             \t 5562770\t       243 ns/op\t       7 B/op\t       0 allocs/op\nBenchmarkService_MMapSet-16            \t 2569632\t       575 ns/op\t       7 B/op\t       0 allocs/op\nBenchmarkService_MemSetParallel-16     \t15095199\t       69.4 ns/op\t       7 B/op\t       0 allocs/op\nBenchmarkService_MMapSetParallel-16    \t 2290414\t       592 ns/op\t       7 B/op\t       0 allocs/op\n```\n\n\n## GoCover\n\n[![GoCover](https://gocover.io/github.com/viant/scache)](https://gocover.io/github.com/viant/scache)\n\n\n\u003ca name=\"License\"\u003e\u003c/a\u003e\n## License\n\nThe source code is made available under the terms of the Apache License, Version 2, as stated in the file `LICENSE`.\n\nIndividual files may be made available under their own specific license,\nall compatible with Apache License, Version 2. Please see individual files for details.\n\n\u003ca name=\"Credits-and-Acknowledgements\"\u003e\u003c/a\u003e\n\n## Contributing to SCache\n\nScache is an open source project and contributors are welcome!\n\nSee [TODO](TODO.md) list\n\n## Credits and Acknowledgements\n\n**Library Author:** Adrian Witas\n\n","funding_links":[],"categories":["Database"],"sub_categories":["Caches"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviant%2Fscache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fviant%2Fscache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fviant%2Fscache/lists"}