pumpwood_communication.cache

Modules to manage a local disk cache for pumpwood requests.

1"""Modules to manage a local disk cache for pumpwood requests."""
2from .cache import PumpwoodCache, default_cache
3
4__docformat__ = "google"
5
6__all__ = [
7    PumpwoodCache, default_cache]
class PumpwoodCache:
10class PumpwoodCache:
11    """Class to implement local cache for Pumpwood Comunication requests."""
12
13    def __init__(self):
14        """__init__."""
15        self._size_limit = int(os.getenv(
16            'PUMPWOOD_COMUNICATION__CACHE_LIMIT_MB', 250)) * 1e8
17        self._expire_time = int(os.getenv(
18            'PUMPWOOD_COMUNICATION__CACHE_DEFAULT_EXPIRE', 60))
19        cache_path = '/tmp/pumpwood_cache/' # NOQA
20        self._cache = Cache(
21            directory=cache_path, cache_size=self._size_limit)
22
23    @classmethod
24    def _generate_hash(cls, hash_dict: dict) -> str:
25        """Generate a hash to be used to storage and retrieve cache.
26
27        It will use pumpJsonDump function from serializers to dump correctly
28        any complex data such as date, geometry and numpy.
29
30        Args:
31            hash_dict (dict):
32                A dictonary with information that will be used on hash.
33
34        Returns:
35            Return a hash that will be used as cache.
36        """
37        str_hash_dict = pumpJsonDump(hash_dict, sort_keys=True)
38        return hashlib.sha512(str_hash_dict).hexdigest()
39
40    def get(self, hash_dict: dict) -> Any:
41        """Get a value from cache.
42
43        Args:
44            hash_dict (dict):
45                A dictonary with information that will be used on hash.
46
47        Returns:
48            Return the cached value or None if not found.
49        """
50        hash_str = self._generate_hash(hash_dict=hash_dict)
51        return self._cache.get(hash_str)
52
53    def set(self, hash_dict: dict, value: Any, expire: int = None) -> bool:
54        """Set cache value.
55
56        Args:
57            hash_dict (dict):
58                A dictonary with information that will be used on hash.
59            value (Any):
60                Value that will be set on diskcache.
61            expire (int):
62                Number of seconds that will be considered as expirity time.
63
64        Returns:
65            Return a boolean value
66        """
67        expire_time = expire or self._expire_time
68        hash_str = self._generate_hash(hash_dict=hash_dict)
69        return self._cache.set(
70            hash_str, value=value, expire=expire_time)

Class to implement local cache for Pumpwood Comunication requests.

PumpwoodCache()
13    def __init__(self):
14        """__init__."""
15        self._size_limit = int(os.getenv(
16            'PUMPWOOD_COMUNICATION__CACHE_LIMIT_MB', 250)) * 1e8
17        self._expire_time = int(os.getenv(
18            'PUMPWOOD_COMUNICATION__CACHE_DEFAULT_EXPIRE', 60))
19        cache_path = '/tmp/pumpwood_cache/' # NOQA
20        self._cache = Cache(
21            directory=cache_path, cache_size=self._size_limit)

__init__.

def get(self, hash_dict: dict) -> Any:
40    def get(self, hash_dict: dict) -> Any:
41        """Get a value from cache.
42
43        Args:
44            hash_dict (dict):
45                A dictonary with information that will be used on hash.
46
47        Returns:
48            Return the cached value or None if not found.
49        """
50        hash_str = self._generate_hash(hash_dict=hash_dict)
51        return self._cache.get(hash_str)

Get a value from cache.

Arguments:
  • hash_dict (dict): A dictonary with information that will be used on hash.
Returns:

Return the cached value or None if not found.

def set(self, hash_dict: dict, value: Any, expire: int = None) -> bool:
53    def set(self, hash_dict: dict, value: Any, expire: int = None) -> bool:
54        """Set cache value.
55
56        Args:
57            hash_dict (dict):
58                A dictonary with information that will be used on hash.
59            value (Any):
60                Value that will be set on diskcache.
61            expire (int):
62                Number of seconds that will be considered as expirity time.
63
64        Returns:
65            Return a boolean value
66        """
67        expire_time = expire or self._expire_time
68        hash_str = self._generate_hash(hash_dict=hash_dict)
69        return self._cache.set(
70            hash_str, value=value, expire=expire_time)

Set cache value.

Arguments:
  • hash_dict (dict): A dictonary with information that will be used on hash.
  • value (Any): Value that will be set on diskcache.
  • expire (int): Number of seconds that will be considered as expirity time.
Returns:

Return a boolean value

PumpwoodCache object at 0x7844c9d96f30>