Skip to content

Commit 39fd2d4

Browse files
author
Andrew Brookins
committed
Add caching
1 parent 0d61980 commit 39fd2d4

10 files changed

+111
-16
lines changed

Diff for: .pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repos:
1313
rev: 3.8.0
1414
hooks:
1515
- id: flake8
16-
exclude: (.git|__pycache__|__init__.py|core/application.py|app/main.py|app/dao/base.py)
16+
exclude: (.git|__pycache__|__init__.py|core/application.py|app/main.py|app/dao/base.py|data)
1717
additional_dependencies: ['flake8-typing-imports==1.7.0']
1818
- repo: https://github.com/pre-commit/mirrors-autopep8
1919
rev: v1.5.2

Diff for: Dockerfile

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-
1111
# Copy poetry.lock* in case it doesn't exist in the repo
1212
COPY ./pyproject.toml ./poetry.lock* /app/
1313

14-
ARG INSTALL_DEV=false
15-
RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --no-dev ; fi"
14+
RUN bash -c "poetry install --no-root"
1615

1716
COPY ./app /app

Diff for: app/config.py

-10
This file was deleted.

Diff for: app/main.py

+24
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,36 @@
22

33
from aiotestspeed.aio import Speedtest
44
from fastapi import FastAPI
5+
from fastapi import Request
6+
from fastapi import Response
7+
from fastapi_redis_cache import cache
8+
from fastapi_redis_cache import FastApiRedisCache
9+
from pydantic import BaseSettings
10+
11+
12+
class Config(BaseSettings):
13+
redis_url: str = 'redis://redis:6379'
14+
515

616
logger = logging.getLogger(__name__)
717
app = FastAPI()
18+
config = Config()
19+
app = FastAPI(title='FastAPI Redis Cache Example')
20+
21+
22+
@app.on_event('startup')
23+
def startup():
24+
redis_cache = FastApiRedisCache()
25+
redis_cache.init(
26+
host_url=config.redis_url,
27+
prefix='speedtest-cache',
28+
response_header='X-Speedtest-Cache',
29+
ignore_arg_types=[Request, Response],
30+
)
831

932

1033
@app.get('/speedtest')
34+
@cache(expire=30)
1135
async def speedtest():
1236
logger.debug('Running speedtest')
1337
s: Speedtest = await Speedtest()

Diff for: data/appendonly.aof

Whitespace-only changes.

Diff for: docker-compose.yaml

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ services:
77
volumes:
88
- $PWD/data:/data
99
command: redis-server --appendonly yes --aof-use-rdb-preamble yes --dir /data
10-
10+
1111
app:
1212
restart: always
1313
build: .
@@ -18,3 +18,11 @@ services:
1818
depends_on:
1919
- redis
2020
command: /start-reload.sh
21+
22+
test:
23+
build: .
24+
volumes:
25+
- $PWD:/app
26+
depends_on:
27+
- redis
28+
entrypoint: "pytest"

Diff for: poetry.lock

+53-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ fastapi = "^0.61.0"
1010
uvicorn = "^0.11.8"
1111
requests = "^2.24.0"
1212
aiotestspeed = {git = "https://github.com/abrookins/aiotestspeed", rev = "master"}
13+
fastapi-redis-cache = "^0.2.2"
1314

1415
[tool.poetry.dev-dependencies]
1516
pytest = "^5.2"

Diff for: tests/conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
import pytest
44
from fastapi.testclient import TestClient
5+
from redis.utils import from_url
56

67
from app.main import app
8+
from app.main import config
79

810

911
@pytest.fixture(scope='module')
1012
def client() -> Generator:
1113
with TestClient(app) as c:
1214
yield c
15+
16+
17+
@pytest.fixture(scope='module')
18+
def redis() -> Generator:
19+
yield from_url(config.redis_url)

Diff for: tests/test_speedtest.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import json
2+
13
from fastapi.testclient import TestClient
4+
from redis import Redis
25

36

47
def test_speedtest(client: TestClient):
@@ -9,3 +12,15 @@ def test_speedtest(client: TestClient):
912

1013
for field in ('ping_ms', 'download_mbps', 'upload_mbps'):
1114
assert field in json
15+
16+
17+
def test_speedtest_cache(client: TestClient, redis: Redis):
18+
# prime the cache
19+
client.get('/speedtest')
20+
21+
cached = redis.get('speedtest-cache:app.main.speedtest()')
22+
assert cached is not None
23+
data = json.loads(cached)
24+
25+
for field in ('ping_ms', 'download_mbps', 'upload_mbps'):
26+
assert field in data

0 commit comments

Comments
 (0)