fastapi-cache icon indicating copy to clipboard operation
fastapi-cache copied to clipboard

Cache-Control not working

Open lccmrx opened this issue 2 years ago • 3 comments

Hi,

I've been using your lib for a while now, and it's amazing! But for some late issues, I've found out that the Cache-Control: no-cache wasn't working.

Here is my code and a printscreen so you could understand my case:

import random
import string
import aioredis
from fastapi import FastAPI
from starlette.requests import Request
from starlette.responses import Response

from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cache

app = FastAPI()

@app.get("/")
@cache(expire=60)
async def index(request: Request, response: Response):
    letters = string.ascii_lowercase
    rand = ''.join(random.choice(letters) for i in range(10))
    print(rand)
    return dict(hello=rand)

@app.on_event("startup")
async def startup():
    redis =  aioredis.from_url("redis://localhost", encoding="utf8", decode_responses=True)
    FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")

I've used the similar as in README.md I changed the response so we could see that I'm generating a new response on each call of the route. Keep in mind that I printed the new generated string inside the function, so I could know every now and then when I create a new string.

image As expected, a new string is generated and printed on console.

image But here you can see that I've added Cache-Control on the following requests and it still retrieves the cached string.

image As I wait for the 60 seconds, I could successfully generated a new string.

Am I doing something wrong?

lccmrx avatar Aug 23 '21 14:08 lccmrx

@long2ice I've created a PR https://github.com/long2ice/fastapi-cache/pull/33. I think this solves my issue, but keep in mind that there are other cache headers, like https://developer.mozilla.org/pt-BR/docs/Web/HTTP/Headers/Cache-Control

lccmrx avatar Aug 23 '21 14:08 lccmrx

no-cache does not mean don't cache, but validate from server first

long2ice avatar Aug 24 '21 01:08 long2ice

sorry for the mozilla doc in portuguese, here is the english version: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

Exactly, but the library doesn't respect no-cache directive as it should.

I understand that it should get the data from server instead from cache, but as is doesn't work. no-store works, and doesn't create the cache key at all, but in my case, I want to explicitly retrieve the data from application.

The use case is such as, a list of users are retrieved, I create a new user, then I retrieve the list of users. My idea is to invalidate the cache on the second request, so the cache can now see the new resource.

lccmrx avatar Aug 24 '21 03:08 lccmrx

Same issue here. If I send Cache-Control: no-cache in a request, I would expect the decorated function to be called, regardless of the current caching status, but it isn't. In fact, there seems to be no way to force revalidation of the requested resource as the requester? :thinking: I think this is a must-have feature. Any plans to implement this?

muety avatar Dec 05 '22 12:12 muety

Hi @muety

I think it was already merged on PR https://github.com/long2ice/fastapi-cache/pull/33

lccmrx avatar Dec 05 '22 13:12 lccmrx

The specific issue here has been resolved. I do have plans to improve the Cache-Control header handling as no-cache doesn't mean the same thing as no-store. See #144.

mjpieters avatar May 15 '23 12:05 mjpieters