Skip to content
11 changes: 4 additions & 7 deletions dogpile/cache/backends/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import dbm
import os
import threading
from typing import cast
from typing import Dict
from typing import Literal
from typing import TypedDict
from typing import Union
Expand Down Expand Up @@ -152,18 +150,17 @@ def release_write_lock(self):
"""

def __init__(self, arguments: DBMBackendArguments):
_arguments = cast(Dict, arguments.copy())
self.filename = os.path.abspath(
os.path.normpath(_arguments["filename"])
os.path.normpath(arguments["filename"])
)
dir_, filename = os.path.split(self.filename)

self.lock_factory = _arguments.get("lock_factory", FileLock)
self.lock_factory = arguments.get("lock_factory", FileLock)
self._rw_lock = self._init_lock(
_arguments.get("rw_lockfile"), ".rw.lock", dir_, filename
arguments.get("rw_lockfile"), ".rw.lock", dir_, filename
)
self._dogpile_lock = self._init_lock(
_arguments.get("dogpile_lockfile"),
arguments.get("dogpile_lockfile"),
".dogpile.lock",
dir_,
filename,
Expand Down
75 changes: 34 additions & 41 deletions dogpile/cache/backends/memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import typing
from typing import Any
from typing import cast
from typing import Dict
from typing import Mapping
from typing import Optional
from typing import Sequence
Expand Down Expand Up @@ -171,16 +170,15 @@ class GenericMemcachedBackend(CacheBackend):
deserializer = None

def __init__(self, arguments: GenericMemcachedBackendArguments):
_arguments = cast(Dict, arguments.copy())
self._imports()
# using a plain threading.local here. threading.local
# automatically deletes the __dict__ when a thread ends,
# so the idea is that this is superior to pylibmc's
# own ThreadMappedPool which doesn't handle this
# automatically.
self.url = util.to_list(_arguments["url"])
self.distributed_lock = _arguments.get("distributed_lock", False)
self.lock_timeout = _arguments.get("lock_timeout", 0)
self.url = util.to_list(arguments["url"])
self.distributed_lock = arguments.get("distributed_lock", False)
self.lock_timeout = arguments.get("lock_timeout", 0)

def has_lock_timeout(self):
return self.lock_timeout != 0
Expand Down Expand Up @@ -281,17 +279,16 @@ class MemcacheArgs(GenericMemcachedBackend):
"""

def __init__(self, arguments: MemcachedArgsArguments):
_arguments = cast(Dict, arguments.copy())
self.min_compress_len = _arguments.get("min_compress_len", 0)
self.min_compress_len = arguments.get("min_compress_len", 0)

