{"id":27093682,"url":"https://github.com/microup/vcache","last_synced_at":"2025-06-25T08:36:46.217Z","repository":{"id":65623690,"uuid":"595951953","full_name":"microup/vcache","owner":"microup","description":"\"vcache\" is a library that provides a concurrent-safe in-memory cache to store key-value pairs.","archived":false,"fork":false,"pushed_at":"2023-08-09T07:38:44.000Z","size":54,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T08:49:08.220Z","etag":null,"topics":["cache","cache-memory","eviction","go","golang","performance"],"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/microup.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":"2023-02-01T06:26:14.000Z","updated_at":"2023-02-12T19:33:40.000Z","dependencies_parsed_at":"2024-06-19T20:05:49.630Z","dependency_job_id":"1ef2e7f9-9919-4074-bb39-40bfdf3986b4","html_url":"https://github.com/microup/vcache","commit_stats":{"total_commits":51,"total_committers":3,"mean_commits":17.0,"dds":0.3137254901960784,"last_synced_commit":"0dd87922568ed8eaf49947cb80f93c260692dbbf"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/microup/vcache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microup%2Fvcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microup%2Fvcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microup%2Fvcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microup%2Fvcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/microup","download_url":"https://codeload.github.com/microup/vcache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microup%2Fvcache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261836752,"owners_count":23217286,"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","cache-memory","eviction","go","golang","performance"],"created_at":"2025-04-06T08:49:12.234Z","updated_at":"2025-06-25T08:36:46.196Z","avatar_url":"https://github.com/microup.png","language":"Go","readme":"[![test-and-linter](https://github.com/microup/vcache/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/microup/vcache/actions/workflows/main.yml)\n\n# What is VCache?\n\nThis is a Go package named \"vcache\" which implements a simple in-memory cache. The cache stores data as key-value pairs, where the keys are interface types and the values can be of any type. The cache is designed to be concurrent-safe with the use of a sync.Mutex.\n\n* This library \"vcache\" is a pure implementation and does not rely on any external dependencies. It is a self-contained implementation of an in-memory cache.\n\nThis project differs from the more well-known [go-cache](https://github.com/patrickmn/go-cache) in that it uses an intraface{} type key instead of a string key in its map structure, making cache management more flexible.\n\nBelow this document you will find the performance test results and comparison with other libraries (include with generics).\n\n## Where can this be used?\n\nThis library can be applied in software systems where caching is needed, such as web applications, databases, or other systems that require fast access to frequently used data. The library provides an in-memory cache that can be used to store frequently used data, such as API responses, database results, or other frequently accessed data. By using this library, the system can avoid unnecessary data processing and improve performance by quickly retrieving the data from the cache. The cache can also be automatically cleaned up based on the time specified in the durationTimeEvict variable, freeing up memory and ensuring that the cache remains relevant.\n\nFor example, where you can use this solution, to save indexes of unique keys from a database, for instance:\n\n```err := c.Add(1027, any_object)```\n\nor for example, if you need to save intermediate results for precise calculations, you can use a key type of:\n\n```err := c.Add(0.5274, any_object)\"```\n\n\n## It has the following main functions:\n\n- New() creates a new instance of the cache. The parameters are timeCheckNewTicker and timeRecordEvict, which represent the frequency of cache eviction check and the time an entry can stay in the cache before being evicted, respectively.\n- StartEvict() starts the cache eviction process in a separate go routine.\n- Add() adds a key-value pair to the cache.\n- Get() retrieves the value for a given key in the cache.\n- Evict() is responsible for removing stale cache entries that have been in the cache for a duration of time greater than durationTimeEvict.\n\n## Automatic Cache Eviction in \"vcache\" Library\n\nThe cache entries are automatically evicted every durationCheckTicker time intervals. The eviction process removes any cache entries that have not been accessed within the specified durationTimeEvict time period.\n\n## Here is an example of how to use the cache library\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"time\"\n\n    \"cache\"\n)\n\nfunc main() {\n    // Create a context for the cache eviction\n    ctx, cancel := context.WithCancel(context.Background())\n    defer cancel()\n\n    // Create a cache with a check ticker of 1 second and a record eviction of 5 seconds\n    c := cache.New(time.Second, 5*time.Second)\n\n    // Start eviction routine\n    c.StartEvict(ctx)\n\n    // Add key-value pairs to the cache\n    err := c.Add(\"key1\", \"value1\")\n    if err != nil {\n        fmt.Println(err)\n    }\n\n    err = c.Add(\"key2\", 2)\n    if err != nil {\n        fmt.Println(err)\n    }\n\n    // Get values from the cache\n    val, found := c.Get(\"key1\")\n    if found {\n        fmt.Printf(\"key1: %v\\n\", val)\n    } else {\n        fmt.Println(\"key1 not found\")\n    }\n\n    val, found = c.Get(\"key2\")\n    if found {\n        fmt.Printf(\"key2: %v\\n\", val)\n    } else {\n        fmt.Println(\"key2 not found\")\n    }\n\n    // Wait for 6 seconds for key1 to be evicted\n    time.Sleep(6 * time.Second)\n\n    // Try to get key1 after eviction\n    val, found = c.Get(\"key1\")\n    if found {\n        fmt.Printf(\"key1: %v\\n\", val)\n    } else {\n        fmt.Println(\"key1 not found\")\n    }\n}\n```\noutput:\n\n```bash\nkey1: value1\nkey2: 2\nkey1 not found\n```\n\nIn this example, the cache is created with a check ticker of 1 second and a record eviction of 5 seconds. The cache eviction routine is started using the StartEvict method and passing in the context created earlier. Key-value pairs are added to the cache using the Add method, and values are retrieved using the Get method. After waiting for 6 seconds, the Get method is used again to retrieve the value for the key \"key1\", but it is no longer found because it has been evicted from the cache.\n\n## Results benchmark on cpu: AMD Ryzen 5 5600X 6-Core Processor\n```\ngo test -bench=. -benchmem -benchtime=5s\n```\n```\nVCacheAdd-12             8966773               761.1 ns/op           213 B/op          7 allocs/op\nVCacheGet-12            39050804               184.0 ns/op             7 B/op          0 allocs/op\nVCacheDelete-12         36885169               192.3 ns/op             7 B/op          0 allocs/op\n```\nThe results of comparison with another librarys:\n\n- [go-cache](https://github.com/patrickmn/go-cache) are also presented:\n```\nGoCacheAdd-12            11722840               459.1 ns/op           203 B/op          5 allocs/op\nGoCacheGet-12            40415334               185.9 ns/op             7 B/op          0 allocs/op\nGoCacheDelete-12         35583009               218.9 ns/op            23 B/op          1 allocs/op\n```\n- [go-generics-cache](https://github.com/Code-Hex/go-generics-cache) are also presented:\n```\nGoCGenericsCacheAdd-12      7212994             745.5 ns/op           304 B/op          7 allocs/op\nGoCGenericsCacheGet-12      31680290            201.4 ns/op            23 B/op          1 allocs/op\nGoCGenericsCacheDelete-12   36424033            231.1 ns/op            23 B/op          1 allocs/op\n```\nYou can find the files for conducting the specified tests at the indicated path: [files_for_test](https://github.com/microup/vcache/tree/main/test_other_libs) \n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicroup%2Fvcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicroup%2Fvcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicroup%2Fvcache/lists"}