m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.9 KiB

6 months ago
  1. # SPDX-FileCopyrightText: 2015 Eric Larson
  2. #
  3. # SPDX-License-Identifier: Apache-2.0
  4. """
  5. The cache object API for implementing caches. The default is a thread
  6. safe in-memory dictionary.
  7. """
  8. from __future__ import annotations
  9. from threading import Lock
  10. from typing import IO, TYPE_CHECKING, MutableMapping
  11. if TYPE_CHECKING:
  12. from datetime import datetime
  13. class BaseCache:
  14. def get(self, key: str) -> bytes | None:
  15. raise NotImplementedError()
  16. def set(
  17. self, key: str, value: bytes, expires: int | datetime | None = None
  18. ) -> None:
  19. raise NotImplementedError()
  20. def delete(self, key: str) -> None:
  21. raise NotImplementedError()
  22. def close(self) -> None:
  23. pass
  24. class DictCache(BaseCache):
  25. def __init__(self, init_dict: MutableMapping[str, bytes] | None = None) -> None:
  26. self.lock = Lock()
  27. self.data = init_dict or {}
  28. def get(self, key: str) -> bytes | None:
  29. return self.data.get(key, None)
  30. def set(
  31. self, key: str, value: bytes, expires: int | datetime | None = None
  32. ) -> None:
  33. with self.lock:
  34. self.data.update({key: value})
  35. def delete(self, key: str) -> None:
  36. with self.lock:
  37. if key in self.data:
  38. self.data.pop(key)
  39. class SeparateBodyBaseCache(BaseCache):
  40. """
  41. In this variant, the body is not stored mixed in with the metadata, but is
  42. passed in (as a bytes-like object) in a separate call to ``set_body()``.
  43. That is, the expected interaction pattern is::
  44. cache.set(key, serialized_metadata)
  45. cache.set_body(key)
  46. Similarly, the body should be loaded separately via ``get_body()``.
  47. """
  48. def set_body(self, key: str, body: bytes) -> None:
  49. raise NotImplementedError()
  50. def get_body(self, key: str) -> IO[bytes] | None:
  51. """
  52. Return the body as file-like object.
  53. """
  54. raise NotImplementedError()