Skip to content

Commit

Permalink
avoid failures when using shared memcached instances on different fro…
Browse files Browse the repository at this point in the history
…ntends
  • Loading branch information
Mehmet S Catalbas authored and jezdez committed Feb 26, 2011
1 parent ef3d56b commit e5eec23
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion compressor/base.py
@@ -1,4 +1,5 @@
import os
import socket
from itertools import chain

from django.template.loader import render_to_string
Expand Down Expand Up @@ -57,7 +58,8 @@ def mtimes(self):
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):
Expand Down
9 changes: 6 additions & 3 deletions compressor/cache.py
@@ -1,4 +1,5 @@
import os
import socket

from django.core.cache import get_cache
from django.utils.encoding import smart_str
Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions compressor/tests/tests.py
@@ -1,5 +1,6 @@
import os
import re
import socket
from BeautifulSoup import BeautifulSoup

try:
Expand Down Expand Up @@ -65,8 +66,9 @@ def test_css_return_if_off(self):
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)
Expand Down

0 comments on commit e5eec23

Please sign in to comment.