Replies: 3 comments 2 replies
-
A standard cache interface would be very welcome! It might be worth checking PHP's PSR-6: Caching Interface and PSR-16: Common Interface for Caching Libraries for a grasp of how it's usually implemented there. Coming from there, I would revisit the interface to add the following concepts:
interface Cache {
set(key: string; value: unknown; ttl?: number): Promise<void>;
get(key: string; defaultValue?: any): Promise<unknown | null>;
del(key: string): Promise<void>;
has(key: string): Promise<boolean>;
} We could also add more methods as syntactic sugar: interface Cache {
setMultiple(values: string[]; ttl?: number): Promise<void>;
getMultiple(keys: string[]; defaultValue?: any): Promise<unknown[] | null>;
deleteMultiple(keys: string[]): Promise<void>;
// callable gets executed only if key isn't cached yet, to set its value
getOrSet(key: string; callable: () => Promise<any>; ttl?: number): Promise<unknown | null>;
} Some implementations also have a 4th parameter to This allow to do stuff like this:
cache.getOrSet(
"member-list",
async () => await db.member.findMany(),
3600,
async () => await db.member.query("SELECT MAX(id)")
); but that may be considered too much |
Beta Was this translation helpful? Give feedback.
-
Should this be something done by a third-party library or by Remix itself? While it could be a cool feature, there's probably a lot of other things Remix as a project needs to solve first in their core offering. Caching is already a "solved" problem by all the libraries you mentioned, and people should be writing small interfaces over these libraries when they use them in their own projects anyways. Not particularly opinionated either way, just curious what everyone's thoughts are. |
Beta Was this translation helpful? Give feedback.
-
The vite & unjs team is working on a similar idea of creating framework agnostic storage layer for web apps; they've been on a roll recently in trying to unify the ecosystem ❤️ https://github.com/unjs/unstorage CC @patak-dev @pi0 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal
Add a generic interface for server-side caches, similar to SessionStorage that works with many storage providers (file system, memory, Worker KV, custom ones with a database), to let Remix developers know how server-side cache works in every Remix app, but allowing the infrastructure behind change between them.
Background
Remix has bring to the table of many frontend developers how to cache the data fetched or queried on the server.
Being able to use HTTP cache is great and in some cases the preferred way, but it gives you less control over your cache, e.g. invalidate browser cache without depending on tricks like Vary header + random cookie value.
Server-side caches are often desired too, it's safer to cache data than the whole HTML, specially if you deploy many times to tweak the UI.
Use Case
API/Example
I was thinking on a simple interface like this
Remix could provide a factory similar to createSessionStorage.
Remix could also provide some built-in cache storages
Beta Was this translation helpful? Give feedback.
All reactions