Example #1
0
        def wrapped(request, *args, **kwargs):
            if not request_gatekeeper(request, *args, **kwargs):
                return func(request, *args, **kwargs)

            key_args = key

            # Default key.
            if not key_args:
                # Don't naively add the `request` arg to the cache key.
                key_args = _make_key_args_from_function(func, *args, **kwargs)

                # Only add specific parts of the `request` object to the key.
                key_args.extend(chain.from_iterable(request.GET.items()))
                key_args.append(request.method)

                # Current language.
                key_args.append(translation.get_language())

                # Current site, if available.
                key_args.append(getattr(settings, 'SITE_ID', None))

            if add_user_to_key and request.user.is_authenticated():
                key_args = chain(key_arg_iterator(key_args, max_depth=0),
                                 [request.user.id])

            # Serialize the key.
            # Add `request` to `args` since _make_key wants all func args in it.
            _args = (request, ) + args

            _namespace = None
            if namespace is not None:
                _namespace = _make_key_for_func(namespace,
                                                _args,
                                                kwargs,
                                                skip_prefix=True)

            _key = _make_key_for_func(key_args,
                                      _args,
                                      kwargs,
                                      namespace=_namespace)

            resp = None
            val = cache.cache.get(_key)
            logger.debug(u'getting cache from {}: {}'.format(_key, val))

            if val is None:
                resp = func(request, *args, **kwargs)

                if response_gatekeeper(resp):
                    set_cache(_key, serializer(resp), timeout)
            else:
                resp = cached_response_wrapper(val)

            return resp
Example #2
0
        def wrapped(request, *args, **kwargs):
            if not request_gatekeeper(request, *args, **kwargs):
                return func(request, *args, **kwargs)

            key_args = key

            # Default key.
            if not key_args:
                # Don't naively add the `request` arg to the cache key.
                key_args = _make_key_args_from_function(func, *args, **kwargs)

                # Only add specific parts of the `request` object to the key.
                key_args.extend(chain.from_iterable(request.GET.items()))
                key_args.append(request.method)

                # Current language.
                key_args.append(translation.get_language())

                # Current site, if available.
                key_args.append(getattr(settings, 'SITE_ID', None))

            if add_user_to_key and request.user.is_authenticated():
                key_args = chain(key_arg_iterator(key_args, max_depth=0),
                                 [request.user.id])

            # Serialize the key.
            # Add `request` to `args` since _make_key wants all func args in it.
            _args = (request,) + args

            _namespace = None
            if namespace is not None:
                _namespace = _make_key_for_func(namespace, _args, kwargs,
                                                skip_prefix=True)

            _key = _make_key_for_func(key_args, _args, kwargs, namespace=_namespace)

            resp = None
            val = cache.cache.get(_key)
            logger.debug(u'getting cache from {}: {}'.format(_key, val))

            if val is None:
                resp = func(request, *args, **kwargs)

                if response_gatekeeper(resp):
                    set_cache(_key, serializer(resp), timeout)
            else:
                resp = cached_response_wrapper(val)

            return resp
Example #3
0
        def wrapped(*args, **kwargs):
            key_args = key
            if key is None:
                key_args = _make_key_args_from_function(func, *args, **kwargs)

            _namespace = None
            if namespace is not None:
                _namespace = _make_key_for_func(namespace, args, kwargs,
                                                skip_prefix=True)

            _key = _make_key_for_func(key_args, args, kwargs,
                                      namespace=_namespace)

            val = cache.cache.get(_key)
            if val is None:
                val = func(*args, **kwargs)
                set_cache(_key, val, timeout)
            return val
Example #4
0
        def wrapped(*args, **kwargs):
            key_args = key
            if key is None:
                key_args = _make_key_args_from_function(func, *args, **kwargs)

            _namespace = None
            if namespace is not None:
                _namespace = _make_key_for_func(namespace,
                                                args,
                                                kwargs,
                                                skip_prefix=True)

            _key = _make_key_for_func(key_args,
                                      args,
                                      kwargs,
                                      namespace=_namespace)

            val = cache.cache.get(_key)
            if val is None:
                val = func(*args, **kwargs)
                set_cache(_key, val, timeout)
            return val