Example #1
0
    def decorated(self, request, *args, **kwargs):
        debug("starting")
        try:
            if not settings.CACHES['default']['JOHNNY_CACHE']:
                raise KeyError
        except KeyError:
            raise ImproperlyConfigured('Not using johnny cache backend')
        if request_is_cacheable(request):
            key = get_cache_key(request)
            debug("Retrievable.")
            cached = cache.get(key)
            if cached is not None:
                debug("serving from cache")
                res = HttpResponse(cached)
                res["ETag"] = key
                return res

            debug("generating!")
            response = self.f(request, *args, **kwargs)
            if response_is_cacheable(request, response):
                debug("storing!")
                cache.set(key, response.content, self.time)
                response["ETag"] = key
            else:
                debug("Not storable.")
            return response 
        debug("Not retrievable.")
        debug("generating!")
        return self.f(request, *args, **kwargs)
Example #2
0
    def decorated(self, request, *args, **kwargs):
        debug("starting")
        try:
            if not settings.CACHES['default']['JOHNNY_CACHE']:
                raise KeyError
        except KeyError:
            raise ImproperlyConfigured('Not using johnny cache backend')
        if request_is_cacheable(request):
            key = get_cache_key(request)
            debug("Retrievable.")
            cached = cache.get(key)
            if cached is not None:
                debug("serving from cache")
                res = HttpResponse(cached)
                res["ETag"] = key
                return res

            debug("generating!")
            response = self.f(request, *args, **kwargs)
            if response_is_cacheable(request, response):
                debug("storing!")
                cache.set(key, response.content, self.time)
                response["ETag"] = key
            else:
                debug("Not storable.")
            return response
        debug("Not retrievable.")
        debug("generating!")
        return self.f(request, *args, **kwargs)
Example #3
0
    def test_key_uniqueness(self):
        req = HttpRequest()
        req.path = "/some/path"
        req.method = "GET"
        req.user = AnonymousUser()

        req2 = HttpRequest()
        req2.path = "/some/path"
        req2.method = "GET"
        req2.user = User.objects.create(username="******")

        req3 = HttpRequest()
        req3.path = "/some/other/path"
        req3.method = "GET"
        req3.user = AnonymousUser()

        self.assertNotEqual(get_cache_key(req), get_cache_key(req2))
        self.assertNotEqual(get_cache_key(req), get_cache_key(req3))
Example #4
0
    def test_key_uniqueness(self):
        req = HttpRequest()
        req.path = "/some/path"
        req.method = "GET"
        req.user = AnonymousUser()

        req2 = HttpRequest()
        req2.path = "/some/path"
        req2.method = "GET"
        req2.user = User.objects.create(username="******")

        req3 = HttpRequest()
        req3.path = "/some/other/path"
        req3.method = "GET"
        req3.user = AnonymousUser()

        self.assertNotEqual(get_cache_key(req), get_cache_key(req2))
        self.assertNotEqual(get_cache_key(req), get_cache_key(req3))
Example #5
0
 def test_etag(self):
     """Ensure the response's ETag is the cache key."""
     request = self.factory.get("/content-types/text/plain/")
     response = self.client.get("/content-types/text/plain/")
     headers = dict(response.items())
     self.assertEqual(headers["ETag"], get_cache_key(request))
Example #6
0
 def get_from_cache(self, request):
     return cache.get(get_cache_key(request))