Bases: BaseKV, BaseDB, KVDB
DiskCache Key-Value DB.
Source code in src/msgflux/data/dbs/providers/diskcache.py
| @register_db
class DiskCacheKVDB(BaseKV, BaseDB, KVDB):
"""DiskCache Key-Value DB."""
provider = "diskcache"
def __init__(self, *, ttl: Optional[int] = 3600, hash_key: Optional[bool] = True):
"""Args:
ttl:
The time-to-live (TTL) for each cache entry in seconds.
hash_key:
Whether to hash the keys before storing them in the cache.
"""
if Cache is None:
raise ImportError(
"`diskcache` client is not available. Install with "
"`pip install diskcache`"
)
self.hash_key = hash_key
self.ttl = ttl
self._initialize()
def _initialize(self):
self.client = Cache(timeout=1)
def add(self, documents: Union[List[Dict[str, Any]], Dict[str, Any]]):
if not isinstance(documents, list):
documents = [documents]
for document in documents:
for k, v in document.items():
encoded_k = convert_str_to_hash(k) if self.hash_key else k
encoded_v = msgspec.msgpack.encode(v)
self.client.set(encoded_k, encoded_v, expire=self.ttl)
|
hash_key
instance-attribute
provider
class-attribute
instance-attribute
__init__
__init__(*, ttl=3600, hash_key=True)
ttl:
The time-to-live (TTL) for each cache entry in seconds.
hash_key:
Whether to hash the keys before storing them in the cache.
Source code in src/msgflux/data/dbs/providers/diskcache.py
| def __init__(self, *, ttl: Optional[int] = 3600, hash_key: Optional[bool] = True):
"""Args:
ttl:
The time-to-live (TTL) for each cache entry in seconds.
hash_key:
Whether to hash the keys before storing them in the cache.
"""
if Cache is None:
raise ImportError(
"`diskcache` client is not available. Install with "
"`pip install diskcache`"
)
self.hash_key = hash_key
self.ttl = ttl
self._initialize()
|
add
Source code in src/msgflux/data/dbs/providers/diskcache.py
| def add(self, documents: Union[List[Dict[str, Any]], Dict[str, Any]]):
if not isinstance(documents, list):
documents = [documents]
for document in documents:
for k, v in document.items():
encoded_k = convert_str_to_hash(k) if self.hash_key else k
encoded_v = msgspec.msgpack.encode(v)
self.client.set(encoded_k, encoded_v, expire=self.ttl)
|