2023-01-12 01:04:47 +00:00
|
|
|
# SPDX-FileCopyrightText: 2015 Eric Larson
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
"""
|
|
|
|
The cache object API for implementing caches. The default is a thread
|
|
|
|
safe in-memory dictionary.
|
|
|
|
"""
|
2023-09-17 19:22:54 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-01-12 01:04:47 +00:00
|
|
|
from threading import Lock
|
2023-09-17 19:22:54 +00:00
|
|
|
from typing import IO, TYPE_CHECKING, MutableMapping
|
2023-01-12 01:04:47 +00:00
|
|
|
|
2023-09-17 19:22:54 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from datetime import datetime
|
2023-01-12 01:04:47 +00:00
|
|
|
|
|
|
|
|
2023-09-17 19:22:54 +00:00
|
|
|
class BaseCache:
|
|
|
|
def get(self, key: str) -> bytes | None:
|
2023-01-12 01:04:47 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2023-09-17 19:22:54 +00:00
|
|
|
def set(
|
|
|
|
self, key: str, value: bytes, expires: int | datetime | None = None
|
|
|
|
) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2023-09-17 19:22:54 +00:00
|
|
|
def delete(self, key: str) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2023-09-17 19:22:54 +00:00
|
|
|
def close(self) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DictCache(BaseCache):
|
2023-09-17 19:22:54 +00:00
|
|
|
def __init__(self, init_dict: MutableMapping[str, bytes] | None = None) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
self.lock = Lock()
|
|
|
|
self.data = init_dict or {}
|
|
|
|
|
2023-09-17 19:22:54 +00:00
|
|
|
def get(self, key: str) -> bytes | None:
|
2023-01-12 01:04:47 +00:00
|
|
|
return self.data.get(key, None)
|
|
|
|
|
2023-09-17 19:22:54 +00:00
|
|
|
def set(
|
|
|
|
self, key: str, value: bytes, expires: int | datetime | None = None
|
|
|
|
) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
with self.lock:
|
|
|
|
self.data.update({key: value})
|
|
|
|
|
2023-09-17 19:22:54 +00:00
|
|
|
def delete(self, key: str) -> None:
|
2023-01-12 01:04:47 +00:00
|
|
|
with self.lock:
|
|
|
|
if key in self.data:
|
|
|
|
self.data.pop(key)
|
2023-01-06 11:47:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SeparateBodyBaseCache(BaseCache):
|
|
|
|
"""
|
|
|
|
In this variant, the body is not stored mixed in with the metadata, but is
|
|
|
|
passed in (as a bytes-like object) in a separate call to ``set_body()``.
|
|
|
|
|
|
|
|
That is, the expected interaction pattern is::
|
|
|
|
|
|
|
|
cache.set(key, serialized_metadata)
|
|
|
|
cache.set_body(key)
|
|
|
|
|
|
|
|
Similarly, the body should be loaded separately via ``get_body()``.
|
|
|
|
"""
|
2023-09-17 19:22:54 +00:00
|
|
|
|
|
|
|
def set_body(self, key: str, body: bytes) -> None:
|
2023-01-06 11:47:44 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2023-09-17 19:22:54 +00:00
|
|
|
def get_body(self, key: str) -> IO[bytes] | None:
|
2023-01-06 11:47:44 +00:00
|
|
|
"""
|
|
|
|
Return the body as file-like object.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError()
|