Package gluon :: Module cache :: Class Cache
[hide private]
[frames] | no frames]

Class Cache

source code

object --+
         |
        Cache


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

Instance Methods [hide private]
 
__call__(self, key=None, time_expire=300, cache_model=None)
Decorator function that can be used to cache any function/method.
source code
 
__init__(self, request)
Args: ...
source code
 
action(self, time_expire=300, cache_model=None, prefix=None, session=False, vars=True, lang=True, user_agent=False, public=True, valid_statuses=None, quick=None)
Better fit for caching an action
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Static Methods [hide private]
 
with_prefix(cache_model, prefix)
allow replacing cache.ram with cache.with_prefix(cache.ram,'prefix') it will add prefix to all the cache keys used.
source code
Class Variables [hide private]
  autokey = ':%(name)s:%(args)s:%(vars)s'
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__call__(self, key=None, time_expire=300, cache_model=None)
(Call operator)

source code 

Decorator function that can be used to cache any function/method.

Args:
    key(str) : the key of the object to be store or retrieved
    time_expire(int) : expiration of the cache in seconds
        `time_expire` is used to compare the current time with the time 
        when the requested object was last saved in cache. 
        It does not affect future requests.
        Setting `time_expire` to 0 or negative value forces the cache to
        refresh.
    cache_model(str): can be "ram", "disk" or other (like "memcache").
        Defaults to "ram"

When the function `f` is called, web2py tries to retrieve
the value corresponding to `key` from the cache if the
object exists and if it did not expire, else it calles the function `f`
and stores the output in the cache corresponding to `key`. In the case
the output of the function is returned.

Example: ::

  @cache('key', 5000, cache.ram)
  def f():
      return time.ctime()

Note:
    If the function `f` is an action, we suggest using
    @cache.action instead

__init__(self, request)
(Constructor)

source code 

Args: 
    request: the global request object

Overrides: object.__init__

action(self, 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 code 
Better fit for caching an action

Warning: 
    Experimental!

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

Args:
    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'