2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
Proxy Backends
|
|
|
|
------------------
|
|
|
|
|
|
|
|
Provides a utility and a decorator class that allow for modifying the behavior
|
|
|
|
of different backends without altering the class itself or having to extend the
|
|
|
|
base backend.
|
|
|
|
|
|
|
|
.. versionadded:: 0.5.0 Added support for the :class:`.ProxyBackend` class.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
from typing import Mapping
|
|
|
|
from typing import Optional
|
|
|
|
from typing import Sequence
|
|
|
|
|
|
|
|
from .api import BackendFormatted
|
|
|
|
from .api import BackendSetType
|
2018-09-17 00:27:00 +00:00
|
|
|
from .api import CacheBackend
|
2022-01-24 04:07:52 +00:00
|
|
|
from .api import CacheMutex
|
|
|
|
from .api import KeyType
|
|
|
|
from .api import SerializedReturnType
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProxyBackend(CacheBackend):
|
|
|
|
"""A decorator class for altering the functionality of backends.
|
|
|
|
|
|
|
|
Basic usage::
|
|
|
|
|
|
|
|
from dogpile.cache import make_region
|
|
|
|
from dogpile.cache.proxy import ProxyBackend
|
|
|
|
|
|
|
|
class MyFirstProxy(ProxyBackend):
|
|
|
|
def get(self, key):
|
|
|
|
# ... custom code goes here ...
|
|
|
|
return self.proxied.get(key)
|
|
|
|
|
|
|
|
def set(self, key, value):
|
|
|
|
# ... custom code goes here ...
|
|
|
|
self.proxied.set(key)
|
|
|
|
|
|
|
|
class MySecondProxy(ProxyBackend):
|
|
|
|
def get(self, key):
|
|
|
|
# ... custom code goes here ...
|
|
|
|
return self.proxied.get(key)
|
|
|
|
|
|
|
|
|
|
|
|
region = make_region().configure(
|
|
|
|
'dogpile.cache.dbm',
|
|
|
|
expiration_time = 3600,
|
|
|
|
arguments = {
|
|
|
|
"filename":"/path/to/cachefile.dbm"
|
|
|
|
},
|
|
|
|
wrap = [ MyFirstProxy, MySecondProxy ]
|
|
|
|
)
|
|
|
|
|
|
|
|
Classes that extend :class:`.ProxyBackend` can be stacked
|
|
|
|
together. The ``.proxied`` property will always
|
|
|
|
point to either the concrete backend instance or
|
|
|
|
the next proxy in the chain that a method can be
|
|
|
|
delegated towards.
|
|
|
|
|
|
|
|
.. versionadded:: 0.5.0
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def __init__(self, *arg, **kw):
|
|
|
|
pass
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def wrap(self, backend: CacheBackend) -> "ProxyBackend":
|
|
|
|
"""Take a backend as an argument and setup the self.proxied property.
|
2018-09-17 00:27:00 +00:00
|
|
|
Return an object that be used as a backend by a :class:`.CacheRegion`
|
|
|
|
object.
|
2022-01-24 04:07:52 +00:00
|
|
|
"""
|
|
|
|
assert isinstance(backend, CacheBackend) or isinstance(
|
|
|
|
backend, ProxyBackend
|
|
|
|
)
|
2018-09-17 00:27:00 +00:00
|
|
|
self.proxied = backend
|
|
|
|
return self
|
|
|
|
|
|
|
|
#
|
|
|
|
# Delegate any functions that are not already overridden to
|
|
|
|
# the proxies backend
|
|
|
|
#
|
2022-01-24 04:07:52 +00:00
|
|
|
def get(self, key: KeyType) -> BackendFormatted:
|
2018-09-17 00:27:00 +00:00
|
|
|
return self.proxied.get(key)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def set(self, key: KeyType, value: BackendSetType) -> None:
|
2018-09-17 00:27:00 +00:00
|
|
|
self.proxied.set(key, value)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def delete(self, key: KeyType) -> None:
|
2018-09-17 00:27:00 +00:00
|
|
|
self.proxied.delete(key)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def get_multi(self, keys: Sequence[KeyType]) -> Sequence[BackendFormatted]:
|
2018-09-17 00:27:00 +00:00
|
|
|
return self.proxied.get_multi(keys)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def set_multi(self, mapping: Mapping[KeyType, BackendSetType]) -> None:
|
2018-09-17 00:27:00 +00:00
|
|
|
self.proxied.set_multi(mapping)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def delete_multi(self, keys: Sequence[KeyType]) -> None:
|
2018-09-17 00:27:00 +00:00
|
|
|
self.proxied.delete_multi(keys)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def get_mutex(self, key: KeyType) -> Optional[CacheMutex]:
|
2018-09-17 00:27:00 +00:00
|
|
|
return self.proxied.get_mutex(key)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
def get_serialized(self, key: KeyType) -> SerializedReturnType:
|
|
|
|
return self.proxied.get_serialized(key)
|
|
|
|
|
|
|
|
def get_serialized_multi(
|
|
|
|
self, keys: Sequence[KeyType]
|
|
|
|
) -> Sequence[SerializedReturnType]:
|
|
|
|
return self.proxied.get_serialized_multi(keys)
|
|
|
|
|
|
|
|
def set_serialized(self, key: KeyType, value: bytes) -> None:
|
|
|
|
self.proxied.set_serialized(key, value)
|
|
|
|
|
|
|
|
def set_serialized_multi(self, mapping: Mapping[KeyType, bytes]) -> None:
|
|
|
|
self.proxied.set_serialized_multi(mapping)
|