Permalink
Browse files

Fix issues with Django memcache backend.

  • Loading branch information...
carljm committed Jun 14, 2012
1 parent f4e0235 commit 82aa41b0051106bb23bc3b3fea437792bd37f5e6
Showing with 32 additions and 4 deletions.
  1. +22 −0 datazilla/cache.py
  2. +2 −1 datazilla/settings/base.py
  3. +8 −3 tests/conftest.py
View
@@ -0,0 +1,22 @@
+from django.core.cache.backends import memcached
+
+
+class MemcachedCache(memcached.MemcachedCache):
+ """
+ A subclass of Django's built-in Memcached backend that fixes some issues.
+
+ - Allows caching forever with a timeout of 0.
+ - Returns the return value of set() to allow for error-checking.
+
+ """
+ def _get_memcache_timeout(self, timeout):
+ if timeout is None:
+ timeout = self.default_timeout
+ if not timeout:
+ return 0
+ return super(MemcachedCache, self)._get_memcache_timeout(timeout)
+
+
+ def set(self, key, value, timeout=0, version=None):
+ key = self.make_key(key, version=version)
+ return self._cache.set(key, value, self._get_memcache_timeout(timeout))
@@ -162,8 +162,9 @@
CACHES = {
"default": {
- "BACKEND": "django.core.cache.backends.memcached.MemcachedCache",
+ "BACKEND": "datazilla.cache.MemcachedCache",
"LOCATION": DATAZILLA_MEMCACHED,
+ "TIMEOUT": 0,
}
}
View
@@ -1,11 +1,10 @@
from functools import partial
import os
-from random import choice
-from string import letters
from datazilla.vendor import add_vendor_lib
+
def pytest_sessionstart(session):
"""
Set up the test environment.
@@ -28,7 +27,13 @@ def pytest_sessionstart(session):
# this effectively clears memcached to make tests deterministic
from django.core.cache import cache
- cache.key_prefix = "t-" + "".join([choice(letters) for i in range(5)])
+ prefix_counter_cache_key = "datazilla-tests-key-prefix-counter"
+ try:
+ key_prefix_counter = cache.incr(prefix_counter_cache_key)
+ except ValueError:
+ key_prefix_counter = 0
+ cache.set(prefix_counter_cache_key, key_prefix_counter)
+ cache.key_prefix = "t{0}".format(key_prefix_counter)
from datazilla.model import DatazillaModel
DatazillaModel.create("testproj")

0 comments on commit 82aa41b

Please sign in to comment.