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:
 15class PumpwoodCache:
 16    """Class to implement local cache for Pumpwood Comunication requests."""
 17
 18    def __init__(self):
 19        """__init__."""
 20        self._cache = None
 21        self._size_limit = CACHE_LIMIT_MB
 22        self._expire_time = CACHE_DEFAULT_EXPIRE
 23        self._transaction_timeout = CACHE_TRANSACTION_TIMEOUT
 24        self._n_shards = CACHE_N_SHARDS
 25        self._enable = CACHE_ENABLE
 26        self._cache_path = (
 27            Path('/tmp/pumpwood_cache/') /
 28            CACHE_BASE_PATH)
 29        self._cache = None
 30
 31    def _create_cache_object(self):
 32        if self._cache is None:
 33            self._cache = FanoutCache(
 34                directory=self._cache_path, cache_size=self._size_limit,
 35                tag_index=True, timeout=self._transaction_timeout,
 36                shards=self._n_shards)
 37
 38    @classmethod
 39    def generate_hash(cls, hash_dict: dict) -> str:
 40        """Generate a hash to be used to storage and retrieve cache.
 41
 42        It will use pumpJsonDump function from serializers to dump correctly
 43        any complex data such as date, geometry and numpy.
 44
 45        Expose _generate_hash
 46
 47        Args:
 48            hash_dict (dict):
 49                A dictonary with information that will be used on hash.
 50
 51        Returns:
 52            Return a hash that will be used as cache.
 53        """
 54        return cls._generate_hash(hash_dict=hash_dict)
 55
 56    @classmethod
 57    def _generate_hash(cls, hash_dict: dict) -> str:
 58        """Generate a hash to be used to storage and retrieve cache.
 59
 60        It will use pumpJsonDump function from serializers to dump correctly
 61        any complex data such as date, geometry and numpy.
 62
 63        Args:
 64            hash_dict (dict):
 65                A dictonary with information that will be used on hash.
 66
 67        Returns:
 68            Return a hash that will be used as cache.
 69        """
 70        str_hash_dict = pumpJsonDump(hash_dict, sort_keys=True)
 71        return hashlib.sha512(str_hash_dict).hexdigest()
 72
 73    def clear(self) -> bool:
 74        """Invalidate cache.
 75
 76        Returns:
 77            True is ok.
 78        """
 79        self._create_cache_object()
 80        return self._cache.clear()
 81
 82    def evict(self, tag_dict: dict) -> bool:
 83        """Invalidate cache from a tag.
 84
 85        Returns:
 86            True is ok.
 87        """
 88        from pumpwood_communication.exceptions import PumpWoodCacheError
 89        self._create_cache_object()
 90
 91        if tag_dict is None:
 92            msg = (
 93                "At pumpwood_communication cache.evict tag_dict should not be "
 94                "'None'. To envict all databse use clear function.")
 95            raise PumpWoodCacheError(msg)
 96        hash_str = self.generate_hash(hash_dict=tag_dict)
 97        return self._cache.evict(hash_str)
 98
 99    def get(self, hash_dict: dict) -> Any:
100        """Get a value from cache.
101
102        Args:
103            hash_dict (dict):
104                A dictonary with information that will be used on hash.
105
106        Returns:
107            Return the cached value or None if not found.
108        """
109        self._create_cache_object()
110        if not self._enable:
111            logger.info("Get cache not enable")
112            return None
113
114        # It cache time is set to 0, than disable cache,
115        # this is usefull for testing
116        if self._expire_time == 0:
117            return None
118
119        hash_str = self.generate_hash(hash_dict=hash_dict)
120        return self._cache.get(hash_str)
121
122    def set(self, hash_dict: dict, value: Any, expire: int = None,
123            tag_dict: dict = None) -> bool:
124        """Set cache value.
125
126        Args:
127            hash_dict (dict):
128                A dictonary with information that will be used on hash.
129            value (Any):
130                Value that will be set on diskcache.
131            expire (int):
132                Number of seconds that will be considered as expirity time.
133            tag_dict (dict):
134                Optional parameter to set a tag to cache. Tagged cache can be
135                envicted together using envict function.
136
137        Returns:
138            Return a boolean value
139        """
140        self._create_cache_object()
141        if not self._enable:
142            logger.info("Set cache not enable")
143            return True
144
145        if hash_dict is None:
146            msg = (
147                "At pumpwood_communication cache.set hash_dict should not be "
148                "'None'")
149            raise PumpWoodCacheError(msg)
150        expire_time = expire or self._expire_time
151        # Do not store cache if expire_time == 0
152        if expire_time == 0:
153            return True
154
155        hash_str = self.generate_hash(hash_dict=hash_dict)
156        tag_str = None
157        if tag_dict is not None:
158            tag_str = self.generate_hash(hash_dict=tag_dict)
159
160        try:
161            return self._cache.set(
162                hash_str, value=value, expire=expire_time,
163                tag=tag_str)
164        except Timeout:
165            # in case of deadlock
166            warning_msg = (
167                "Cache on deadlock, it was not possible " +
168                "to set information for key %s" % (hash_str))
169            logger.warning(warning_msg)
170            return False
171        except Exception as e:
172            msg = (
173                'Error when retrieving cache not associated with Timeout. '
174                '{error}')
175            raise PumpWoodCacheError(
176                message=msg, payload={'error': str(e)})

