Combined cachekey generator in compressor.cache module.

This commit is contained in:
Jannis Leidel
2011-05-13 12:42:05 +02:00
parent 068a7e24c4
commit 32b9e8963e
4 changed files with 45 additions and 39 deletions

View File

@@ -86,9 +86,8 @@ class Compressor(object):
@cached_property
def cachekey(self):
key = get_hexdigest(''.join(
return get_hexdigest(''.join(
[self.content] + self.mtimes).encode(self.charset), 12)
return "django_compressor.%s.%s" % (socket.gethostname(), key)
@cached_property
def hunks(self):

View File

@@ -1,5 +1,6 @@
import os
import socket
import time
from django.core.cache import get_cache
from django.utils.encoding import smart_str
@@ -15,15 +16,22 @@ def get_hexdigest(plaintext, length=None):
return digest
def get_cachekey(key):
return ("django_compressor.%s.%s" % (socket.gethostname(), key))
def get_mtime_cachekey(filename):
return "django_compressor.mtime.%s.%s" % (socket.gethostname(),
get_hexdigest(filename))
return get_cachekey("mtime.%s" % get_hexdigest(filename))
def get_offline_cachekey(source):
return ("django_compressor.offline.%s.%s" %
(socket.gethostname(),
get_hexdigest("".join(smart_str(s) for s in source))))
return get_cachekey(
"offline.%s" % get_hexdigest("".join(smart_str(s) for s in source)))
def get_templatetag_cachekey(compressor, mode, kind):
return get_cachekey(
"templatetag.%s.%s.%s" % (compressor.cachekey, mode, kind))
def get_mtime(filename):
@@ -46,4 +54,26 @@ def get_hashed_mtime(filename, length=12):
return get_hexdigest(mtime, length)
def cache_get(key):
packed_val = cache.get(key)
if packed_val is None:
return None
val, refresh_time, refreshed = packed_val
if (time.time() > refresh_time) and not refreshed:
# Store the stale value while the cache
# revalidates for another MINT_DELAY seconds.
cache_set(key, val, refreshed=True,
timeout=settings.COMPRESS_MINT_DELAY)
return None
return val
def cache_set(key, val, refreshed=False,
timeout=settings.COMPRESS_REBUILD_TIMEOUT):
refresh_time = timeout + time.time()
real_timeout = timeout + settings.COMPRESS_MINT_DELAY
packed_val = (val, refresh_time, refreshed)
return cache.set(key, packed_val, real_timeout)
cache = get_cache(settings.COMPRESS_CACHE_BACKEND)

View File

@@ -1,9 +1,8 @@
import time
from django import template
from django.core.exceptions import ImproperlyConfigured
from compressor.cache import cache, get_offline_cachekey
from compressor.cache import (cache, cache_get, cache_set,
get_offline_cachekey, get_templatetag_cachekey)
from compressor.conf import settings
from compressor.utils import get_class
@@ -26,29 +25,6 @@ class CompressorNode(template.Node):
self.compressor_cls = get_class(
COMPRESSORS.get(self.kind), exception=ImproperlyConfigured)
def cache_get(self, key):
packed_val = cache.get(key)
if packed_val is None:
return None
val, refresh_time, refreshed = packed_val
if (time.time() > refresh_time) and not refreshed:
# Store the stale value while the cache
# revalidates for another MINT_DELAY seconds.
self.cache_set(key, val, refreshed=True,
timeout=settings.COMPRESS_MINT_DELAY)
return None
return val
def cache_set(self, key, val, refreshed=False,
timeout=settings.COMPRESS_REBUILD_TIMEOUT):
refresh_time = timeout + time.time()
real_timeout = timeout + settings.COMPRESS_MINT_DELAY
packed_val = (val, refresh_time, refreshed)
return cache.set(key, packed_val, real_timeout)
def cache_key(self, compressor):
return "%s.%s.%s" % (compressor.cachekey, self.mode, self.kind)
def debug_mode(self, context):
if settings.COMPRESS_DEBUG_TOGGLE:
# Only check for the debug parameter
@@ -72,8 +48,9 @@ class CompressorNode(template.Node):
and return a tuple of cache key and output
"""
if settings.COMPRESS_ENABLED and not forced:
cache_key = self.cache_key(compressor)
cache_content = self.cache_get(cache_key)
cache_key = get_templatetag_cachekey(
compressor, self.mode, self.kind)
cache_content = cache_get(cache_key)
return cache_key, cache_content
return None, None
@@ -97,7 +74,7 @@ class CompressorNode(template.Node):
try:
rendered_output = compressor.output(self.mode, forced=forced)
if cache_key:
self.cache_set(cache_key, rendered_output)
cache_set(cache_key, rendered_output)
return rendered_output
except Exception, e:
if settings.DEBUG or forced:

View File

@@ -86,9 +86,9 @@ class CompressorTestCase(TestCase):
self.assertEqual(self.css, self.css_node.output())
def test_cachekey(self):
host_name = socket.gethostname()
is_cachekey = re.compile(r'django_compressor\.%s\.\w{12}' % host_name)
self.assert_(is_cachekey.match(self.css_node.cachekey), "cachekey is returning something that doesn't look like r'django_compressor\.%s\.\w{12}'" % host_name)
is_cachekey = re.compile(r'\w{12}')
self.assertTrue(is_cachekey.match(self.css_node.cachekey),
"cachekey is returning something that doesn't look like r'\w{12}'")
def test_css_hash(self):
self.assertEqual('c618e6846d04', get_hexdigest(self.css, 12))