Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
test cases added and cache_prefix changes added to other backend
Browse files Browse the repository at this point in the history
  • Loading branch information
lavish205 committed Mar 10, 2016
1 parent 8a9278c commit 40c3ffd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
10 changes: 8 additions & 2 deletions cliquet/cache/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Cache(CacheBase):
"""

def __init__(self, *args, **kwargs):
self.prefix = kwargs['prefix']
super(Cache, self).__init__(*args, **kwargs)
self.flush()

Expand All @@ -25,33 +26,38 @@ def flush(self):
self._store = {}

def ttl(self, key):
key = self.prefix + key
ttl = self._ttl.get(key)
if ttl is not None:
return (ttl - utils.msec_time()) / 1000.0
return -1

def expire(self, key, ttl):
key = self.prefix + key
self._ttl[key] = utils.msec_time() + int(ttl * 1000.0)

def set(self, key, value, ttl=None):
self._store[key] = value
if ttl is not None:
self.expire(key, ttl)
key = self.prefix + key
self._store[key] = value

def get(self, key):
key = self.prefix + key
current = utils.msec_time()
expired = [k for k, v in self._ttl.items() if current > v]
for key in expired:
self.delete(key)
return self._store.get(key)

def delete(self, key):
key = self.prefix + key
self._ttl.pop(key, None)
self._store.pop(key, None)


def load_from_config(config):
settings = config.get_settings()
prefix = settings.get('cache_prefix')
prefix = settings['cache_prefix']
backend = Cache(prefix=prefix)
return backend
63 changes: 61 additions & 2 deletions cliquet/tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ def test_ttl_return_none_if_unknown(self):

class MemoryCacheTest(BaseTestCache, unittest.TestCase):
backend = memory_backend
settings = {
'cache_prefix': ''
}

def test_backend_error_is_raised_anywhere(self):
pass
Expand All @@ -156,12 +159,67 @@ def test_ping_returns_false_if_unavailable(self):
def test_ping_logs_error_if_unavailable(self):
pass

def test_cache_prefix_is_set(self):
settings_prefix = self.settings.copy()
settings_prefix['cache_prefix'] = 'prefix_'
config_prefix = self._get_config(settings=settings_prefix)

# initiating cache backend with prefix:
backend_prefix = self.backend.load_from_config(config_prefix)

# set a value with a cache that has no prefix
backend_prefix.set('key', 'foo')

# obtain the value of cache that has prefix
obtained = backend_prefix.get('key')

self.assertEqual(obtained, 'foo')

def test_cache_when_prefix_is_not_set(self):
settings_prefix = self.settings.copy()
config_prefix = self._get_config(settings=settings_prefix)
backend_prefix = self.backend.load_from_config(config_prefix)

# set a value with a cache that has no prefix
backend_prefix.set('key', 'foo')

obtained = backend_prefix.get('key')
self.assertEqual(obtained, 'foo')

def test_prefix_value_use_to_delete_data(self):
settings_prefix = self.settings.copy()
settings_prefix['cache_prefix'] = 'prefix_'
config_prefix = self._get_config(settings=settings_prefix)

# initiating cache backend with prefix:
backend_prefix = self.backend.load_from_config(config_prefix)

# set a value with a cache that has no prefix
backend_prefix.set('key', 'foo')

# deleting data
backend_prefix.delete('key')

def test_prefix_value_used_with_ttl(self):
settings_prefix = self.settings.copy()
settings_prefix['cache_prefix'] = 'prefix_'
config_prefix = self._get_config(settings=settings_prefix)

# initiating cache backend with prefix:
backend_prefix = self.backend.load_from_config(config_prefix)

# set a value with a cache that has no prefix
backend_prefix.set('key', 'foo', 10.0)
obtained = backend_prefix.ttl('key')
self.assertEqual(obtained, 10.0)


class RedisCacheTest(BaseTestCache, unittest.TestCase):
backend = redis_backend
settings = {
'cache_url': '',
'cache_pool_size': 10
'cache_pool_size': 10,
'cache_prefix': ''
}

def setUp(self):
Expand All @@ -185,7 +243,8 @@ class PostgreSQLCacheTest(BaseTestCache, unittest.TestCase):
backend = postgresql_backend
settings = {
'cache_pool_size': 10,
'cache_url': 'postgres://postgres:postgres@localhost:5432/testdb'
'cache_url': 'postgres://postgres:postgres@localhost:5432/testdb',
'cache_prefix': ''
}

def setUp(self):
Expand Down

0 comments on commit 40c3ffd

Please sign in to comment.