TurboGears 2 has a quite good and complete caching support inherited from Pylons, as it is a pylons feature it is not really available by itself, but you can import it.
All you need is those three little things:
from pylons.decorators.cache import beaker_cache
from pylons.controllers.util import etag_cache
from pylons import cache
The first imports a decorator which makes possible to cache entire controller methods, the second imports a function to use client side caching and the third makes available a caching repository where to store whatever data you might want.
The easiest caching mechanism is etag_cache, this tells to the browser to use its own cached version of the page if it has any available instead of requesting it again to the server. etag_cache requires only one parameter: the caching key. By placing as the first instruction inside your controller action etag_cache(‘mykey’) you will tell to the browser to use its own cached version if it has any. You can use the same key inside each action as the browser will check both for key and url, so different urls won’t collide. Keep in mind that this will let the browser keep using the cached version of the page until the browser won’t be restarted and this is usually something that you don’t want. To avoid this behaviour I suggest to keep changing the key argument constantly each time you want the cache to decay, using a timestamp as key might be a good idea.
For example you can add to your lib.base.BaseController.__call__ method something like
app_globals = tg.config['pylons.app_globals']
if app_globals.caching_key+datetime.timedelta(0,10) < datetime.datetime.now():
app_globals.caching_key = datetime.datetime.now()
self.caching_key = str(app_globals.caching_key)
and then use etag_cache(self.caching_key) inside the controller action, this will let your cache expire every 10 seconds.
This might be enough in some situations where you want to completely cache your page, but often you might want to cache only your controller and render your view again. This can be achieved by using the @beaker_cache decorator. This will use Beaker to perform caching of the values returned by your controller, if it finds any available data for your controller it will return it without calling the controller method.
#Long and slow operation here
This way you will keep your action cached for 10 seconds and will cache different versions if the action parameters change.
For more complex things you might want to cache only parts of an action, this can be achieved by directly using the cache object.
c = cache.get_cache(‘my_function’)
result = c.get_value(key=function_args, createfunc=slow_function, type=”memory”, expiretime=10)
This will get the caching namespace for the current function and will retrieve the available value with the given key if available (you might see that I have called the key “function_args”, this is because it is usually a good idea to build the key by using function arguments that have any effect on the result). If it isn’t found any value (or the value has expired) slow_function will be called to calculate the new value.
New versions of beaker have a nice decorator @cache.cache which prevent you from having to get the cache namespace and the cache value by yourself, by applying @cache.cache to slow_function each call to slow_function will return the available cached value by itself. More information can be found on the relative beaker documentation section. Keep in mind that @cache.cache decorator can only be used by passing arguments as a list, it won’t work for keyword arguments.