diff --git a/redis_cache/redis_cache.py b/redis_cache/redis_cache.py index fd0cc11..aa1bf8d 100644 --- a/redis_cache/redis_cache.py +++ b/redis_cache/redis_cache.py @@ -26,6 +26,15 @@ def wrapper(*args, **kwargs): ) else: self.redis_client.set(fn_hash, ret, **options) + + # Add the hash to the fn_name and tags lookup tables + self.redis_client.sadd(fn_name, fn_hash) + + if 'tags' in options: + tags = options.get('tags') + for tag in tags: + self.redis_client.sadd(tag, fn_hash) + else: # Cache hit return cache_request @@ -33,6 +42,26 @@ def wrapper(*args, **kwargs): return wrapper return cache_inside + def get_tag_cache(tag): + return self.redis_client.smembers(tag) + + def reset_tag_cache(tag): + caches = self.redis_client.smembers(tag) + self.redis_client.delete(caches) + self.redis_client.delete(tag) + + def get_function_cache(fn_name): + self.get_tag_cache(fn_name) + + def reset_function_cache(fn_name): + self.reset_tag_cache(fn_name) + + def get_function_instance_cache(): + raise NotImplemented() + + def reset_function_instance_cache(): + raise NotImplemented() + def _get_signature(*args, **kwargs): """ Gets the signature of the decorated method diff --git a/tests/test_redis_cache.py b/tests/test_redis_cache.py index 98b5850..aa4cbae 100644 --- a/tests/test_redis_cache.py +++ b/tests/test_redis_cache.py @@ -92,3 +92,25 @@ def test_function(a): mock_client.get.assert_called_once_with(expected_hash) self.assertEqual(mock_client.set.call_count, 0) self.assertEqual(function_response, test_param) + + @patch('redis_cache.redis_cache.RedisClient') + def test_cache_get_tag_cache(self, mock_client_object): + pass + + @patch('redis_cache.redis_cache.RedisClient') + def test_cache_reset_tag_cache(self, mock_client_object): + pass + + @patch('redis_cache.redis_cache.RedisClient') + def test_cache_get_function_cache(self, mock_client_object): + pass + + @patch('redis_cache.redis_cache.RedisClient') + def test_cache_reset_function_cache(self, mock_client_object): + pass + + def test_get_function_instance_cache(self, mock_client_object): + raise NotImplemented() + + def test_reset_function_instance_cache(self, mock_client_object): + raise NotImplemented()