Clearing redis cache on a query on new data or data change

Options

Hey guys,

I just enabled caching for the first time on a query (GET endpoint) level via the interface, see screenshot.

Now I am wondering, can I also force a cache clearing for this query (GET endpoint), if e.g. a user hits a POST endpoint and adds a new data row?

Right now, I have the problem that my query uses an Addon and if data changes in the table that this addon relys on I would love to reset the cache for queries on this table and also for queries that use the addon otherwise I serve old data.

I saw that there are cache management functions available but I don't know the key for this query cache as it is setup on a query level in the interface and not programatically.

Thanks so much

Timo

Tagged:

Answers

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    I'd add a call to the GET endpoint via External API Request in your POST with a cache-busting header. That way the next human/FE call to it will use the fresh data.

  • timofischer
    timofischer Member
    Options

    Thanks so much @Ray, that sounds clever. How does this "cache-busting header" work? What kind of header would I have to send? Cheers Timo

  • MattLaro
    MattLaro Member ✭✭
    Options

    I think the way you're doing it right now by using the prebuilt caching of Xano will not let you have the necessary control of what you're trying to accomplish here (anyone could correct me if I'm wrong. I personally did not find any way to do this).

    You will need to create a cache using the Add function dialog in your API endpoint to Create, Get and Delete your cache via Data Caching (Redis)

    In this case, you would Query your table like you did under alert_config… Then, right after that, you'd create a Cache with a key name of your liking.

    The cache-buster part would be to fetch a header from $http_headers at the beginning of your endpoint (again, with a name of your liking… example : X-Clear-Cache:true). Then proceed as following.

    Here's an example.

    1. If $http_headers → get X-Clear-Cache = true
      1. Delete cache x
    2. Check if cache exists with the key x = has_cache
    3. If has_cache = false
      1. Query your data → alert_config
      2. Create cache with key x with your alert_config data → N/A
    4. else
      1. Get cache with key x → alert_config

    Return alert_config.

  • timofischer
    timofischer Member
    Options

    Thanks @MattLaro. I understood Ray in the way that there is some header parameter (like X-Clear-Cache:true) that I can send also to an endpoint using the prebuild Xano query cashing to trigger clearing its cache. I just don't know what header parameter this would be (if I understood him right).

  • MattLaro
    MattLaro Member ✭✭
    edited June 2023
    Options

    Hmmm @Ray Deck is there such thing you would know about ? I'd be glad if there was ! 😁

    As far as I know, the prebuilt caching in Xano limits itself to the parameters you put in the endpoint configuration (timed cache, cache by user, etc.) and the caching time must be adjusted to match the frequency of when you update your data.

    Clearing Xano cache is possible through it's backend maintenance tool (Ex: when you push a branch into production and you want to start on a clean slate), but not in context of API calls.

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    I would be glad if there was too. To my knowledge, a solution like @timofischer describes (and I errantly implied in my note) this is not currently available.

    To address these needs, I was imagining an approach using the data caching functions in Redis like you envision @MattLaro. By caching at that level, you could control the cache through either listening for a cache-busting header or do it more directly by removing the appropriate key in Redis.

  • timofischer
    timofischer Member
    Options

    Thanks a lot for the detailed explanations. Not as simple as I hoped for, but then it is clear how to do it.