Skip to content

Commit

Permalink
Fixed #7967 -- Make sure the __contains__ method in the cache backend…
Browse files Browse the repository at this point in the history
…s call the

right has_key() method for the subclass. Patch from Marty Alchin.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8084 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Jul 26, 2008
1 parent f49c5c2 commit 6f16b5b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion django/core/cache/backends/base.py
Expand Up @@ -63,4 +63,11 @@ def has_key(self, key):
"""
return self.get(key) is not None

__contains__ = has_key
def __contains__(self, key):
"""
Returns True if the key is in the cache and has not expired.
"""
# This is a separate method, rather than just a copy of has_key(),
# so that it always has the same functionality as has_key(), even
# if a subclass overrides it.
return self.has_key(key)
4 changes: 4 additions & 0 deletions tests/regressiontests/cache/tests.py
Expand Up @@ -56,11 +56,15 @@ def test_has_key(self):
cache.set("hello1", "goodbye1")
self.assertEqual(cache.has_key("hello1"), True)
self.assertEqual(cache.has_key("goodbye1"), False)
cache.set("empty", 'fred')
self.assertEqual(cache.has_key("empty"), True)

def test_in(self):
cache.set("hello2", "goodbye2")
self.assertEqual("hello2" in cache, True)
self.assertEqual("goodbye2" in cache, False)
cache.set("empty", 'fred')
self.assertEqual("empty" in cache, True)

def test_data_types(self):
stuff = {
Expand Down

0 comments on commit 6f16b5b

Please sign in to comment.