-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NUT-19: Cached Requests and Responses (#624)
* fast-api-cache setup * testing the cache * fix * still not working * asynccontextmanager * move test * use redis & custom caching setup (like CDK) * make format * poetry lock * fix format string + log when a cached response is found * log when a cahced response is found * fix tests * poetry lock * try tests on github * use docker compose * maybe we dont need docker * fix types * create_task instead of run * how about we start postgres * mint features * format * remove deprecated setex call * use global expiry for all cached routes * refactor feature map and set default to 1 week * refactor feature construction * Cache NUT-19 --------- Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
- Loading branch information
1 parent
ee90d84
commit 399c201
Showing
14 changed files
with
804 additions
and
391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
name: tests_redis_cache | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
python-version: | ||
default: "3.10.4" | ||
type: string | ||
poetry-version: | ||
default: "1.7.1" | ||
type: string | ||
mint-database: | ||
default: "" | ||
type: string | ||
os: | ||
default: "ubuntu-latest" | ||
type: string | ||
mint-only-deprecated: | ||
default: "false" | ||
type: string | ||
|
||
jobs: | ||
poetry: | ||
name: Run (db ${{ inputs.mint-database }}, deprecated api ${{ inputs.mint-only-deprecated }}) | ||
runs-on: ${{ inputs.os }} | ||
steps: | ||
- name: Start PostgreSQL service | ||
if: contains(inputs.mint-database, 'postgres') | ||
run: | | ||
docker run -d --name postgres -e POSTGRES_USER=cashu -e POSTGRES_PASSWORD=cashu -e POSTGRES_DB=cashu -p 5432:5432 postgres:latest | ||
until docker exec postgres pg_isready; do sleep 1; done | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Prepare environment | ||
uses: ./.github/actions/prepare | ||
with: | ||
python-version: ${{ inputs.python-version }} | ||
poetry-version: ${{ inputs.poetry-version }} | ||
|
||
- name: Start Redis service | ||
run: | | ||
docker compose -f docker/docker-compose.yml up -d redis | ||
- name: Run tests | ||
env: | ||
MINT_BACKEND_BOLT11_SAT: FakeWallet | ||
WALLET_NAME: test_wallet | ||
MINT_HOST: localhost | ||
MINT_PORT: 3337 | ||
MINT_TEST_DATABASE: ${{ inputs.mint-database }} | ||
TOR: false | ||
MINT_REDIS_CACHE_ENABLED: true | ||
MINT_REDIS_CACHE_URL: redis://localhost:6379 | ||
run: | | ||
poetry run pytest tests/test_mint_api_cache.py -v --cov=mint --cov-report=xml | ||
- name: Stop and clean up Docker Compose | ||
run: | | ||
docker compose -f docker/docker-compose.yml down | ||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ | |
HTLC_NUT = 14 | ||
MPP_NUT = 15 | ||
WEBSOCKETS_NUT = 17 | ||
CACHE_NUT = 19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import asyncio | ||
import functools | ||
import json | ||
|
||
from fastapi import Request | ||
from loguru import logger | ||
from pydantic import BaseModel | ||
from redis.asyncio import from_url | ||
from redis.exceptions import ConnectionError | ||
|
||
from ..core.errors import CashuError | ||
from ..core.settings import settings | ||
|
||
|
||
class RedisCache: | ||
expiry = settings.mint_redis_cache_ttl | ||
|
||
def __init__(self): | ||
if settings.mint_redis_cache_enabled: | ||
if settings.mint_redis_cache_url is None: | ||
raise CashuError("Redis cache url not provided") | ||
self.redis = from_url(settings.mint_redis_cache_url) | ||
asyncio.create_task(self.test_connection()) | ||
|
||
async def test_connection(self): | ||
# PING | ||
try: | ||
await self.redis.ping() | ||
logger.success("Connected to Redis caching server.") | ||
except ConnectionError as e: | ||
logger.error("Redis connection error.") | ||
raise e | ||
|
||
def cache(self): | ||
def passthrough(func): | ||
@functools.wraps(func) | ||
async def wrapper(*args, **kwargs): | ||
logger.trace(f"cache wrapper on route {func.__name__}") | ||
result = await func(*args, **kwargs) | ||
return result | ||
|
||
return wrapper | ||
|
||
def decorator(func): | ||
@functools.wraps(func) | ||
async def wrapper(request: Request, payload: BaseModel): | ||
logger.trace(f"cache wrapper on route {func.__name__}") | ||
key = request.url.path + payload.json() | ||
logger.trace(f"KEY: {key}") | ||
# Check if we have a value under this key | ||
if await self.redis.exists(key): | ||
logger.trace("Returning a cached response...") | ||
resp = await self.redis.get(key) | ||
if resp: | ||
return json.loads(resp) | ||
else: | ||
raise Exception(f"Found no cached response for key {key}") | ||
result = await func(request, payload) | ||
await self.redis.set(name=key, value=result.json(), ex=self.expiry) | ||
return result | ||
|
||
return wrapper | ||
|
||
return passthrough if not settings.mint_redis_cache_enabled else decorator | ||
|
||
async def disconnect(self): | ||
await self.redis.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.