update memory cache

This commit is contained in:
long2ice
2020-11-10 10:34:52 +08:00
parent ff8b4b385d
commit 0fd9e53e06
2 changed files with 5 additions and 5 deletions

View File

@@ -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:

View File

@@ -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())