diff --git a/compressor/cache.py b/compressor/cache.py index c441522..5f45506 100644 --- a/compressor/cache.py +++ b/compressor/cache.py @@ -5,8 +5,10 @@ import time from django.core.cache import get_cache from django.utils.encoding import smart_str from django.utils.hashcompat import md5_constructor +from django.utils.importlib import import_module from compressor.conf import settings +from compressor.utils import get_mod_func def get_hexdigest(plaintext, length=None): @@ -15,10 +17,18 @@ def get_hexdigest(plaintext, length=None): return digest[:length] return digest +def simple_cachekey(key): + return 'django_compressor.%s' % smart_str(key) -def get_cachekey(key): - return ("django_compressor.%s.%s" % (socket.gethostname(), key)) +def socket_cachekey(key): + return "django_compressor.%s.%s" % (socket.gethostname(), smart_str(key)) +try: + mod_name, func_name = get_mod_func(settings.COMPRESS_CACHE_KEY_FUNCTION) + get_cachekey = getattr(import_module(mod_name), func_name) +except (AttributeError, ImportError), e: + raise ImportError("Couldn't import cache key function %s: %s" % + (settings.COMPRESS_CACHE_KEY_FUNCTION, e)) def get_mtime_cachekey(filename): return get_cachekey("mtime.%s" % get_hexdigest(filename)) diff --git a/compressor/conf.py b/compressor/conf.py index eed3c66..1f99238 100644 --- a/compressor/conf.py +++ b/compressor/conf.py @@ -46,6 +46,8 @@ class CompressorSettings(AppSettings): # the cache backend to use CACHE_BACKEND = None + # the dotted path to the function that creates the cache key + CACHE_KEY_FUNCTION = 'compressor.cache.simple_cachekey' # rebuilds the cache every 30 days if nothing has changed. REBUILD_TIMEOUT = 60 * 60 * 24 * 30 # 30 days # the upper bound on how long any compression should take to be generated diff --git a/docs/changelog.txt b/docs/changelog.txt index bf10332..05eff14 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -9,6 +9,13 @@ HEAD has the potential to breaking lots of apps but on the other hand will help find bugs. +- **BACKWARDS-INCOMPATIBLE** The default function to create the cache + key stopped containing the server hostname. Instead the cache key + now only has the form ``'django_compressor.'``. + + To revert to the previous way simply set the ``COMPRESS_CACHE_KEY_FUNCTION`` + to ``'django_compressor.cache.socket_cachekey'``. + - Added Compass filter (beta). - Fixed compiler filters on Windows.