本文整理汇总了Python中flask_cache.Cache.add方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.add方法的具体用法?Python Cache.add怎么用?Python Cache.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask_cache.Cache
的用法示例。
在下文中一共展示了Cache.add方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CacheTestCase
# 需要导入模块: from flask_cache import Cache [as 别名]
# 或者: from flask_cache.Cache import add [as 别名]
class CacheTestCase(unittest.TestCase):
def _set_app_config(self, app):
app.config['CACHE_TYPE'] = 'simple'
def setUp(self):
app = Flask(__name__, template_folder=os.path.dirname(__file__))
app.debug = True
self._set_app_config(app)
self.cache = Cache(app)
self.app = app
def tearDown(self):
self.app = None
self.cache = None
self.tc = None
def test_00_set(self):
self.cache.set('hi', 'hello')
assert self.cache.get('hi') == 'hello'
def test_01_add(self):
self.cache.add('hi', 'hello')
assert self.cache.get('hi') == 'hello'
self.cache.add('hi', 'foobar')
assert self.cache.get('hi') == 'hello'
def test_02_delete(self):
self.cache.set('hi', 'hello')
self.cache.delete('hi')
assert self.cache.get('hi') is None
def test_03_cached_view(self):
@self.app.route('/')
@self.cache.cached(5)
def cached_view():
return str(time.time())
tc = self.app.test_client()
rv = tc.get('/')
the_time = rv.data.decode('utf-8')
time.sleep(2)
rv = tc.get('/')
assert the_time == rv.data.decode('utf-8')
time.sleep(5)
rv = tc.get('/')
assert the_time != rv.data.decode('utf-8')
def test_04_cached_view_unless(self):
@self.app.route('/a')
@self.cache.cached(5, unless=lambda: True)
def non_cached_view():
return str(time.time())
@self.app.route('/b')
@self.cache.cached(5, unless=lambda: False)
def cached_view():
return str(time.time())
tc = self.app.test_client()
rv = tc.get('/a')
the_time = rv.data.decode('utf-8')
time.sleep(1)
rv = tc.get('/a')
assert the_time != rv.data.decode('utf-8')
rv = tc.get('/b')
the_time = rv.data.decode('utf-8')
time.sleep(1)
rv = tc.get('/b')
assert the_time == rv.data.decode('utf-8')
def test_05_cached_function(self):
with self.app.test_request_context():
@self.cache.cached(2, key_prefix='MyBits')
def get_random_bits():
return [random.randrange(0, 2) for i in range(50)]
#.........这里部分代码省略.........