Subsections

7. How to use caching

7.1 Introduction to caching

If you have a web site where pages take a long time to build but don't change too often, then caching is just the tool you need to speed up your web site.

Here is how caching works:

7.2 Caching with CherryPy

7.2.1 Where are pages stored ?

With CherryPy, all pages are saved in memory. This allows for maximum speed, but it also means that the size of your process will grow really fast if you cache lots of big pages. To avoid that, CherryPy provides a way to "purge" old pages that are in the cache.

7.2.2 How does it know if a page is already in the cache ?

The page that a server returns to a client usually depends on 3 parameters: CherryPy lets you define what your cache "key" will be. Two requests that have the same cache key will receive the same response from the server.

For instance, if your pages don't depend on cookies, your cache key could just be request.browserUrl. But if your pages depend on cookies, your cache key can be request.browserUrl+str(request.simpleCookie).

7.2.3 How do I control which pages I want to cache or not ?

It is very easy. All you have to do is use initRequest or initNonStaticRequest to set a couple of special variables if you want to use caching: Let's take an example: Here is what the initNonStaticRequest special function will look like:
import time

def initNonStaticRequest():
    if request.path.find('part1')==0:
        request.cacheKey=request.browserUrl
        request.cacheExpire=time.time()+30*60 # 30 minutes
    elif request.path.find('part2')==0:
        request.cacheKey=request.browserUrl
        request.cacheExpire=time.time()+12*60*60 # 12 hours
That's all ! Just restart your server and the pages will be cached

7.2.4 How do I control when the cache is purged ?

Since all cached pages are stored in memory, the memory usage of the server will grow really fast if it has to cache lots of different pages (or, to be more precise, "different cache keys"), especially if these pages are big. For this reason, you should only cache pages for which it will really make a speed difference (good candidates are pages that are complicated to build but that don't change too often).

If all your pages in the cache are requested very often, there isn't much you can do to lower the memory usage.

But if some of them are only requested once in a while, then it might be worth it to remove them from the cache when they haven't been requested for a long time. This will free up some memory.

To control how often CherryPy will purge the cache, use a parameter called flushCacheDelay in the section cache of the config file:

[cache]
flushCacheDelay=10 # In minutes
The default value for the flushCachDelay is zero, which means that the cache will never be flushed.

Note: This parameter is only useful in very specific cases, so you probably don't need to use it ...

See About this document... for information on suggesting changes.