add clear method

This commit is contained in:
long2ice
2020-11-03 18:08:06 +08:00
parent dc2ac9cc90
commit e483e0dc55
6 changed files with 40 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
import time
from copy import copy
from dataclasses import dataclass
from threading import Lock
from typing import Dict, Optional, Tuple
@@ -44,3 +45,16 @@ class InMemoryBackend(Backend):
async def set(self, key: str, value: str, expire: int = None):
with self._lock:
self._store[key] = Value(value, self._now + expire)
async def clear(self, namespace: str = None, key: str = None) -> int:
count = 0
if namespace:
keys = list(self._store.keys())
for key in keys:
if key.startswith(namespace):
del self._store[key]
count += 1
elif key:
del self._store[key]
count += 1
return count