-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
56 lines (48 loc) · 1.56 KB
/
Copy pathbase.py
File metadata and controls
56 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# fastapi_cacheable/backend/base.py
"""
Abstract base class for cache backends.
Defines the interface that all cache backends must implement.
"""
from abc import ABC, abstractmethod
from typing import Any, Optional
class BaseCacheBackend(ABC):
"""
Abstract base class for cache backends.
All cache backends must implement this interface.
"""
@abstractmethod
async def get(self, key: str) -> Optional[Any]:
"""
Retrieve a value from the cache by its key.
:param key: The key to look up in the cache.
:return: The cached value, or None if not found.
"""
raise NotImplementedError
@abstractmethod
async def set(
self,
key: str,
value: Any,
ttl: Optional[int] = None,
) -> None:
"""
Set a value in the cache with an optional time-to-live (TTL).
:param key: The key under which to store the value.
:param value: The value to store in the cache.
:param ttl: Optional time-to-live in seconds.
"""
raise NotImplementedError
@abstractmethod
async def delete(self, key: str) -> None:
"""
Delete a value from the cache by its key.
:param key: The key to delete from the cache.
"""
raise NotImplementedError
@abstractmethod
async def clear(self, namespace: Optional[str] = None) -> None:
"""
Clear the cache, optionally within a specific namespace.
:param namespace: Optional namespace to clear.
"""
raise NotImplementedError