def __init__(self, func, keys, function_args, function_kwargs, timeout=None, cache_backend=None, cache_host=None, verbose=None): self.timeout = PySmartCacheSettings._get_timeout(timeout) self.cache_backend = PySmartCacheSettings._get_cache_backend(cache_backend) self.verbose = PySmartCacheSettings._get_verbose(verbose) self.func = func self.keys = keys self.function_args = function_args self.function_kwargs = function_kwargs self.function_arguments = inspect.getcallargs(self.func, *self.function_args, **self.function_kwargs) self.now_reference = datetime.datetime.now() self.cache_client = CacheClient.instantiate(self.cache_backend, cache_host) self._create_func_full_qualified_name() self._create_cache_key() self.stored_info = self.info() self.stored_value = self.stored_info['value'] if self.stored_info else None self.stored_at = self.stored_info['date added'] if self.stored_info else None if self.verbose: print '-' * 50 print 'KEY: {}'.format(self.cache_key)
def tearDown(self): super(SettingsHierarchyTestCase, self).tearDown() os.environ.pop('PYSMARTCACHE_VERBOSE', None) os.environ.pop('PYSMARTCACHE_TIMEOUT', None) os.environ.pop('PYSMARTCACHE_HOST', None) os.environ.pop('PYSMARTCACHE_BACKEND', None) PySmartCacheSettings.reset()
def __init__(self, func, keys, function_args, function_kwargs, timeout=None, cache_backend=None, cache_host=None, verbose=None): self.timeout = PySmartCacheSettings._get_timeout(timeout) self.cache_backend = PySmartCacheSettings._get_cache_backend( cache_backend) self.verbose = PySmartCacheSettings._get_verbose(verbose) self.func = func self.keys = keys self.function_args = function_args self.function_kwargs = function_kwargs self.function_arguments = inspect.getcallargs(self.func, *self.function_args, **self.function_kwargs) self.now_reference = datetime.datetime.now() self.cache_client = CacheClient.instantiate(self.cache_backend, cache_host) self._create_func_full_qualified_name() self._create_cache_key() self.stored_info = self.info() self.stored_value = self.stored_info[ 'value'] if self.stored_info else None self.stored_at = self.stored_info[ 'date added'] if self.stored_info else None if self.verbose: print '-' * 50 print 'KEY: {}'.format(self.cache_key)
def test_common_timeout(self): PySmartCacheSettings.timeout = 20 os.environ['PYSMARTCACHE_TIMEOUT'] = '30' self.assertEquals(PySmartCacheSettings._get_timeout(10), 10) self.assertEquals(PySmartCacheSettings._get_timeout(None), 20) PySmartCacheSettings.reset() self.assertEquals(PySmartCacheSettings._get_timeout(None), 30) os.environ.pop('PYSMARTCACHE_TIMEOUT', None) self.assertEquals(PySmartCacheSettings._get_timeout(None), PySmartCacheSettings._DEFAULT_TIMEOUT)
def test_common_cache_backend(self): PySmartCacheSettings.cache_backend = 'redis' os.environ['PYSMARTCACHE_BACKEND'] = 'redis' self.assertEquals(PySmartCacheSettings._get_cache_backend('redis'), 'redis') self.assertEquals(PySmartCacheSettings._get_cache_backend(None), 'redis') PySmartCacheSettings.reset() self.assertEquals(PySmartCacheSettings._get_cache_backend(None), 'redis') os.environ.pop('PYSMARTCACHE_BACKEND', None) self.assertEquals(PySmartCacheSettings._get_cache_backend(None), PySmartCacheSettings._DEFAULT_CACHE_BACKEND)
def test_cache_host_boolean_false(self): with self.assertRaises(ImproperlyConfigured) as e: PySmartCacheSettings._get_cache_host('', default='') self.assertEquals(str(e.exception), 'PySmartCache cache host settings must not be empty') with self.assertRaises(ImproperlyConfigured) as e: PySmartCacheSettings._get_cache_host(None, default=None) self.assertEquals(str(e.exception), 'PySmartCache cache host settings must not be empty') with self.assertRaises(ImproperlyConfigured) as e: PySmartCacheSettings._get_cache_host([], default=[]) self.assertEquals(str(e.exception), 'PySmartCache cache host settings must not be empty')
def test_common_verbose(self): PySmartCacheSettings.verbose = True os.environ['PYSMARTCACHE_VERBOSE'] = '1' self.assertTrue(PySmartCacheSettings._get_verbose(True)) self.assertTrue(PySmartCacheSettings._get_verbose(None)) PySmartCacheSettings.reset() self.assertTrue(PySmartCacheSettings._get_verbose(None)) os.environ.pop('PYSMARTCACHE_VERBOSE', None) self.assertFalse(PySmartCacheSettings._get_verbose(None))
def test_common_cache_host_redis(self): PySmartCacheSettings.cache_host = '127.0.0.1:6373' os.environ['PYSMARTCACHE_HOST'] = '127.0.0.1:6372' self.assertEquals( PySmartCacheSettings._get_cache_host('127.0.0.1:6374', default=RedisClient._DEFAULT_HOST, use_list=False), '127.0.0.1:6374' ) self.assertEquals( PySmartCacheSettings._get_cache_host(None, default=RedisClient._DEFAULT_HOST, use_list=False), '127.0.0.1:6373' ) PySmartCacheSettings.reset() self.assertEquals( PySmartCacheSettings._get_cache_host(None, default=RedisClient._DEFAULT_HOST, use_list=False), '127.0.0.1:6372' ) os.environ.pop('PYSMARTCACHE_HOST', None) self.assertEquals( PySmartCacheSettings._get_cache_host(None, default=RedisClient._DEFAULT_HOST, use_list=False), RedisClient._DEFAULT_HOST )
def test_common_cache_host_memcached(self): PySmartCacheSettings.cache_host = ['127.0.0.1:11213', ] os.environ['PYSMARTCACHE_HOST'] = '127.0.0.1:11212' self.assertEquals( PySmartCacheSettings._get_cache_host(['127.0.0.1:11214', ], default=MemcachedClient._DEFAULT_HOST, use_list=True), ['127.0.0.1:11214', ] ) self.assertEquals( PySmartCacheSettings._get_cache_host(None, default=MemcachedClient._DEFAULT_HOST, use_list=True), ['127.0.0.1:11213', ] ) PySmartCacheSettings.reset() self.assertEquals( PySmartCacheSettings._get_cache_host(None, default=MemcachedClient._DEFAULT_HOST, use_list=True), ['127.0.0.1:11212', ] ) os.environ.pop('PYSMARTCACHE_HOST', None) self.assertEquals( PySmartCacheSettings._get_cache_host(None, default=MemcachedClient._DEFAULT_HOST, use_list=True), MemcachedClient._DEFAULT_HOST )
def test_cache_backend_not_implemented(self): with self.assertRaises(ImproperlyConfigured) as e: PySmartCacheSettings._get_cache_backend('Amazon RDS?!') self.assertEquals(str(e.exception), 'PySmartCache cache backend settings must be one of "memcached", "redis"')
def get_client(self, host=None): import redis host = PySmartCacheSettings._get_cache_host(host, default=self._DEFAULT_HOST, use_list=False) return redis.StrictRedis.from_url(host)
def get_client(self, host=None): import pylibmc host = PySmartCacheSettings._get_cache_host(host, default=self._DEFAULT_HOST, use_list=True) return pylibmc.Client(host)
def test_timeout_zero(self): with self.assertRaises(ImproperlyConfigured) as e: PySmartCacheSettings._get_timeout(0) self.assertEquals(str(e.exception), 'PySmartCache timeout settings must be positive')
def test_timeout_nan(self): with self.assertRaises(ImproperlyConfigured) as e: PySmartCacheSettings._get_timeout('NaN NaN NaN NaN Batman!') self.assertEquals(str(e.exception), 'PySmartCache timeout settings must be numeric')
def test_verbose_different_from_0_or_1(self): with self.assertRaises(ImproperlyConfigured) as e: PySmartCacheSettings._get_verbose(2) self.assertEquals(str(e.exception), 'PySmartCache verbose settings must be 0 or 1')