Class to implement local cache for Pumpwood Comunication requests.

PumpwoodCache()
18    def __init__(self):
19        """__init__."""
20        self._cache = None
21        self._size_limit = CACHE_LIMIT_MB
22        self._expire_time = CACHE_DEFAULT_EXPIRE
23        self._transaction_timeout = CACHE_TRANSACTION_TIMEOUT
24        self._n_shards = CACHE_N_SHARDS
25        self._enable = CACHE_ENABLE
26        self._cache_path = (
27            Path('/tmp/pumpwood_cache/') /
28            CACHE_BASE_PATH)
29        self._cache = None

__init__.

@classmethod
def generate_hash(cls, hash_dict: dict) -> str:
38    @classmethod
39    def generate_hash(cls, hash_dict: dict) -> str:
40        """Generate a hash to be used to storage and retrieve cache.
41
42        It will use pumpJsonDump function from serializers to dump correctly
43        any complex data such as date, geometry and numpy.
44
45        Expose _generate_hash
46
47        Args:
48            hash_dict (dict):
49                A dictonary with information that will be used on hash.
50
51        Returns:
52            Return a hash that will be used as cache.
53        """
54        return cls._generate_hash(hash_dict=hash_dict)

Generate a hash to be used to storage and retrieve cache.

It will use pumpJsonDump function from serializers to dump correctly any complex data such as date, geometry and numpy.

Expose _generate_hash

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

Return a hash that will be used as cache.

def clear(self) -> bool:
73    def clear(self) -> bool:
74        """Invalidate cache.
75
76        Returns:
77            True is ok.
78        """
79        self._create_cache_object()
80        return self._cache.clear()

Invalidate cache.

Returns:

True is ok.

def evict(self, tag_dict: dict) -> bool:
82    def evict(self, tag_dict: dict) -> bool:
83        """Invalidate cache from a tag.
84
85        Returns:
86            True is ok.
87        """
88        from pumpwood_communication.exceptions import PumpWoodCacheError
89        self._create_cache_object()
90
91        if tag_dict is None:
92            msg = (
93                "At pumpwood_communication cache.evict tag_dict should not be "
94                "'None'. To envict all databse use clear function.")
95            raise PumpWoodCacheError(msg)
96        hash_str = self.generate_hash(hash_dict=tag_dict)
97        return self._cache.evict(hash_str)

Invalidate cache from a tag.

Returns:

True is ok.

def get(self, hash_dict: dict) -> Any:
 99    def get(self, hash_dict: dict) -> Any:
100        """Get a value from cache.
101
102        Args:
103            hash_dict (dict):
104                A dictonary with information that will be used on hash.
105
106        Returns:
107            Return the cached value or None if not found.
108        """
109        self._create_cache_object()
110        if not self._enable:
111            logger.info("Get cache not enable")
112            return None
113
114        # It cache time is set to 0, than disable cache,
115        # this is usefull for testing
116        if self._expire_time == 0:
117            return None
118
119        hash_str = self.generate_hash(hash_dict=hash_dict)
120        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, tag_dict: dict = None) -> bool:
122    def set(self, hash_dict: dict, value: Any, expire: int = None,
123            tag_dict: dict = None) -> bool:
124        """Set cache value.
125
126        Args:
127            hash_dict (dict):
128                A dictonary with information that will be used on hash.
129            value (Any):
130                Value that will be set on diskcache.
131            expire (int):
132                Number of seconds that will be considered as expirity time.
133            tag_dict (dict):
134                Optional parameter to set a tag to cache. Tagged cache can be
135                envicted together using envict function.
136
137        Returns:
138            Return a boolean value
139        """
140        self._create_cache_object()
141        if not self._enable:
142            logger.info("Set cache not enable")
143            return True
144
145        if hash_dict is None:
146            msg = (
147                "At pumpwood_communication cache.set hash_dict should not be "
148                "'None'")
149            raise PumpWoodCacheError(msg)
150        expire_time = expire or self._expire_time
151        # Do not store cache if expire_time == 0
152        if expire_time == 0:
153            return True
154
155        hash_str = self.generate_hash(hash_dict=hash_dict)
156        tag_str = None
157        if tag_dict is not None:
158            tag_str = self.generate_hash(hash_dict=tag_dict)
159
160        try:
161            return self._cache.set(
162                hash_str, value=value, expire=expire_time,
163                tag=tag_str)
164        except Timeout:
165            # in case of deadlock
166            warning_msg = (
167                "Cache on deadlock, it was not possible " +
168                "to set information for key %s" % (hash_str))
169            logger.warning(warning_msg)
170            return False
171        except Exception as e:
172            msg = (
173                'Error when retrieving cache not associated with Timeout. '
174                '{error}')
175            raise PumpWoodCacheError(
176                message=msg, payload={'error': str(e)})

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.
  • tag_dict (dict): Optional parameter to set a tag to cache. Tagged cache can be envicted together using envict function.
Returns:

Return a boolean value

PumpwoodCache object at 0x74bc22daffe0>