Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to invalidate the cache for a post method #298

Open
coolsnake opened this issue Sep 10, 2023 · 7 comments
Open

How to invalidate the cache for a post method #298

coolsnake opened this issue Sep 10, 2023 · 7 comments
Labels
question Further information is requested

Comments

@coolsnake
Copy link

I want to invalidate the cache if user call POST method to update data into database, so that th GET method can return the latest data to user.

@hard-coders
Copy link

You need to delete a key by yourself. Of course, you should use a custom key builder to get the key or delete it by using namespace.

Here's the later example.
e.g.

from redis import Redis
from fastapi import FastAPI
from fastapi_cache.decorator import cache

app = FastAPI()
redis = Redis()


@app.get("/")
@cache(namespace="some_namespace")
def get_some_data():
    return {"data": "some data from database"}


@app.post("/")
def update_some_data():
    for key in redis.keys("some_namespace:*"):
        redis.delete(key)
    return {"data": "updated data"}

If the namespace is too large, use scan_iter rather than keys function.

@coolsnake
Copy link
Author

coolsnake commented Oct 1, 2023

Thanks. But it looks we can not find key by redis.keys("some_namespace:*") without custom key builder because the key is finally hashed.

@hard-coders
Copy link

Thanks. But it looks we can not find key by redis.keys("some_namespace:*") without custom key builder because the key is finally hashed.

You are right. That's why I told you, "You should use a custom key builder". FastAPI-Cache hash keys using MD5 default, so you cannot identify resources. In general, if you don't have to invalidate immediately, set the TTL value properly.

@vicchi vicchi added the question Further information is requested label Nov 5, 2024
@espdev
Copy link

espdev commented Nov 11, 2024

This is a very common case where we need to invalidate the cache for related GET methods when updating data via POST/PUT/PATCH.

Maybe consider to add some high-level API to the library to invalidate the cache in such cases? Without having to specify a custom key builder. Since in essence the proposed method reveals internal implementation details and violates the encapsulation principle.

@vicchi
Copy link
Collaborator

vicchi commented Nov 11, 2024

@espdev Would you be willing to submit a PR for this?

@espdev
Copy link

espdev commented Nov 12, 2024

@vicchi First, I need to look at the code. I think I may if I have some time on my hands.

@espdev
Copy link

espdev commented Nov 13, 2024

It seems this PR already adds such functionality. The TagProvider approach looks flexible, but somewhat complicated. Duplicate argument names in items_provider can cause problems during refactoring. But I didn't look too deeply into the architecture of this solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants