Example #1
0
    def wrapped(method, self, *args, **kwargs):
        kwargs = function_args_as_kwargs(method, self, *args, **kwargs)
        kwargs.pop('self')

        _key = key(self, kwargs)
        _namespace = namespace(self, kwargs)
        _cache = cache(self, kwargs)
        _use_cache = use_cache(self, kwargs)
        _renew = renew(self, kwargs)
        _serializer = serializer(self, kwargs)
        _metadata = metadata(self, kwargs)

        if _cache is None or not _use_cache:
            return method(self, **kwargs)

        if _cache.has_key(
                _key, namespace=_namespace
        ) and not _renew:  # noqa: has_key is not of a dictionary here
            try:
                return _cache.get(_key,
                                  namespace=_namespace,
                                  serializer=_serializer)
            except:
                logger.warning(
                    "Failed to retrieve results from cache. Renewing the cache..."
                )
                if config.cache_fail_hard:
                    six.reraise(*sys.exc_info())
            finally:
                logger.caveat('Loaded from cache')

        # Renewing/creating cache
        value = method(self, **kwargs)
        if value is None:
            logger.warning("Method value returned None. Not saving to cache.")
            return

        try:
            _cache.set(_key,
                       value=value,
                       namespace=_namespace,
                       serializer=_serializer,
                       metadata=_metadata)
            # Return from cache every time, just in case serialization operation was
            # destructive (e.g. reading from cursors)
            return _cache.get(_key,
                              namespace=_namespace,
                              serializer=_serializer)
        except:
            logger.warning(
                "Failed to save results to cache. If needed, please save them manually."
            )
            if config.cache_fail_hard:
                six.reraise(*sys.exc_info())
            return value  # As a last resort, return value object (which could be mutated by serialization).
Example #2
0
    def wrapped(method, self, *args, **kwargs):
        kwargs = function_args_as_kwargs(method, self, *args, **kwargs)
        kwargs.pop('self')

        _key = key(self, kwargs)
        _namespace = namespace(self, kwargs)
        _cache = cache(self, kwargs)
        _use_cache = use_cache(self, kwargs)
        _renew = renew(self, kwargs)
        _serializer = serializer(self, kwargs)
        _metadata = metadata(self, kwargs)

        if _cache is None or not _use_cache:
            return method(self, **kwargs)

        if _renew or not _cache.has_key(_key, namespace=_namespace):  # noqa: has_key is not of a dictionary here
            value = method(self, **kwargs)
            if value is None:
                logger.warning("Method value returned None. Not saving to cache.")
                return

            try:
                _cache.set(
                    _key,
                    value=value,
                    namespace=_namespace,
                    serializer=_serializer,
                    metadata=_metadata
                )
            except:
                logger.warning("Failed to save results to cache. If needed, please save them manually.")
                if config.cache_fail_hard:
                    six.reraise(*sys.exc_info())
        else:
            logger.caveat('Loaded from cache')

        # Return from cache every time, just in case serialization operation was
        # destructive (e.g. reading from cursors)
        return _cache.get(
            _key,
            namespace=_namespace,
            serializer=_serializer
        )
Example #3
0
    def wrapped(method, self, *args, **kwargs):
        if six.PY3 and not hasattr(sys, 'pypy_version_info'):
            arguments = inspect.signature(method).parameters.keys()
        else:
            arguments = inspect.getargspec(method).args
        kwargs.update(dict(zip(list(arguments)[1:], args)))

        _cache = cache(self)
        _use_cache = use_cache(self, kwargs)
        _renew = renew(self, kwargs)
        _format = format(self, kwargs)

        if _cache is None or not _use_cache:
            return method(self, **kwargs)

        _id_duct = id_duct(self, kwargs)
        _id_str = id_str(self, kwargs)

        if _renew or not _cache.has_key(
                _id_duct,
                _id_str):  # noqa: has_key is not of a dictionary here
            value = method(self, **kwargs)
            try:
                _cache.set(id_duct=_id_duct,
                           id_str=_id_str,
                           value=value,
                           serializer=serializer(_format))
            except Exception:  # Remove any lingering (perhaps partial) cache files
                _cache.clear(id_duct=_id_duct, id_str=_id_str)
                logger.warning(
                    "Failed to save results to cache. If needed, please save them manually."
                )
                if config.cache_fail_hard:
                    raise

            return value

        logger.caveat('Loaded from cache')

        return _cache.get(id_duct=_id_duct,
                          id_str=_id_str,
                          deserializer=deserializer(_format))