Exemplo n.º 1
0
    def get(self, key):
        now_timestamp = time.time()
        file_path_payload = self._file_path_payload(key)
        file_path_metadata = self._file_path_metadata(key)
        with self._get_rw_lock(key):
            if not os.path.exists(file_path_payload) or not os.path.exists(
                file_path_metadata
            ):
                return NO_VALUE
            if self.expiration_time is not None:
                last_modified_timestamp = utils._get_last_modified(
                    utils.stat_or_warn(file_path_payload)
                )
                if (
                    last_modified_timestamp
                    < now_timestamp - self.expiration_time.total_seconds()
                ):
                    return NO_VALUE

            with open(file_path_metadata, "rb") as i:
                metadata = pickle.load(i)

            file = io.open(file_path_payload, "rb")
            file.seek(metadata.original_file_offset, 0)
            if metadata.dogpile_metadata is not None:
                return CachedValue(file, metadata.dogpile_metadata)
            return file
Exemplo n.º 2
0
def raw_loads(value):
    """'
    we need to unpack the value and stash it into a CachedValue
    """
    if COMPAT_PY3:
        # this is True for backward compatibility
        value = msgpack.unpackb(value, use_list=False, raw=False)
    else:
        value = msgpack.unpackb(value, use_list=False)
    return CachedValue(value, {"ct": time.time(), "v": value_version})
Exemplo n.º 3
0
 def _loads(self, value, safe=True):
     """
     pickle maintained the `CachedValue` wrapper of the tuple
     msgpack does not, so it must be added back in.
    """
     try:
         value = msgpack.unpackb(value, use_list=False)
         return CachedValue(*value)
     except Exception:
         if safe:
             return NO_VALUE
         else:
             raise
Exemplo n.º 4
0
def my_loads(value):
    """'
    we need to unpack the value and stash it into a CachedValue
    we support strings in this version, because it's used in unit tests
    that require the ability to set/read raw data.
    we could disable that test, but this workaround supports it.
    """
    if COMPAT_PY3:
        # this is True for backward compatibility
        value = msgpack.unpackb(value, use_list=False, raw=False)
    else:
        value = msgpack.unpackb(value, use_list=False)
    if isinstance(value, tuple):
        return CachedValue(*value)
    return value
Exemplo n.º 5
0
 def value_decode(self, k, v):
     if not v or v is NO_VALUE:
         return NO_VALUE
     # v is returned as bytestring, so we need to `unicodify` on python < 3.6 before we can use json.loads
     v = json.loads(unicodify(v))
     if v.get('tool_cache_version', 0) != CURRENT_TOOL_CACHE_VERSION:
         return NO_VALUE
     for path, modtime in v['paths_and_modtimes'].items():
         if os.path.getmtime(path) != modtime:
             return NO_VALUE
     payload = get_tool_source(config_file=k,
                               xml_tree=etree.ElementTree(
                                   etree.fromstring(
                                       v['payload'].encode('utf-8'))),
                               macro_paths=v['macro_paths'])
     return CachedValue(metadata=v['metadata'], payload=payload)
Exemplo n.º 6
0
    def load(self, value):
        """
        Unserialize some data from JSON.

        If the JSON was compressed, decompress it first.

        Parameters:
            value: :py:data:`~dogpile.cache.api.NO_VALUE` or :term:`bytes`.

        Returns:
            :py:data:`~dogpile.cache.api.NO_VALUE` if *value* was
            :py:data:`~dogpile.cache.api.NO_VALUE`, or a
            :py:class:`~dogpile.cache.api.CachedValue`.
        """
        if value is NO_VALUE:
            return NO_VALUE
        if value.startswith(b'\0'):
            value = zlib.decompress(value[1:])
        payload, metadata = json.loads(value)
        return CachedValue(payload, metadata)
Exemplo n.º 7
0
    def test_log_is_hard_invalidated(self):

        reg = self._region()
        inv = mock.Mock(is_hard_invalidated=lambda val: True)
        with mock.patch(
            "dogpile.cache.region.log"
        ) as mock_log, mock.patch.object(reg, "region_invalidator", inv):
            is_(
                reg._is_cache_miss(
                    CachedValue("some value", {"v": value_version, "ct": 500}),
                    "some key",
                ),
                True,
            )
        eq_(
            mock_log.mock_calls,
            [
                mock.call.debug(
                    "Hard invalidation detected for key: %r", "some key"
                )
            ],
        )
Exemplo n.º 8
0
def cache_loads(value, loads=json.loads, cls=EnhancedJSONDecoder):
    return CachedValue(*loads(value, cls=cls))
Exemplo n.º 9
0
 def loads(cls, value):
     value = msgpack.unpackb(value, use_list=False)
     return CachedValue(value[0], {"ct": value[1], "v": value_version})
Exemplo n.º 10
0
 def loads(cls, value):
     """'unpack the value and stash it into a CachedValue"""
     value = msgpack.unpackb(value, use_list=False)
     return CachedValue(value, {"ct": time.time(), "v": value_version})
Exemplo n.º 11
0
def msgpack_loads(value):
    value = msgpack.unpackb(value, use_list=False)
    if isinstance(value, tuple):
        return CachedValue(*value)
    return value
Exemplo n.º 12
0
 def loads(cls, value):
     value = json.loads(value)
     return CachedValue(value[0], {"ct": value[1], "v": value_version})
Exemplo n.º 13
0
 def loads(cls, value):
     """'unpack the value and stash it into a CachedValue"""
     value = json.loads(value)
     return CachedValue(value, {"ct": time.time(), "v": value_version})
Exemplo n.º 14
0
 def loads(cls, value):
     """'unpack the value and stash it into a CachedValue"""
     value = json.loads(value)
     return CachedValue(value)
Exemplo n.º 15
0
 def wrap_value(self, value):
     return CachedValue(value, {'ct': time.time(), 'v': value_version})
Exemplo n.º 16
0
 def deserialize(self, bytestring, flag):
     payload, metadata = json.loads(bytestring)
     return CachedValue(payload, metadata)