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

Help: Quickstart example not working #447

Open
tariksetia opened this issue Oct 11, 2024 · 2 comments
Open

Help: Quickstart example not working #447

tariksetia opened this issue Oct 11, 2024 · 2 comments
Assignees
Labels
bug Something isn't working cant-reproduce Cannot reproduce after testing invalid This doesn't seem right

Comments

@tariksetia
Copy link

tariksetia commented Oct 11, 2024

Versions:

fastapi-cache2==0.2.2
fastapi-cache2[redis]==0.2.2

.venv ❯ python --version
Python 3.12.4

Redis:
docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:7.4.0-v0

Code:

from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
import time

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

from redis import asyncio as aioredis
import uvicorn


@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
    redis = aioredis.from_url("redis://0.0.0.0")
    FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
    yield


app = FastAPI(lifespan=lifespan)


@cache()
async def get_cache():
    return 1


@app.get("/")
@cache(expire=10)
async def index(request: Request):
    return time.time()


if __name__ == "__main__":
    uvicorn.run(app, port=8000)
Screen.Recording.2024-10-11.at.6.55.13.PM.mov

Issue:: Every time I hit http://0.0.0.0:8000/ I get a new value of time, even within 10 seconds

@tariksetia tariksetia changed the title Quickstart example not working Help: Quickstart example not working Oct 21, 2024
@jameswinegar
Copy link

Enable debug logging. I am guessing that you're not actually connecting to redis on 0.0.0.0

@vicchi vicchi self-assigned this Nov 9, 2024
@vicchi
Copy link
Collaborator

vicchi commented Nov 9, 2024

@tariksetia Thanks for getting in touch. Unfortunately I can't reproduce this.

$ uname -a
Darwin garys-m1-mbp-e.vicchi.xyz 24.1.0 Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:15 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6000 arm64

$ docker --version
Docker version 27.3.1, build ce1223035a

$ which python
/Users/gary/Projects/personal/quickstart/.direnv/python-3.13/bin/python

$ python --version
Python 3.13.0

$ docker pull redis/redis-stack-server:7.4.0-v0
...
$ docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:7.4.0-v0
...

$ ./quickstart-test.py

First test ...

> GET http://localhost:8000/
< 200 OK
< date: Sat, 09 Nov 2024 10:35:44 GMT
< server: uvicorn
< content-length: 18
< content-type: application/json
< cache-control: max-age=10
< etag: W/-2101217112019676328
< x-fastapi-cache: MISS

Second test ...

> GET http://localhost:8000/
< 200 OK
< date: Sat, 09 Nov 2024 10:36:01 GMT
< server: uvicorn
< content-length: 17
< content-type: application/json
< cache-control: max-age=6
< etag: W/8484820968731344498
< x-fastapi-cache: HIT

Third test (after waiting 15 seconds) ...

> GET http://localhost:8000/
< 200 OK
< date: Sat, 09 Nov 2024 10:37:03 GMT
< server: uvicorn
< content-length: 17
< content-type: application/json
< cache-control: max-age=10
< etag: W/9133270487873532393
< x-fastapi-cache: MISS

Fourth test ...

> GET http://localhost:8000/
< 200 OK
< date: Sat, 09 Nov 2024 10:37:26 GMT
< server: uvicorn
< content-length: 17
< content-type: application/json
< cache-control: max-age=9
< etag: W/6747949079204684616
< x-fastapi-cache: HIT

I've also repeated the tests on an X86_64 box running Ubuntu 22.04 with the same results

$ uname -a
Linux hopper.vicchi.xyz 6.8.0-48-generic #48~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct  7 11:24:13 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

@vicchi vicchi added bug Something isn't working invalid This doesn't seem right cant-reproduce Cannot reproduce after testing labels Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cant-reproduce Cannot reproduce after testing invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants