缓存其它函数

同样地,使用 @cached 装饰器也能够缓存其它非视图函数的结果。唯一的要求是需要指定 key_prefix,否则会使用请求路径(request.path)作为cache_key:

  1. @cache.cached(timeout=50, key_prefix='all_comments')
  2. def get_all_comments():
  3. comments = do_serious_dbio()
  4. return [x.author for x in comments]
  5. cached_comments = get_all_comments()