{"id":18914679,"url":"https://github.com/helium/cream","last_synced_at":"2025-04-15T08:31:31.189Z","repository":{"id":44307116,"uuid":"512014305","full_name":"helium/cream","owner":"helium","description":"An Erlang cache backed by the performant Moka library","archived":false,"fork":false,"pushed_at":"2022-08-05T22:24:09.000Z","size":36,"stargazers_count":5,"open_issues_count":2,"forks_count":2,"subscribers_count":24,"default_branch":"main","last_synced_at":"2025-03-28T18:55:14.986Z","etag":null,"topics":["cache","erlang","nif"],"latest_commit_sha":null,"homepage":"","language":"Erlang","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/helium.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":"2022-07-08T20:07:10.000Z","updated_at":"2025-03-13T04:41:40.000Z","dependencies_parsed_at":"2022-09-23T01:50:59.343Z","dependency_job_id":null,"html_url":"https://github.com/helium/cream","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helium%2Fcream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helium%2Fcream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helium%2Fcream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helium%2Fcream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/helium","download_url":"https://codeload.github.com/helium/cream/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249035546,"owners_count":21202109,"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","erlang","nif"],"created_at":"2024-11-08T10:12:26.047Z","updated_at":"2025-04-15T08:31:30.923Z","avatar_url":"https://github.com/helium.png","language":"Erlang","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![.github/workflows/ci.yml](https://github.com/helium/cream/actions/workflows/ci.yml/badge.svg)](https://github.com/helium/cream/actions/workflows/ci.yml)\n\nCache Rules Everything Around Me\n================================\n\n`cream` is a an Erlang caching module backed by the performant [Moka] library.\n\n## Dependencies\n\nBecause this module is based on Rust NIFs, you need to have a Rust toolchain [installed].\n\n[Moka]: https://github.com/moka-rs/moka\n[installed]: https://rustup.rs\n\n## Cache options\n\nCreating a cache is as simple as calling `cream:new/1` with the max capacity of the cache. In addition to capacity, you can create a cache by calling `cream:new/2` with a property list of advanced cache options.\n\n| Option             | Type                | Unit          | Default | Description                                                 |\n|--------------------|---------------------|---------------|---------|-------------------------------------------------------------|\n| `bounding`         | `items` \\| `memory` |               | items   | Whether to bound cache by item count or memory.             |\n| `initial_capacity` | positive integer    | bounding      |       0 | Size cache can grow to without reallocation.                |\n| `seconds_to_live`  | positive integer    | seconds       |       ∞ | How long until entries will be evicted after first caching. |\n| `seconds_to_idle`  | positive integer    | seconds       |       ∞ | How long until entries will be evicted after last access.   |\n\n## Examples\n\nCreate a cache that can hold up 10 items:\n\n```erlang\nMaxCapacity = 10,\n{ok, Cache} = cream:new(MaxCapacity).\n```\n\n---\n\nCreate a cache that can hold up 62 items, where items are evicted 5 minutes after they are first cached:\n\n```erlang\nMaxCapacity = 62,\nTTLSeconds = 5 * 60,\n{ok, Cache} = cream:new(MaxCapacity, [{seconds_to_live, TTLSeconds}]).\n```\n\n---\n\nCreate a cache that can hold up 10,000 items (without reallocation), where items are evicted 5 seconds after they are last accessed (written or read):\n\n```erlang\nInitialCapacity = 10000,\nMaxCapacity = 10000,\nTTISeconds = 5,\n{ok, Cache} = cream:new(MaxCapacity, [{initial_capacity, InitialCapacity}, {seconds_to_idle, TTISeconds}]).\n```\n\n---\n\nCreate a memory-bounded cache:\n\n```erlang\nMaxCapacity_memory = 22,\n{ok, Cache} = cream:new(MaxCapacity_memory, [{bounding, memory}]).\n\ncream:cache(Cache, \u003c\u003c\"hello\"\u003e\u003e, fun() -\u003e \u003c\u003c\"world\"\u003e\u003e end).\n\nok = cream:sync(Cache),\n{cream:entry_count(Cache), cream:mem_used(Cache)}.\n\ncream:cache(Cache, \u003c\u003c\"foo\"\u003e\u003e, fun() -\u003e \u003c\u003c\"bar\"\u003e\u003e end).\n\nok = cream:sync(Cache),\n{cream:entry_count(Cache), cream:mem_used(Cache)}.\n\n% Cache is already at max capacity of 22 memory, so orignal entry for\n% `\u003c\u003c\"hello\"\u003e\u003e` has been evicted.\nnotfound = cream:get(Cache, \u003c\u003c\"hello\"\u003e\u003e).\n```\n\n\noutput:\n\n```\n1\u003e MaxCapacity_memory = 22,\n1\u003e {ok, Cache} = cream:new(MaxCapacity_memory, [{bounding, memory}]).\n{ok,#Ref\u003c0.1353329680.1113980932.213153\u003e}\n2\u003e\n2\u003e cream:cache(Cache, \u003c\u003c\"hello\"\u003e\u003e, fun() -\u003e \u003c\u003c\"world\"\u003e\u003e end).\n\u003c\u003c\"world\"\u003e\u003e\n3\u003e\n3\u003e ok = cream:sync(Cache),\n3\u003e {cream:entry_count(Cache), cream:mem_used(Cache)}.\n{1,{ok,22}}\n4\u003e\n4\u003e cream:cache(Cache, \u003c\u003c\"foo\"\u003e\u003e, fun() -\u003e \u003c\u003c\"bar\"\u003e\u003e end).\n\u003c\u003c\"bar\"\u003e\u003e\n5\u003e\n5\u003e ok = cream:sync(Cache),\n5\u003e {cream:entry_count(Cache), cream:mem_used(Cache)}.\n{1,{ok,18}}\n6\u003e\n6\u003e % Cache is already at max capacity of 22 memory, so orignal entry for\n6\u003e % `\u003c\u003c\"hello\"\u003e\u003e` has been evicted.\n6\u003e notfound = cream:get(Cache, \u003c\u003c\"hello\"\u003e\u003e).\nnotfound\n```\n\n---\n\nBasic caching\n\n```erlang\nMaxCapacity = 3,\n{ok, Cache} = cream:new(MaxCapacity),\nExpensiveOperation = fun() -\u003e io:format(\"value is not in cache, performing expensive operation\\n\"), 1 end,\n1 = cream:cache(Cache, {my, key}, ExpensiveOperation).\n\n1 = cream:cache(Cache, {my, key}, ExpensiveOperation).\n```\n\noutput:\n\n```\n1\u003e MaxCapacity = 3,\n1\u003e {ok, Cache} = cream:new(MaxCapacity),\n1\u003e ExpensiveOperation = fun() -\u003e io:format(\"value is not in cache, performing expensive operation\\n\"), 1 end,\n1\u003e 1 = cream:cache(Cache, {my, key}, ExpensiveOperation).\nvalue is not in cache, performing expensive operation\n1\n2\u003e 1 = cream:cache(Cache, {my, key}, ExpensiveOperation).\n1\n3\u003e\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelium%2Fcream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhelium%2Fcream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelium%2Fcream/lists"}