jaredwray/cacheable

Thoughts on using standardized web Request / Response / Headers?

Closed this issue · 9 comments

The new era of modern api uses standard web Request / Response using the fetch api.
I was just wondering if you would maybe perhaps thing if such adoption would be acceptable...
there are some new modern backends that embraces the fetch api and turns regular incoming http request from nodejs and turns them into standard Request. Deno, Bun and cloudflare workers also operates on standard web Request & Response.

Maybe starting out fresh and create a new package?

That is exactly where we will be focusing on an plan in 2024 to release a @cacheable/fetch that will enable caching to fetch. Is that what you are thinking?

YES


Wondering if it could be based on CacheStorage perhaps.

Maybe add a
@cacheable/cachestorage also?

I havent seen the CacheStorage interface yet but the current plan is to base it off of Keyv (https://github.com/jaredwray/keyv).

How would @cacheable/cachestorage function?

@cacheable/cachestorage would be kind of a polyfill for the missing window.cachestorage that could store Request / Responses á la browser spec'ed way.

you shove in a Request as the "key" and map it to a Response

const request = new Request('https://example.com')

let response;
const cachedResponse = caches
  .match(request)
  .then((r) => (r !== undefined ? r : fetch(request)))
  .then((r) => {
    response = r;
    caches.open("v1").then((cache) => {
      cache.put(request, response);
    });
    return response.clone();
  })
  .catch(() => caches.match("https://example.com"));

code example from: https://developer.mozilla.org/en-US/docs/Web/API/Cache/put

this @cacheable/cachestorage would be working as a kind of CRUD (Create, read, update, delete) layer on top of any kv storage maybe?

Deno have CacheStorage: https://github.com/denoland/deno/blob/main/ext/cache/01_cache.js
So dose undici: https://github.com/nodejs/undici/blob/main/lib/cache/cachestorage.js

But they are very type brand strict, so they can't work with any Request/Response like object...
nodejs/undici#2082 (comment)

Closing this now as will create a new issue as we migrate cache systems to this.

Kindly post an update here when the new issue is created 🙂

@brianjenkins94 - will do. We are finishing up Keyv v5 in Q1 and then will move onto big updates in cacheable libraries in Q2 and Q3.