https://github.com/maraisr/swrr
⏩ Stale While Revalidated Resources — keeps data fast
https://github.com/maraisr/swrr
resources stale-while-revalidate swr workers-kv
Last synced: 7 months ago
JSON representation
⏩ Stale While Revalidated Resources — keeps data fast
- Host: GitHub
- URL: https://github.com/maraisr/swrr
- Owner: maraisr
- License: mit
- Created: 2022-02-15T08:12:07.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-19T02:15:46.000Z (over 1 year ago)
- Last Synced: 2024-10-12T05:28:21.196Z (12 months ago)
- Topics: resources, stale-while-revalidate, swr, workers-kv
- Language: TypeScript
- Homepage:
- Size: 101 KB
- Stars: 26
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# swrr
Stale While Revalidate Resource · an edge pass-through cache utility
This is free to use software, but if you do like it, consisder supporting me ❤️
[](https://github.com/sponsors/maraisr)
[](https://www.buymeacoffee.com/marais)> **Caveat** ~ Currently cache backplane is assumed to be
> [Cloudflare Workers KV](https://developers.cloudflare.com/workers/runtime-apis/kv). Before we hit 1.0 release, we will
> aim to support a generic and abstract backplane for the likes of Redis, Memcached, or other layers.## 🚀 Usage
```ts
// file: my-worker.esm.tsimport * as swr from 'swrr';
const getPostsInCategorySince = async ({ category, since }) => {
const posts = await cms.getPostsInCategory(category);return posts.filter((post) => post.updatedAt > since);
};export default {
async fetch(req, env, ctx) {
// ⬇️️ create "container", all resources will batch in this boundary.
// ~> you'd probably want this in a middleware.
const makeResource = swr.make(env.KV_NAMESPACE, ctx);// ⬇️ create a resource connected to a handler and name it
const getLatestPosts = makeResource('posts', getPostsInCategorySince);// ... whatever elese
// ⬇️ run the resource, passing in whatever arguments you'd like
const posts = await getLatestPosts({ category: 'foobar', since: '2022-01-01' });// ~> and one would now find a KV entry for
// 'posts__cdbdf4617568dc29453d3fee5f5ca79a7713b15f'return new Response(posts, { headers: { 'content-type': 'application/json' } });
},
};
```## License
MIT © [Marais Rossouw](https://marais.io)