self.set_arguments = {}
if "memcached_expire_time" in _arguments:
self.set_arguments["time"] = _arguments["memcached_expire_time"]
if "min_compress_len" in _arguments:
self.set_arguments["min_compress_len"] = _arguments[
if "memcached_expire_time" in arguments:
self.set_arguments["time"] = arguments["memcached_expire_time"]
if "min_compress_len" in arguments:
self.set_arguments["min_compress_len"] = arguments[
"min_compress_len"
]
_arguments_super = cast(GenericMemcachedBackendArguments, _arguments)
_arguments_super = cast(GenericMemcachedBackendArguments, arguments)
super(MemcacheArgs, self).__init__(_arguments_super)


Expand Down Expand Up @@ -330,10 +327,9 @@ class PylibmcBackend(MemcacheArgs, GenericMemcachedBackend):
"""

def __init__(self, arguments):
_arguments = cast(Dict, arguments.copy())
self.binary = _arguments.get("binary", False)
self.behaviors = _arguments.get("behaviors", {})
_arguments_super = cast(MemcachedArgsArguments, _arguments)
self.binary = arguments.get("binary", False)
self.behaviors = arguments.get("behaviors", {})
_arguments_super = cast(MemcachedArgsArguments, arguments)
super(PylibmcBackend, self).__init__(_arguments_super)

def _imports(self):
Expand Down Expand Up @@ -383,10 +379,9 @@ class MemcachedBackend(MemcacheArgs, GenericMemcachedBackend):
"""

def __init__(self, arguments: MemcachedBackendArguments):
_arguments = cast(Dict, arguments.copy())
self.dead_retry = _arguments.get("dead_retry", 30)
self.socket_timeout = _arguments.get("socket_timeout", 3)
_arguments_super = cast(MemcachedArgsArguments, _arguments)
self.dead_retry = arguments.get("dead_retry", 30)
self.socket_timeout = arguments.get("socket_timeout", 3)
_arguments_super = cast(MemcachedArgsArguments, arguments)
super(MemcachedBackend, self).__init__(_arguments_super)

def _imports(self):
Expand Down Expand Up @@ -461,11 +456,10 @@ class BMemcachedBackend(GenericMemcachedBackend):
"""

def __init__(self, arguments: BMemcachedBackendArguments):
_arguments = cast(Dict, arguments.copy())
self.username = _arguments.get("username", None)
self.password = _arguments.get("password", None)
self.tls_context = _arguments.get("tls_context", None)
_arguments_super = cast(GenericMemcachedBackendArguments, _arguments)
self.username = arguments.get("username", None)
self.password = arguments.get("password", None)
self.tls_context = arguments.get("tls_context", None)
_arguments_super = cast(GenericMemcachedBackendArguments, arguments)
super(BMemcachedBackend, self).__init__(_arguments_super)

def _imports(self):
Expand Down Expand Up @@ -652,26 +646,25 @@ class PyMemcacheBackend(GenericMemcachedBackend):
""" # noqa E501

def __init__(self, arguments: PyMemcacheBackendArguments):
_arguments = cast(Dict, arguments.copy())
_arguments_super = cast(GenericMemcachedBackendArguments, _arguments)
_arguments_super = cast(GenericMemcachedBackendArguments, arguments)
super().__init__(_arguments_super)

self.serde = _arguments.get("serde", pymemcache.serde.pickle_serde)
self.default_noreply = _arguments.get("default_noreply", False)
self.tls_context = _arguments.get("tls_context", None)
self.socket_keepalive = _arguments.get("socket_keepalive", None)
self.enable_retry_client = _arguments.get("enable_retry_client", False)
self.retry_attempts = _arguments.get("retry_attempts", None)
self.retry_delay = _arguments.get("retry_delay", None)
self.retry_for = _arguments.get("retry_for", None)
self.do_not_retry_for = _arguments.get("do_not_retry_for", None)
self.hashclient_retry_attempts = _arguments.get(
self.serde = arguments.get("serde", pymemcache.serde.pickle_serde)
self.default_noreply = arguments.get("default_noreply", False)
self.tls_context = arguments.get("tls_context", None)
self.socket_keepalive = arguments.get("socket_keepalive", None)
self.enable_retry_client = arguments.get("enable_retry_client", False)
self.retry_attempts = arguments.get("retry_attempts", None)
self.retry_delay = arguments.get("retry_delay", None)
self.retry_for = arguments.get("retry_for", None)
self.do_not_retry_for = arguments.get("do_not_retry_for", None)
self.hashclient_retry_attempts = arguments.get(
"hashclient_retry_attempts", 2
)
self.hashclient_retry_timeout = _arguments.get(
self.hashclient_retry_timeout = arguments.get(
"hashclient_retry_timeout", 1
)
self.hashclient_dead_timeout = _arguments.get(
self.hashclient_dead_timeout = arguments.get(
"hashclient_dead_timeout", 60
)
if (
Expand All @@ -685,8 +678,8 @@ def __init__(self, arguments: PyMemcacheBackendArguments):
"will be ignored"
)
self.set_arguments = {}
if "memcached_expire_time" in _arguments:
self.set_arguments["expire"] = _arguments["memcached_expire_time"]
if "memcached_expire_time" in arguments:
self.set_arguments["expire"] = arguments["memcached_expire_time"]

def _imports(self):
global pymemcache
Expand Down
4 changes: 1 addition & 3 deletions dogpile/cache/backends/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from __future__ import annotations

from typing import Any
from typing import cast
from typing import Dict
from typing import TypedDict

Expand Down Expand Up @@ -61,8 +60,7 @@ class MemoryBackend(CacheBackend):
"""

def __init__(self, arguments: MemoryBackendArguments):
_arguments = cast(Dict, arguments.copy())
self._cache = _arguments.pop("cache_dict", {})
self._cache = arguments.get("cache_dict", {})

def get(self, key):
return self._cache.get(key, NO_VALUE)
Expand Down
1 change: 0 additions & 1 deletion dogpile/cache/backends/null.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class NullBackend(CacheBackend):
"""

def __init__(self, arguments: Dict[str, Any]):
# _arguments = typing.cast(typing.Dict, arguments.copy())
pass

def get_mutex(self, key):
Expand Down
49 changes: 23 additions & 26 deletions dogpile/cache/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,36 +166,35 @@ class RedisBackend(BytesBackend):
"""

def __init__(self, arguments: RedisBackendKwargs):
_arguments = cast(Dict, arguments.copy())
self._imports()
self.url = _arguments.pop("url", None)
self.host = _arguments.pop("host", "localhost")
self.username = _arguments.pop("username", None)
self.password = _arguments.pop("password", None)
self.port = _arguments.pop("port", 6379)
self.db = _arguments.pop("db", 0)
self.distributed_lock = _arguments.pop("distributed_lock", False)
self.socket_timeout = _arguments.pop("socket_timeout", None)
self.socket_connect_timeout = _arguments.pop(
self.url = arguments.get("url", None)
self.host = arguments.get("host", "localhost")
self.username = arguments.get("username", None)
self.password = arguments.get("password", None)
self.port = arguments.get("port", 6379)
self.db = arguments.get("db", 0)
self.distributed_lock = arguments.get("distributed_lock", False)
self.socket_timeout = arguments.get("socket_timeout", None)
self.socket_connect_timeout = arguments.get(
"socket_connect_timeout", None
)
self.socket_keepalive = _arguments.pop("socket_keepalive", False)
self.socket_keepalive_options = _arguments.pop(
self.socket_keepalive = arguments.get("socket_keepalive", False)
self.socket_keepalive_options = arguments.get(
"socket_keepalive_options", None
)
self.lock_timeout = _arguments.pop("lock_timeout", None)
self.lock_sleep = _arguments.pop("lock_sleep", 0.1)
self.thread_local_lock = _arguments.pop("thread_local_lock", True)
self.connection_kwargs = _arguments.pop("connection_kwargs", {})
self.lock_timeout = arguments.get("lock_timeout", None)
self.lock_sleep = arguments.get("lock_sleep", 0.1)
self.thread_local_lock = arguments.get("thread_local_lock", True)
self.connection_kwargs = arguments.get("connection_kwargs", {})

if self.distributed_lock and self.thread_local_lock:
warnings.warn(
"The Redis backend thread_local_lock parameter should be "
"set to False when distributed_lock is True"
)

self.redis_expiration_time = _arguments.pop("redis_expiration_time", 0)
self.connection_pool = _arguments.pop("connection_pool", None)
self.redis_expiration_time = arguments.get("redis_expiration_time", 0)
self.connection_pool = arguments.get("connection_pool", None)
self._create_client()

def _imports(self) -> None:
Expand Down Expand Up @@ -405,15 +404,14 @@ class RedisSentinelBackend(RedisBackend):
"""

def __init__(self, arguments: RedisSentinelBackendKwargs):
_arguments = cast(Dict, arguments.copy())
self.sentinels = _arguments.pop("sentinels", None)
self.service_name = _arguments.pop("service_name", "mymaster")
self.sentinel_kwargs = _arguments.pop("sentinel_kwargs", {})
self.sentinels = arguments.get("sentinels", None)
self.service_name = arguments.get("service_name", "mymaster")
self.sentinel_kwargs = arguments.get("sentinel_kwargs", {})
super().__init__(
arguments={
"distributed_lock": True,
"thread_local_lock": False,
**_arguments,
**arguments,
}
)

Expand Down Expand Up @@ -592,9 +590,8 @@ class RedisClusterBackend(RedisBackend):
"""

def __init__(self, arguments: RedisClusterBackendKwargs):
_arguments = cast(Dict, arguments.copy())
self.startup_nodes = _arguments.pop("startup_nodes", None)
_arguments_super = cast(RedisBackendKwargs, _arguments)
self.startup_nodes = arguments.get("startup_nodes", None)
_arguments_super = cast(RedisBackendKwargs, arguments)
super().__init__(_arguments_super)

def _imports(self) -> None:
Expand Down
Loading