diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py index 73b02a55b3..aba8b61a4d 100644 --- a/django/core/cache/backends/locmem.py +++ b/django/core/cache/backends/locmem.py @@ -29,8 +29,8 @@ class LocMemCache(BaseCache): self.validate_key(key) pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) with self._lock.writer(): - exp = self._expire_info.get(key) - if exp is None or exp <= time.time(): + exp = self._expire_info.get(key, 0) + if exp is not None and exp <= time.time(): self._set(key, pickled, timeout) return True return False diff --git a/tests/cache/tests.py b/tests/cache/tests.py index c93bcd816a..a9f5927c83 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -491,6 +491,9 @@ class BaseCacheTests(object): cache.add('key2', 'ham', None) self.assertEqual(cache.get('key2'), 'ham') + added = cache.add('key1', 'new eggs', None) + self.assertEqual(added, False) + self.assertEqual(cache.get('key1'), 'eggs') cache.set_many({'key3': 'sausage', 'key4': 'lobster bisque'}, None) self.assertEqual(cache.get('key3'), 'sausage')