cache Module

This file is part of the web2py Web Framework
Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>

Basic caching classes and methods

  • Cache - The generic caching object interfacing with the others
  • CacheInRam - providing caching in ram
  • CacheOnDisk - provides caches on disk

Memcache is also available via a different module (see gluon.contrib.memcache)

When web2py is running on Google App Engine, caching will be provided by the GAE memcache (see gluon.contrib.gae_memcache)

class gluon.cache.Cache(request)[source]

Bases: object

Sets up generic caching, creating an instance of both CacheInRam and CacheOnDisk. In case of GAE will make use of gluon.contrib.gae_memcache.

  • self.ram is an instance of CacheInRam
  • self.disk is an instance of CacheOnDisk
action(time_expire=300, cache_model=None, prefix=None, session=False, vars=True, lang=True, user_agent=False, public=True, valid_statuses=None, quick=None)[source]

Better fit for caching an action

Warning

Experimental!

Currently only HTTP 1.1 compliant reference : http://code.google.com/p/doctype-mirror/wiki/ArticleHttpCaching

Parameters:
  • time_expire (int) – same as @cache
  • cache_model (str) – same as @cache
  • prefix (str) – add a prefix to the calculated key
  • session (bool) – adds response.session_id to the key
  • vars (bool) – adds request.env.query_string
  • lang (bool) – adds T.accepted_language
  • user_agent (bool or dict) – if True, adds is_mobile and is_tablet to the key. Pass a dict to use all the needed values (uses str(.items())) (e.g. user_agent=request.user_agent()). Used only if session is not True
  • public (bool) – if False forces the Cache-Control to be ‘private’
  • valid_statuses – by default only status codes starting with 1,2,3 will be cached. pass an explicit list of statuses on which turn the cache on
  • quick – Session,Vars,Lang,User-agent,Public: fast overrides with initials, e.g. ‘SVLP’ or ‘VLP’, or ‘VLP’
autokey = ':%(name)s:%(args)s:%(vars)s'
static with_prefix(cache_model, prefix)[source]

allow replacing cache.ram with cache.with_prefix(cache.ram,’prefix’) it will add prefix to all the cache keys used.

gluon.cache.lazy_cache(key=None, time_expire=None, cache_model='ram')[source]

Can be used to cache any function including ones in modules, as long as the cached function is only called within a web2py request

If a key is not provided, one is generated from the function name time_expire defaults to None (no cache expiration)

If cache_model is “ram” then the model is current.cache.ram, etc.