{"id":15538550,"url":"https://github.com/shastick/zio-prefetcher","last_synced_at":"2025-04-23T15:24:47.938Z","repository":{"id":39666786,"uuid":"288437457","full_name":"Shastick/zio-prefetcher","owner":"Shastick","description":"When caching is not enough. Pre-fetch everything you need to RAM and refresh it on a regular basis.","archived":false,"fork":false,"pushed_at":"2022-05-27T16:52:31.000Z","size":62,"stargazers_count":17,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-09T12:31:43.502Z","etag":null,"topics":["caching","prefetching","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/Shastick.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":"2020-08-18T11:31:38.000Z","updated_at":"2023-07-30T00:29:45.000Z","dependencies_parsed_at":"2022-09-20T07:42:27.909Z","dependency_job_id":null,"html_url":"https://github.com/Shastick/zio-prefetcher","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shastick%2Fzio-prefetcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shastick%2Fzio-prefetcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shastick%2Fzio-prefetcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shastick%2Fzio-prefetcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shastick","download_url":"https://codeload.github.com/Shastick/zio-prefetcher/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250458540,"owners_count":21433891,"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":["caching","prefetching","scala","zio"],"created_at":"2024-10-02T12:04:49.620Z","updated_at":"2025-04-23T15:24:47.921Z","avatar_url":"https://github.com/Shastick.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `zio-prefetcher`\n\nRAM is cheap. Caches can behave in unpredictable ways. Thus:\n\n\u003e Either things are in RAM or they don't exist.\n\n## What's a Prefetcher?\n \nA prefetcher is something that will do some work _in advance_ of when it is needed, while keeping the result around until it is eventually useful.\n\nHence, the time it takes to access pre-fetched things is entirely predictable. Think of a prefetcher as a `Supplier[T]` \nthat returns a `T` immediately while regulary refreshing itself in the background  \n\nNote that it is _slightly_ different from a cache. Please refer to [this blog post](https://j3t.ch/tech/prefetching-pattern/) for some further context.\n\n\n## How To Use\n\n```scala\n\ntype PrefetchedVal = Map[UserId, UserSettings]\n\nval supplier(): ZIO[PrefetchedVal, Throwable, PrefetchedVal] = ...\n\nfor {\n  prefetcher \u003c- PrefetchingSupplier.withInitialValue(initialValue = Map(), supplier = supplier(), updateInterval = 1.second)\n  ...\n  instantAccess \u003c- prefetcher.currentValueRef.get\n  settings = instantAccess(someUserId)\n  ...\n} yield ...\n\n\n```\n\nThe artifact coordinates are:\n\n```\nlibraryDependencies += \"ch.j3t\" %% \"zio-prefetcher\" % version\n```\n\n[Check over here for all available versions](https://mvnrepository.com/artifact/ch.j3t/zio-prefetcher)\n\nThis library is built against `zio` version `1.0.7` and `zio-logging` version `0.5.8` but expects you to provide `zio` and `zio-logging` at runtime.\n\nAdditionally, if you intend to rely on the pre-fetchers that expose metrics, you'll need to provide `zio-metrics-dropwizard`.\n\n## Example use cases\n\nAssume a service with some users and their associated settings saved somewhere in a storage engine. \nOccasionally, these settings may change, but it's OK if updates don't propagate immediately.\n\nIn your code, you have some implementation of `UserId =\u003e Future[UserSettings]`. This is nice, but...\n\nYour constraints may be such that looking up some settings in the backend takes too much time to your taste (_very_ picky users, crappy database, ...)\n\nAssuming you have some spare RAM, you can set up a prefetcher such that you'll always have a `Map[UserId, UserSettings]`\nsitting around, ready to be used. Now you can access all existing settings with a direct map lookup at any time.\n\n# Consideration\n\n## When to use a pre-fetcher\n\nA pre-fetcher can be useful in situations where you have some data (configuration or otherwise) that is either:\n - entirely static\n - rarely updated \n - robust to staleness (ie, for which not immediately having the latest version is OK).\n  \nCombined with access costs that range from _annoying_ to _prohibitive_, eg:\n - latencies to reach some other service\n - complex queries that take a (long) while but yield small results\n \nAnd assuming that everything you generally need fits in RAM...\n\nFor cases that match all the above, it can be interesting to rely on pre-fetched data instead of looking things up\nthrough an external service, be it with or without some caching involved.\n\n## What does this solve that a cache cannot?\n\nCaches are great, no questions here.\n\nThe issue with a cache, in general, is that on a cache miss, the caller still needs to wait.  \n\nHaving a cache that pre-loads all possible queries or arguments to a function or service, while pro-actively refreshing \nthe cached values if required, would solve for that problem.\n\nHowever, if you do so, you essentially end up with a _prefetcher_, not using the other cache features like eviction policies: \nThis pre-fetcher is intended for use in exactly such cases.  \n\n## How it came to be\n\nI've been using this _prefetching pattern_ in some projects, and am currently learning more about ZIO. \nMaking a pure ZIO prefetcher seemed like a nice little challenge. \n\n## Disclaimer\n\nThis is ~~some toy-code that will see some production use~~ actually being used in production but is, \nnevertheless, written by a relative ZIO-Newbie: so use it at your own risks.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshastick%2Fzio-prefetcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshastick%2Fzio-prefetcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshastick%2Fzio-prefetcher/lists"}