Mirrored from https://github.com/Camo67/OCIU-npo.git
Synced Nov 21, 06:00 UTC
fix/ociu-remove-placeholder-content
| 1 | import os |
| 2 | import json |
| 3 | import httpx |
| 4 | from typing import Optional |
| 5 | |
| 6 | class RedisCache: |
| 7 | def __init__(self): |
| 8 | self.url = os.getenv("UPSTASH_REDIS_REST_URL") |
| 9 | self.token = os.getenv("UPSTASH_REDIS_REST_TOKEN") |
| 10 | |
| 11 | async def get(self, key: str) -> Optional[str]: |
| 12 | if not self.url or not self.token: |
| 13 | return None |
| 14 | |
| 15 | async with httpx.AsyncClient() as client: |
| 16 | resp = await client.get( |
| 17 | f"{self.url}/get/{key}", |
| 18 | headers={"Authorization": f"Bearer {self.token}"} |
| 19 | ) |
| 20 | if resp.status_code == 200: |
| 21 | return resp.json().get("result") |
| 22 | return None |
| 23 | |
| 24 | async def set(self, key: str, value: str, ttl: int = 3600): |
| 25 | if not self.url or not self.token: |
| 26 | return |
| 27 | |
| 28 | async with httpx.AsyncClient() as client: |
| 29 | await client.get( |
| 30 | f"{self.url}/set/{key}/{value}/ex/{ttl}", |
| 31 | headers={"Authorization": f"Bearer {self.token}"} |
| 32 | ) |
| 33 | |
| 34 | redis_cache = RedisCache() |