Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Implementing caching

default discord avatar
augdust11 months ago
14

Hi Payload-server! I'm looking to implement caching of my frontend, as it is a mainly static marketing landing page site. Payload is used as a website-builder in this instance, allowing other people in the department to build out the website without the need to rebuild and redeploy every time.



We are self-hosting in Openshift/Kubernetes, and serve the application across 2 pods. I need to have a shared cache solution, so invalidating a tagged cache does it for both running instances at the same time. I've tried setting up Redis for this, with no luck so far.



I managed to cache my LocalAPI responses/fetches, but that made Live Preview stop working, so a caching solution would need to take live preview into account somehow and only serve cached content to actual website visitors, not editors working in the admin panel.



If anyone has set up a solution akin to what I need, I would love to get some ideas or pinpointers to get the ball rolling, or look at some examples.

  • default discord avatar
    fofoy.11 months ago

    Hi

    @170674545965924352

    , what exactly didn't work in setting up Redis to handle the shared cache ? I have exactly the same setup as you and it works just fine !

  • default discord avatar
    augdust11 months ago

    I've just been trying to configure a custom cache-handler with various GPTs (life of a solo dev 🥲 ) and none of they have compiled/functioned correctly. Do you have a example of how you configured yours?

  • default discord avatar
    fofoy.11 months ago

    Yep, I've been using this

    https://caching-tools.github.io/next-shared-cache/redis

    as a base and just added some logic related to CI stuff but it worked straight away, let me know if you need a hand and I'll try to help you

  • default discord avatar
    augdust11 months ago

    that link gives me a 404, but it looks like the neshca/cache-handler package, which unfortunately doesn't work with NextJS 15 yet, which my project is running (running Payload v3 atm)

  • default discord avatar
    fofoy.11 months ago

    Ah yes indeed it's nearly there but not yet compatible with Next 15 unfortunately, there's an alternative package that's been made based on neshca/cache-handler here :

    https://github.com/fortedigital/nextjs-cache-handler
  • default discord avatar
    augdust11 months ago

    nice! will take a look.



    Did you manage to cache your LocalAPI-calls and still maintain livepreview functionality? when I wrapped my fetch-functions for payload collections in "unstable_cache()" it broke my livepreview, requiring either a publish of the document or a reload of the page to view the changes that had been made

  • default discord avatar
    fofoy.11 months ago

    I am not using livepreview atm, I will re-introduce it sometime soon but I didn't have the time to spend on that. I was using it before implementing cache methods like

    unstable_cache

    . I guess you could wrap the cache logic into an util and disable the cache when you are in

    draftMode
  • default discord avatar
    augdust11 months ago

    if you have an example of your implemented cache handler i'd love to take a look at it if possible :)

  • default discord avatar
    fofoy.11 months ago

    Not at all, what exactly do you want to see ? Bear in mind that I'm using Next 14 for now but happy to help

  • default discord avatar
    augdust11 months ago

    ah okay, then it might not be too helpful. specifically looking for a nextjs 15 compatible implementation, never used Redis or any caching system before so it's all just a bit overwhelming (and for GPT-agents too atm haha).



    I have got unstable_cache to work nicely with live preview now tho, so for now we will just live with content being stale for visitors for a while until the revalidate kicks in :) will probably have another go once neshca gets a 2.0 release, or we really need to be able to invalidate tags across multiple pods

  • default discord avatar
    fofoy.11 months ago

    Yeah, everything is a bit more obscure when you try to avoid using Vercel where all the magic happens 😄

  • default discord avatar
    augdust11 months ago
    :upsidedown_cry:
  • default discord avatar
    baotaoh11 months ago

    I have a similar setup but deploying in azure.


    I am think of implementing a endpoint that handles revalidation in the nextjs website that I will call from the payload instance in a aftersave hook.

  • default discord avatar
    fofoy.11 months ago

    Yes, that's the first step (the easy bit), no matter how many servers are serving the frontend application.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.