a-luna / fastapi-redis-cache

A simple and robust caching solution for FastAPI that interprets request header values and creates proper response header values (powered by Redis)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nothing being cached

joeflack4 opened this issue · comments

Description

Sorry for the very apt title. I'm working on this codebase: https://github.com/joeflack4/ccdh-terminology-service/tree/feature_cache

I have an app.py file where I'm importing FastApiRedisCache() and setting it up in startup().

I'm adding @cache() to several routers, one of which is in models.py. I'm using this endpoint as a test case to make sure things re working.

There's not much there at the moment, but it returns something. Here's what you can get from that endpoint as seen on our production server:
https://terminology.ccdh.io/models/
https://terminology.ccdh.io/docs#/CRDC-H%20and%20CRDC%20Node%20Models/get_models

However when I check server logs or the Redis monitor, I'm not seeing anything being cached at this or any other endpoint.

To see all the changes I've made to my codebase to implement this feature, perhaps looking at this diff in my draft pull request might help: https://github.com/cancerDHC/ccdh-terminology-service/pull/53/files

Note that this setup uses docker.

Where I've checked

1. Redis monitor

docker exec -it docker_ccdh-redis_1 sh

I don't see anything coming up as I'm checking my routes on localhost.

/data # redis-cli FLUSHALL
OK
/data # redis-cli MONITOR
OK

2. Server logs

I can't see any of the tell-tale signs of caching as per the fastapi-redis-cache documentation. I do see that the endpoints are getting hit. But they're not caching.

INFO: Application startup complete.
INFO:uvicorn.error:Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:uvicorn.error:Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

INFO: 172.19.0.1:57602 - "GET / HTTP/1.1" 307 Temporary Redirect
INFO: 172.19.0.1:57602 - "GET /docs HTTP/1.1" 200 OK
INFO: 172.19.0.1:57602 - "GET /openapi.json HTTP/1.1" 200 OK
INFO: 172.19.0.1:57864 - "GET /models/ HTTP/1.1" 200 OK
INFO: 172.19.0.1:57864 - "GET /models/ HTTP/1.1" 200 OK
INFO: 172.19.0.1:57880 - "GET /conceptreferences?key=uri&value=1&modifier=equals HTTP/1.1" 404 Not Found
INFO: 172.19.0.1:57880 - "GET /conceptreferences?key=uri&value=1&modifier=equals HTTP/1.1" 404 Not Found
INFO: 172.19.0.1:57890 - "GET / HTTP/1.1" 307 Temporary Redirect
INFO: 172.19.0.1:57890 - "GET /docs HTTP/1.1" 200 OK
INFO: 172.19.0.1:57896 - "GET /models/ HTTP/1.1" 200 OK
INFO: 172.19.0.1:57940 - "GET / HTTP/1.1" 307 Temporary Redirect
INFO: 172.19.0.1:57940 - "GET /docs HTTP/1.1" 200 OK
INFO: 172.19.0.1:57940 - "GET /openapi.json HTTP/1.1" 200 OK
INFO: 172.19.0.1:57944 - "GET /models HTTP/1.1" 307 Temporary Redirect
INFO: 172.19.0.1:57944 - "GET /models/ HTTP/1.1" 200 OK
INFO: 172.19.0.1:57944 - "GET /favicon.ico HTTP/1.1" 404 Not Found
INFO: 172.19.0.1:57948 - "GET /models/GDC HTTP/1.1" 200 OK
INFO: 172.19.0.1:57948 - "GET / HTTP/1.1" 307 Temporary Redirect
INFO: 172.19.0.1:57948 - "GET /docs HTTP/1.1" 200 OK
INFO: 172.19.0.1:57948 - "GET /openapi.json HTTP/1.1" 200 OK
INFO: 172.19.0.1:57954 - "GET /models/GDC/entities HTTP/1.1" 200 OK

What I've tried

I tried Checking that the 'redis_url' is actually getting set.
In app.py, I set up FastApiRedisCache() as follows

    redis_cache = FastApiRedisCache()
    redis_cache.init(host_url=get_settings().redis_url)

I then entered the docker container and ran python and just checked to make sure that get_settings().redis_url existed and was correct.

# ls
Pipfile  Pipfile.lock  README.md  ccdh	crdc-nodes  data  docker  docs	env  output  pytest.ini  tests
# python
Python 3.8.11 (default, Jul 22 2021, 15:32:17)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ccdh.config import get_settings
>>> get_settings()
Settings(app_name='TCCM API', neo4j_username='neo4j', neo4j_password='nFDqqgkNqzt', neo4j_host='ccdh-neo4j', neo4j_bolt_port='7687', redis_url='redis://127.0.0.1:6379', ccdhmodel_branch='main')

Possible solutions

This actually may be a problem on my docker config end. I'm investigating that now, trying out at least these 3 things:

  1. Make sure my redis url is correct, given docker setup. (currently FastAPIRedisCache says it can't connect!)
  2. Add 'link' to redis container
  3. Add depends_on health check to redis container

I'm not sure what else to try. Maybe there is something wrong with my setup in my Python code? Maybe something wrong with my docker setup?

This issue seems similar to: #33

My problem was that I am still new at docker and I needed to connect to this kind of URL:

REDIS_URL=redis://DOCKER_REDIS_SERVICE_NAME:6379

In my docker-compose.yml, I named my redis service ccdh-redis, so I needed it to look like this:

REDIS_URL=redis://ccdh-redis:6379