Code

Ticket #4041: 4041.2.diff

File 4041.2.diff, 1.0 KB (added by SmileyChris, 7 years ago)

optimized version

Line 
1Index: django/core/cache/backends/base.py
2===================================================================
3--- django/core/cache/backends/base.py  (revision 5023)
4+++ django/core/cache/backends/base.py  (working copy)
5@@ -54,3 +54,6 @@
6         Returns True if the key is in the cache and has not expired.
7         """
8         return self.get(key) is not None
9+
10+    # Allow use of 'in' operator.
11+    __contains__ = has_key
12Index: tests/regressiontests/cache/tests.py
13===================================================================
14--- tests/regressiontests/cache/tests.py        (revision 5023)
15+++ tests/regressiontests/cache/tests.py        (working copy)
16@@ -46,6 +46,11 @@
17         self.assertEqual(cache.has_key("hello"), True)
18         self.assertEqual(cache.has_key("goodbye"), False)
19 
20+    def test_in(self):
21+        cache.set("hello", "goodbye")
22+        self.assertEqual("hello" in cache, True)
23+        self.assertEqual("goodbye" in cache, False)
24+
25     def test_data_types(self):
26         # test data types
27         stuff = {