Next.js Discord

Discord Forum

"use cache" directive with redis cache handler

Unanswered
Cuckoo wasp posted this in #help-forum
Open in Discord
Cuckoo waspOP
Hello there.
I'm using redis for handling cache. But it seems use cache directive doesn’t work with cacheHandler property in next conifg.
Is it possible to save cached content on redis using use cache ?

21 Replies

Cuckoo waspOP
bump 😄
Dutch Smoushond
i don't think use cache can be used to save cache on redis
Chow Chow
for cacheComponents and "use cache" you need cacheHandlers not cacheHandler
I wrote this one https://gist.github.com/Haaxor1689/55e9afd3409030211315fc80db6db462
but it's not fully working
Chow Chow
https://github.com/vercel/next.js/discussions/86901
I've made a discussion post on github about it
@Cuckoo wasp bump 😄
Greek Shepherd
Havent used it myself yet, but it looks pretty good just based on code alone. Supports several different backends too
Chow Chow
huh this part makes no sense
it tees the readable stream, assigns the new stream to entry.value and the then immediately replaces it with the return stream in the return statement
so the tee is completely useless right?
@Chow Chow it tees the readable stream, assigns the new stream to `entry.value` and the then immediately replaces it with the return stream in the return statement
Greek Shepherd
I thinkkkk its just defensive programming to ensure the returned objects value always contains a valid readablestream while ensuring the internal entry does too. Otherwise if u return entry directly, the caller consumes the stream immediately, thus invalidating the internal reference. So youll temporarily have corrupted state until the caller releases the lock and the entry is gc'd. In this case i think it doesnt matter bc nothing is done with the internal entry after returning it. But its a generic defensive programming pattern to make sure the DataCacheEntry contract is being upheld (entry.value must contain a valid readablestream)
I just spent like 20 mins researching this so if im wrong pls tell me cuz its the first time ive encountered this as well and its pretty interesting
@Greek Shepherd I just spent like 20 mins researching this so if im wrong pls tell me cuz its the first time ive encountered this as well and its pretty interesting
Chow Chow
I'm asking because the readable stream is created few lines above by reading a base64 string to a new instance of readable stream so it isn't used anywhere else
I'll go try this solution and see if it produces the same issues as mine was having and if not I'll pull it apart to see what's different
I don't do tag stale/expired times, split the cache entries into multiple redis keys (meta, tags and buffer) and also do buffer streaming instead of sending and reading the hole buffer all at once
would love to find someone more familiar with Redis to tell me if my approach of splitting keys is better or not
@Chow Chow would love to find someone more familiar with Redis to tell me if my approach of splitting keys is better or not
Greek Shepherd
though not sure ab this. probably better to ask in a different server
also wanna dm? we're spamming this poor guys post