Skip to content

Commit 0f6a835

Browse files
committed
Adds custom 'has_key' implementation using redis's 'exists' command.
1 parent 0921e2e commit 0f6a835

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

redis_cache/cache.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ def ttl(self, key, version=None):
333333
return self._client.ttl(key)
334334
return 0
335335

336+
def has_key(self, key, version=None):
337+
"""
338+
Returns True if the key is in the cache and has not expired.
339+
"""
340+
key = self.make_key(key, version=version)
341+
return self._client.exists(key)
342+
336343

337344
class RedisCache(CacheClass):
338345
"""

tests/testapp/tests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,13 @@ def test_non_existent_key(self):
418418
ttl = self.cache.ttl('does_not_exist')
419419
self.assertEqual(ttl, 0)
420420

421+
def test_has_key_with_no_key(self):
422+
self.assertFalse(self.cache.has_key('does_not_exist'))
423+
424+
def test_has_key_with_key(self):
425+
self.cache.set('a', 'a')
426+
self.assertTrue(self.cache.has_key('a'))
427+
421428

422429
if __name__ == '__main__':
423430
import unittest

0 commit comments

Comments
 (0)