Git Product home page Git Product logo

swrr's Introduction

swrr

Stale While Revalidate Resource · an edge pass-through cache utility

js downloads licenses gzip size brotli size

This is free to use software, but if you do like it, consisder supporting me ❤️

sponsor me buy me a coffee

Caveat ~ Currently cache backplane is assumed to be Cloudflare Workers 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

// file: my-worker.esm.ts

import * 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

swrr's People

Contributors

maraisr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.