avoid failures when using shared memcached instances on different frontends

This commit is contained in:
Mehmet S Catalbas
2011-02-24 09:30:24 +08:00
committed by Jannis Leidel
parent ef3d56b6cd
commit e5eec23a9c
3 changed files with 13 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
import os
import socket
from itertools import chain
from django.template.loader import render_to_string
@@ -57,7 +58,8 @@ class Compressor(object):
def cachekey(self):
cachestr = "".join(
chain([self.content], self.mtimes)).encode(self.charset)
return "django_compressor.%s" % get_hexdigest(cachestr)[:12]
return "django_compressor.%s.%s" % (socket.gethostname(),
get_hexdigest(cachestr)[:12])
@cached_property
def storage(self):

View File

@@ -1,4 +1,5 @@
import os
import socket
from django.core.cache import get_cache
from django.utils.encoding import smart_str
@@ -10,11 +11,13 @@ def get_hexdigest(plaintext):
return sha_constructor(plaintext).hexdigest()
def get_mtime_cachekey(filename):
return "django_compressor.mtime.%s" % get_hexdigest(filename)
return "django_compressor.mtime.%s.%s" % (socket.gethostname(),
get_hexdigest(filename))
def get_offline_cachekey(source):
return ("django_compressor.offline.%s" %
get_hexdigest("".join(smart_str(s) for s in source)))
return ("django_compressor.offline.%s.%s" %
(socket.gethostname(),
get_hexdigest("".join(smart_str(s) for s in source))))
def get_mtime(filename):
if settings.COMPRESS_MTIME_DELAY:

View File

@@ -1,5 +1,6 @@
import os
import re
import socket
from BeautifulSoup import BeautifulSoup
try:
@@ -65,8 +66,9 @@ class CompressorTestCase(TestCase):
self.assertEqual(self.css, self.css_node.output())
def test_cachekey(self):
is_cachekey = re.compile(r'django_compressor\.\w{12}')
self.assert_(is_cachekey.match(self.css_node.cachekey), "cachekey is returning something that doesn't look like r'django_compressor\.\w{12}'")
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)
def test_css_hash(self):
self.assertEqual('f7c661b7a124', self.css_node.hash)