Caching on server with TTL
I can't find anything in the docs (great products by the TanStack team but the docs are pretty barebones) around caching data on the server so hoping someone here might be able to validate/reject my my idea.
I've got an async data loader for my root route that makes a request (could be many in the future) to an async api (at it's just a db) . I realistically only need to make this request once per user and so would be great if I could cache the result on the server and have some way to invalidate the cache when the user takes an action that requires the data to be refetched.
I saw there's a static function server cache from the docs but from what I understand:
a) a static function wouldn't work since I need the data to be cached on a per user basis.
b) even if I could build a cache at build time by querying the db, it wouldn't allow me to invalidate it at any point afterwards.
If there's currently nothing built in to TS Start for this I'll take a look at building a solution myself, but thought I'd ask the question before I get around to needing to implement this.
Thanks for any advice in advance.
3 Replies
rival-black•6mo ago
why would you want to cache this on the server?
why not on the client?
ambitious-aquaOP•6mo ago
In a later variation I'll likely be grouping users and having a cache per group. It's the server that needs access to the info anyway, only parts of it will be fed through to the client.
I'm thinking I might just pick a static location in the filesystem and just read/write to that using
fs
. Then if I end up having multiple services in my monorepo I can just wrap the access in a workspace package and call it a day.harsh-harlequin•6mo ago
What ORM are you using? Prisma has some built in stuff for caching and invalidating db queries. I think Drizzle is releasing something similar soon if they haven't already. That might suit your usecase