Skip to content

Commit

Permalink
Merge branch 'master' of github.com:django-cache-machine/django-cache…
Browse files Browse the repository at this point in the history
…-machine
  • Loading branch information
tobiasmcnulty committed Oct 17, 2015
2 parents bf470c2 + 95a7de5 commit 6f4d440
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
19 changes: 14 additions & 5 deletions caching/backends/memcached.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
from __future__ import unicode_literals

import django
from django.core.cache.backends import memcached

from caching.compat import DEFAULT_TIMEOUT


# Add infinite timeout support to the memcached backend.
# Add infinite timeout support to the memcached backend, if needed.
class InfinityMixin(object):

def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
return super(InfinityMixin, self).add(key, value, timeout, version)
if django.VERSION[:2] < (1, 6):
# Django 1.6 and later do it the right way already
def _get_memcache_timeout(self, timeout):
if timeout == 0:
return timeout
else:
return super(InfinityMixin, self)._get_memcache_timeout(timeout)

def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
return super(InfinityMixin, self).set(key, value, timeout, version)
def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
return super(InfinityMixin, self).add(key, value, timeout, version)

def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
return super(InfinityMixin, self).set(key, value, timeout, version)


class MemcachedCache(InfinityMixin, memcached.MemcachedCache):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
else:
import mock
from nose.tools import eq_
from nose.plugins.skip import SkipTest

from caching import base, invalidation, config, compat
from .testapp.models import Addon, User
Expand Down Expand Up @@ -405,6 +406,17 @@ def test_timeout_from_queryset(self):
assert hasattr(a, 'from_cache')
eq_(a.id, 1)

@mock.patch('memcache.Client.set')
def test_infinite_timeout(self, mock_set):
"""
Test that memcached infinite timeouts work with all Django versions.
"""
if not any(['memcache' in c['BACKEND'] for c in settings.CACHES.values()]):
raise SkipTest('This test requires that Django use memcache')
cache.set('foo', 'bar', timeout=compat.FOREVER)
# for memcached, 0 timeout means store forever
mock_set.assert_called_with(':1:foo', 'bar', 0)

def test_cache_and_no_cache(self):
"""Whatever happens last sticks."""
q = Addon.objects.no_cache().cache(12).filter(id=1)
Expand Down

0 comments on commit 6f4d440

Please sign in to comment.