Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
"sqlalchemy>=2.0.44",
"apitally[fastapi]>=0.22.3",
"auth0-fastapi-api>=1.0.0b5",
"fastapi-cache2[memcache]>=0.2.2",
"fastapi-cache2[redis,memcache]>=0.2.2",
"slowapi>=0.1.9",
"grpc-requests>=0.1.21",
"gunicorn>=25.3.0",
Expand Down
21 changes: 20 additions & 1 deletion src/quartz_api/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,26 @@ def _create_server(conf: ConfigTree) -> FastAPI:
},
)

FastAPICache.init(InMemoryBackend(), expire=120, prefix="fastapi-cache")
match conf.get_string("cache.backend"):
case "redis":
from fastapi_cache.backends.redis import RedisBackend
from redis import asyncio as aioredis

redis = aioredis.from_url(conf.get_string("cache.url"))
FastAPICache.init(RedisBackend(redis), expire=120, prefix="fastapi-cache")
case "memcache":
from aiomcache import Client as MemcachedClient
from fastapi_cache.backends.memcached import MemcachedBackend

host, _, port = conf.get_string("cache.url").rpartition(":")
mc = MemcachedClient(host or "localhost", int(port or 11211))
FastAPICache.init(MemcachedBackend(mc), expire=120, prefix="fastapi-cache")
case _:
FastAPICache.init(InMemoryBackend(), expire=120, prefix="fastapi-cache")
log.warning(
"using in-memory cache. NOT recommended for production"
" with multiple workers",
)

# Register rate limiter
server.state.limiter = ratelimit.limiter
Expand Down
7 changes: 7 additions & 0 deletions src/quartz_api/cmd/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ sentry {
cache {
refresh_token = ""
refresh_token = ${?CACHE_REFRESH_TOKEN}
// Cache backend: "inmemory", "redis", or "memcache"
// Use redis or memcache for production with multiple workers
backend = "inmemory"
backend = ${?CACHE_BACKEND}
// Connection URL for redis (redis://host:6379) or memcache (host:11211)
url = ""
url = ${?CACHE_URL}
}

// Apitally configuration
Expand Down
Loading