{"id":21976791,"url":"https://github.com/stuartapp/zcaffeine","last_synced_at":"2025-07-29T08:09:37.480Z","repository":{"id":41830830,"uuid":"485362815","full_name":"StuartApp/zcaffeine","owner":"StuartApp","description":"ZIO wrapper for the Caffeine library : https://github.com/ben-manes/caffeine","archived":false,"fork":false,"pushed_at":"2024-08-15T20:06:27.000Z","size":88,"stargazers_count":12,"open_issues_count":13,"forks_count":1,"subscribers_count":44,"default_branch":"main","last_synced_at":"2025-04-28T16:57:12.324Z","etag":null,"topics":["cache","caffeine","scala","zio"],"latest_commit_sha":null,"homepage":"","language":"Scala","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/StuartApp.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,"zenodo":null}},"created_at":"2022-04-25T12:29:46.000Z","updated_at":"2024-12-14T22:35:04.000Z","dependencies_parsed_at":"2025-04-28T16:45:48.545Z","dependency_job_id":"b59e8cf7-8335-40a1-9493-96bd1479777d","html_url":"https://github.com/StuartApp/zcaffeine","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/StuartApp/zcaffeine","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StuartApp%2Fzcaffeine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StuartApp%2Fzcaffeine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StuartApp%2Fzcaffeine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StuartApp%2Fzcaffeine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StuartApp","download_url":"https://codeload.github.com/StuartApp/zcaffeine/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StuartApp%2Fzcaffeine/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267652788,"owners_count":24122098,"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","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"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","caffeine","scala","zio"],"created_at":"2024-11-29T16:12:18.552Z","updated_at":"2025-07-29T08:09:37.455Z","avatar_url":"https://github.com/StuartApp.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zcaffeine: ZIO-friendly Caffeine wrapper\n\n![GitHub Workflow Status](https://img.shields.io/github/workflow/status/StuartApp/zcaffeine/Continuous%20Integration)\n![GitHub](https://img.shields.io/github/license/StuartApp/zcaffeine)\n![Maven Central](https://img.shields.io/maven-central/v/com.stuart/zcaffeine_2.13?label=Release)\n![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/com.stuart/zcaffeine_2.13?label=Snapshot\u0026server=https%3A%2F%2Fs01.oss.sonatype.org)\n\nzcaffeine is a ZIO-friendly wrapper for the [Caffeine cache library](https://github.com/ben-manes/caffeine).\n\n* Built for ZIO 2\n* Available for Scala 2.13 and Scala 3\n\n## Setup\n\nAdd the library to your dependencies:\n\n```scala\nlibraryDependencies += \"com.stuart\" %% \"zcaffeine\" % \"1.0.0-M1\"\n```\n\nSnapshots are also published on [Sonatype’s snapshots repository](https://s01.oss.sonatype.org/content/repositories/snapshots/com/stuart/).\n\n## Usage\n\nThe [ZCaffeine](src/main/scala/com/stuart/zcaffeine/ZCaffeine.scala) object is the entrypoint for configuring your cache, by using either:\n* A [CaffeineSpec](https://www.javadoc.io/doc/com.github.ben-manes.caffeine/caffeine/latest/com.github.benmanes.caffeine/com/github/benmanes/caffeine/cache/CaffeineSpec.html),\n  either in its string representation or already loaded\n* An unconfigured builder\n\nPlease note that setting that can be configured by a [CaffeineSpec](https://www.javadoc.io/doc/com.github.ben-manes.caffeine/caffeine/latest/com.github.benmanes.caffeine/com/github/benmanes/caffeine/cache/CaffeineSpec.html)\ncan also be configured from the builder, with the builder supporting more options:\n* Setting removal and/or evictions listeners\n* Dynamically setting the expiration of cache entries based on the key \u0026 value\n* Enabling scheduling of cache maintenance (rather than running maintenance on cache operations)\n* Compute a cache entry weight dynamically, based on the key \u0026 value\n\nAfter configuration, the cache can be constructed through either:\n* `build()`: builds a \"manually operated\" `Cache` which requires the use of `get(key, mappingFunction)`/`getAll(keys, mappingFunction`)/`put(key,value)` \n             to load or replace values in the cache\n* `build(loadOne,loadAll,reloadOne)`: builds an \"automated\" `LoadingCache` cache which offers, along with the other methods from `Cache`,\n                                      `get(key)`/`getAll(keys)`/`refresh`/`refreshAll` that defers to `loadOne`/`loadAll`,`reloadAll` to compute the cache entries\n\n\u003e Note:\n\u003e `loadAll` and `reloadOne` are optional: both defer to `loadOne` if missing, but they can\n\u003e set if the behavior should differ (eg. parallel computation in `loadAll`)\n\n## Examples\n\n### Building a cache\n\n```scala\n\n// Configure a cache\nval zcaffeine = ZCaffeine[Any, String, String]().map(\n    _.initialCapacity(InitialCapacity(1))\n      .maximumSize(MaxSize(10))\n      .enableScheduling()\n      .recordStats()\n      .expireAfter(\n        afterCreate = (key, _, _) =\u003e key.length.hour,\n        afterUpdate = (key, _, _, _) =\u003e key.length.minutes,\n        afterRead = (_, _, _, _) =\u003e Duration.Infinity\n      )\n  )\n  \nval getTime = ZIO.clockWith(_.instant)\n\n// Create a Cache \nval cache = zcaffeine.flatMap(_.build())\n\n// Create a LoadingCache\nval loadingCache = zcaffeine.flatMap(\n    _.build(\n      loadOne = key =\u003e getTime.map(time =\u003e s\"one::$key:$time\"),\n      loadAll = Some(keys =\u003e getTime.map(time =\u003e keys.map(key =\u003e (key, s\"all::$key:$time\")).toMap)),\n      reloadOne = Some((key, oldValue) =\u003e getTime.map(time =\u003e s\"reload::$key:$oldValue--\u003e$time\"))\n    )\n  )  \n```\n\n### Using a cache\n\nYou can also find additional examples from [zcaffeine’s tests](src/test/scala/com/stuart/zcaffeine).\n\n```scala\n/**********************************************/\n/*               Using a Cache                */\n/* Supposing cache = Cache[Any,String,String] */\n/**********************************************/\n\n\n// get or compute\ncache.get(\"key\")\n// get all or compute all\ncache.getAll(\"key1\",\"key2\"))(keys =\u003e ZIO.succeed(keys.map(key =\u003e (key, key + key)).toMap))\n// get only if cached\ncache.getIfPresent(\"key3\")\n// insert or replace\ncache.put(\"key\", ZIO.succeed(\"value1\"))\n// invalidate one\ncache.invalidate(\"key\")\n// invalidate all\ncache.invalidateAll\n// invalidate all specified keys\ncache.invalidateAll(\"key1\",\"key2\")\n\n\n/*****************************************************/\n/*               Using a LoadingCache                */\n/* Supposing cache = LoadingCache[Any,String,String] */\n/*****************************************************/\n\n// get or compute using the functions set while building the cache\ncache.get(\"key1\")\n// get all or compute all using the functions set while building the cache\ncache.getAll(Set(\"key1\",\"key2\"))\n// refresh using the functions set while building the cache and get\ncache.refresh(\"key1\")\n// refresh all using the functions set while building the cache and get all\ncache.refreshAll(\"key1\",\"key2\")\n```\n## License\n\nThis project is published under the [Apache 2.0 license](https://www.apache.org/licenses/LICENSE-2.0).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstuartapp%2Fzcaffeine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstuartapp%2Fzcaffeine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstuartapp%2Fzcaffeine/lists"}