Skip to content

Commit

Permalink
Don't call pickle on memory cache
Browse files Browse the repository at this point in the history
After this pull request sqlalchemy/dogpile.cache#191
Pickle is called on memory cache
  • Loading branch information
sbrunner committed Jun 13, 2022
1 parent caa2e3e commit d70a2ee
Showing 1 changed file with 16 additions and 41 deletions.
57 changes: 16 additions & 41 deletions geoportal/c2cgeoportal_geoportal/lib/caching.py
Expand Up @@ -34,7 +34,8 @@
import pyramid.request
import pyramid.response
import sqlalchemy.ext.declarative
from dogpile.cache.api import NO_VALUE
from dogpile.cache.api import NO_VALUE, CacheBackend
from dogpile.cache.backends.memory import MemoryBackend
from dogpile.cache.backends.redis import RedisBackend, RedisSentinelBackend
from dogpile.cache.region import CacheRegion, make_region
from dogpile.cache.util import sha1_mangle_key
Expand Down Expand Up @@ -123,72 +124,46 @@ def invalidate_region(region: Optional[str] = None) -> None:
get_region(region).invalidate()


class HybridRedisBackend(RedisBackend): # type: ignore
class HybridRedisBackend(CacheBackend): # type: ignore
"""A memory and redis backend."""

def __init__(self, arguments: Dict[str, Any]):
self._cache: Dict[str, SerializedReturnType] = arguments.pop("cache_dict", {})
self._use_memory_cache = not arguments.pop("disable_memory_cache", False)

super().__init__(arguments)
self._memory = MemoryBackend({"cache_dict": arguments.pop("cache_dict", {})})
self._redis = RedisBackend(arguments)

def get(self, key: str) -> SerializedReturnType:
value = self._cache.get(key, NO_VALUE)
value = self._memory.get(key)
if value == NO_VALUE:
value = super().get(sha1_mangle_key(key.encode()))
value = self._redis.get(sha1_mangle_key(key.encode()))
if value != NO_VALUE and self._use_memory_cache:
self._cache[key] = value
self._memory.set(value)
return value

def get_multi(self, keys: List[str]) -> List[SerializedReturnType]:
return [self.get(key) for key in keys]

def set(self, key: str, value: SerializedReturnType) -> None:
if self._use_memory_cache:
self._cache[key] = value
super().set(sha1_mangle_key(key.encode()), value)
self._memory.set(key, value)
self._redis.set(sha1_mangle_key(key.encode()), value)

def set_multi(self, mapping: Dict[str, SerializedReturnType]) -> None:
for key, value in mapping.items():
self.set(key, value)

def delete(self, key: str) -> None:
self._cache.pop(key, None)
super().delete(key)
self._memory.delete(key)
self._redis.delete(key)


class HybridRedisSentinelBackend(RedisSentinelBackend): # type: ignore
"""A memory and redis sentinel backend."""
class HybridRedisSentinelBackend(HybridRedisBackend): # type: ignore
"""A memory and redis backend."""

def __init__(self, arguments: Dict[str, Any]):
self._cache: Dict[str, SerializedReturnType] = arguments.pop("cache_dict", {})
self._use_memory_cache = not arguments.pop("disable_memory_cache", False)

super().__init__(arguments)

def get(self, key: str) -> SerializedReturnType:
value = self._cache.get(key, NO_VALUE)
if value == NO_VALUE:
value = super().get(sha1_mangle_key(key.encode()))
if value != NO_VALUE and self._use_memory_cache:
self._cache[key] = value
return value

def get_multi(self, keys: List[str]) -> List[SerializedReturnType]:
return [self.get(key) for key in keys]

def set(self, key: str, value: SerializedReturnType) -> None:
if self._use_memory_cache:
self._cache[key] = value
super().set(sha1_mangle_key(key.encode()), value)

def set_multi(self, mapping: Dict[str, SerializedReturnType]) -> None:
for key, value in mapping.items():
self.set(key, value)

def delete(self, key: str) -> None:
self._cache.pop(key, None)
super().delete(key)
self._memory = MemoryBackend({"cache_dict": arguments.pop("cache_dict", {})})
self._redis = RedisSentinelBackend(arguments)


class Cache(Enum):
Expand Down

0 comments on commit d70a2ee

Please sign in to comment.