def test_cache_sets_times(): container = App() container.bind('CacheConfig', cache) container.bind('CacheDiskDriver', CacheDiskDriver) container.bind('CacheManager', CacheManager(container)) container.bind('Application', container) container.bind('Cache', container.make('CacheManager').driver('disk')) cache_driver = container.make('Cache') cache_driver.store_for('key_for_1_minute', 'value', 1, 'minute') cache_driver.store_for('key_for_1_hour', 'value', 1, 'hour') cache_driver.store_for('key_for_1_day', 'value', 1, 'day') cache_driver.store_for('key_for_1_month', 'value', 1, 'month') cache_driver.store_for('key_for_1_year', 'value', 1, 'year') assert cache_driver.is_valid('key_for_1_minute') assert cache_driver.is_valid('key_for_1_hour') assert cache_driver.is_valid('key_for_1_day') assert cache_driver.is_valid('key_for_1_month') assert cache_driver.is_valid('key_for_1_year') assert cache_driver.get('key_for_1_minute') == 'value' assert cache_driver.get('key_for_1_hour') == 'value' assert cache_driver.get('key_for_1_day') == 'value' assert cache_driver.get('key_for_1_month') == 'value' assert cache_driver.get('key_for_1_year') == 'value' for cache_file in glob.glob('bootstrap/cache/key*'): os.remove(cache_file)
def setup_method(self): self.app = App() self.app.bind('CacheConfig', cache) self.app.bind('CacheDiskDriver', CacheDiskDriver) self.app.bind('CacheManager', CacheManager(self.app)) self.app.bind('Application', self.app) self.app.bind('Cache', self.app.make('CacheManager').driver('disk'))
def setup_method(self): self.app = App() self.app.bind('CacheConfig', cache) self.app.bind('CacheDiskDriver', CacheDiskDriver) self.app.bind('CacheRedisDriver', CacheRedisDriver) self.app.bind('CacheManager', CacheManager(self.app)) self.app.bind('Application', self.app) self.app.bind('Cache', self.app.make('CacheManager').driver('disk')) self.drivers = ['disk'] if os.environ.get('REDIS_CACHE_DRIVER'): self.drivers.append('redis')
def test_cache_throws_exception_with_incorrect_cache_type(self): self.container.bind('CacheConfig', cache) self.container.bind('CacheDiskDriver', CacheDiskDriver) self.container.bind('CacheManager', CacheManager(self.container)) self.container.bind('Application', self.container) self.container.bind('Cache', self.container.make('CacheManager').driver('disk')) view = self.container.make('View') with self.assertRaises(ValueError): view('test_exception', {'test': 'test'}).cache_for(1, 'monthss')
def test_cache_throws_exception_with_incorrect_cache_type(): container = App() view = View(container) container.bind('CacheConfig', cache) container.bind('CacheDiskDriver', CacheDiskDriver) container.bind('CacheManager', CacheManager(container)) container.bind('Application', container) container.bind('Cache', container.make('CacheManager').driver('disk')) container.bind('View', view.render) view = container.make('View') with pytest.raises(ValueError): view('test_exception', {'test': 'test'}).cache_for(1, 'monthss')
def test_view_cache_caches_files(self): self.container.bind('CacheConfig', cache) self.container.bind('CacheDiskDriver', CacheDiskDriver) self.container.bind('CacheManager', CacheManager(self.container)) self.container.bind('Application', self.container) self.container.bind('Cache', self.container.make('CacheManager').driver('disk')) view = self.container.make('View') self.assertEqual( view('test_cache', { 'test': 'test' }).cache_for(1, 'second').rendered_template, 'test') self.assertEqual( open(glob.glob('bootstrap/cache/test_cache:*')[0]).read(), 'test') time.sleep(2) self.assertEqual( view('test_cache', { 'test': 'macho' }).cache_for(5, 'seconds').rendered_template, 'macho') time.sleep(2) self.assertEqual( open(glob.glob('bootstrap/cache/test_cache:*')[0]).read(), 'macho') self.assertEqual( view('test_cache', { 'test': 'macho' }).cache_for(1, 'second').rendered_template, 'macho') time.sleep(1) self.assertEqual( open(glob.glob('bootstrap/cache/test_cache:*')[0]).read(), 'macho') self.assertEqual( view('test_cache', { 'test': 'macho' }).cache_for('1', 'second').rendered_template, 'macho')
def test_get_cache(): container = App() container.bind('CacheConfig', cache) container.bind('CacheDiskDriver', CacheDiskDriver) container.bind('CacheManager', CacheManager(container)) container.bind('Application', container) container.bind('Cache', container.make('CacheManager').driver('disk')) cache_driver = container.make('Cache') cache_driver.store('key', 'value') cache_driver.store_for('key_time', 'key value', 2, 'seconds') assert cache_driver.get('key') == 'value' assert cache_driver.get('key_time') == 'key value' for cache_file in glob.glob('bootstrap/cache/key*'): os.remove(cache_file)
def test_driver_disk_cache_store(): container = App() container.bind('CacheConfig', cache) container.bind('CacheDiskDriver', CacheDiskDriver) container.bind('CacheManager', CacheManager(container)) container.bind('Application', container) container.bind('Cache', container.make('CacheManager').driver('disk')) key = "forever_cache_driver_test" key = container.make('Cache').store(key, "macho") # This return the same key because it's forever assert key == key content = container.make('Cache').get(key) assert content == "macho" assert container.make('Cache').cache_exists(key) assert container.make('Cache').is_valid(key) container.make('Cache').delete(key)
def test_view_cache_caches_files(): container = App() view = View(container) container.bind('CacheConfig', cache) container.bind('CacheDiskDriver', CacheDiskDriver) container.bind('CacheManager', CacheManager(container)) container.bind('Application', container) container.bind('Cache', container.make('CacheManager').driver('disk')) container.bind('View', view.render) view = container.make('View') assert view('test_cache', { 'test': 'test' }).cache_for(1, 'second').rendered_template == 'test' assert open(glob.glob('bootstrap/cache/test_cache:*')[0]).read() == 'test' time.sleep(2) assert view('test_cache', { 'test': 'macho' }).cache_for(5, 'seconds').rendered_template == 'macho' time.sleep(2) assert open(glob.glob('bootstrap/cache/test_cache:*')[0]).read() == 'macho' assert view('test_cache', { 'test': 'macho' }).cache_for(1, 'second').rendered_template == 'macho' time.sleep(1) assert open(glob.glob('bootstrap/cache/test_cache:*')[0]).read() == 'macho' assert view('test_cache', { 'test': 'macho' }).cache_for('1', 'second').rendered_template == 'macho'
def test_driver_disk_cache_store_for(): container = App() container.bind('CacheConfig', cache) container.bind('CacheDiskDriver', CacheDiskDriver) container.bind('CacheManager', CacheManager(container)) container.bind('Application', container) container.bind('Cache', container.make('CacheManager').driver('disk')) key = "cache_driver_test" key_store = container.make('Cache').store_for(key, "macho", 5, "seconds") # This return one key like this: cache_driver_test:1519741028.5628147 assert key == key_store[:len(key)] content = container.make('Cache').get(key) assert content == "macho" assert container.make('Cache').cache_exists(key) assert container.make('Cache').is_valid(key) container.make('Cache').delete(key) assert not container.make('Cache').is_valid("error")
def test_cache_expired_before_get(): container = App() container.bind('CacheConfig', cache) container.bind('CacheDiskDriver', CacheDiskDriver) container.bind('CacheManager', CacheManager(container)) container.bind('Application', container) container.bind('Cache', container.make('CacheManager').driver('disk')) cache_driver = container.make('Cache') cache_driver.store_for('key_for_1_second', 'value', 1, 'second') assert cache_driver.is_valid('key_for_1_second') assert cache_driver.get('key_for_1_second') == 'value' time.sleep(2) assert not cache_driver.is_valid('key_for_1_second') assert cache_driver.get('key_for_1_second') is None for cache_file in glob.glob('bootstrap/cache/key*'): os.remove(cache_file)
def boot(self, CacheManager, CacheConfig): self.app.bind('Cache', CacheManager.driver(CacheConfig.DRIVER))
def register(self): self.app.bind('CacheConfig', cache) self.app.bind('CacheDiskDriver', CacheDiskDriver) self.app.bind('CacheManager', CacheManager(self.app))