mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
update memory cache
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import time
|
import time
|
||||||
|
from asyncio import Lock
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from threading import Lock
|
|
||||||
from typing import Dict, Optional, Tuple
|
from typing import Dict, Optional, Tuple
|
||||||
|
|
||||||
from fastapi_cache.backends import Backend
|
from fastapi_cache.backends import Backend
|
||||||
@@ -29,20 +29,20 @@ class InMemoryBackend(Backend):
|
|||||||
return v
|
return v
|
||||||
|
|
||||||
async def get_with_ttl(self, key: str) -> Tuple[int, Optional[str]]:
|
async def get_with_ttl(self, key: str) -> Tuple[int, Optional[str]]:
|
||||||
with self._lock:
|
async with self._lock:
|
||||||
v = self._get(key)
|
v = self._get(key)
|
||||||
if v:
|
if v:
|
||||||
return v.ttl_ts - self._now, v.data
|
return v.ttl_ts - self._now, v.data
|
||||||
return 0, None
|
return 0, None
|
||||||
|
|
||||||
async def get(self, key: str) -> str:
|
async def get(self, key: str) -> str:
|
||||||
with self._lock:
|
async with self._lock:
|
||||||
v = self._get(key)
|
v = self._get(key)
|
||||||
if v:
|
if v:
|
||||||
return v.data
|
return v.data
|
||||||
|
|
||||||
async def set(self, key: str, value: str, expire: int = None):
|
async def set(self, key: str, value: str, expire: int = None):
|
||||||
with self._lock:
|
async with self._lock:
|
||||||
self._store[key] = Value(value, self._now + expire)
|
self._store[key] = Value(value, self._now + expire)
|
||||||
|
|
||||||
async def clear(self, namespace: str = None, key: str = None) -> int:
|
async def clear(self, namespace: str = None, key: str = None) -> int:
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ class MemcachedBackend(Backend):
|
|||||||
self.mcache = mcache
|
self.mcache = mcache
|
||||||
|
|
||||||
async def get_with_ttl(self, key: str) -> Tuple[int, str]:
|
async def get_with_ttl(self, key: str) -> Tuple[int, str]:
|
||||||
return 0, await self.mcache.get(key.encode())
|
return 3600, await self.mcache.get(key.encode())
|
||||||
|
|
||||||
async def get(self, key: str):
|
async def get(self, key: str):
|
||||||
return await self.mcache.get(key, key.encode())
|
return await self.mcache.get(key, key.encode())
|
||||||
|
|||||||
Reference in New Issue
Block a user