Subsections
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:
- The first time someone requests a page, the server builds the page normally and returns the page to the client
- The server also saves a copy of the page "somewhere" for later retrieval. It also sets a delay for how long this copy
can be used.
- The next time someone requests the same page, the server uses the copy it made instead of rebuilding the page.
- When the copy expires, the server builds a new version of the page.
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.
The page that a server returns to a client usually depends on 3 parameters:
- The URL that the client requested
- The parameters that the client sent (via a GET or a POST)
- The cookies that the client sent
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).
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:
- request.cacheKey:
this is the cache key as described in the previous section
- request.cacheExpire:
This is the time the cached version of the page will expire
Let's take an example:
- You have a web site with 3 main parts: /part1, /part2 and /part3
- Pages under /part1 are complicated to build, and the pages might change every 30 minutes: we'll use caching on this part and
the caching delay will be set to 10 minutes.
- Pages under /part2 are complicated to build, and the pages might change every day: we'll use caching on this part and the
caching delay will be set to 12 hours.
- Pages under /part3 are very fast to build, and not many people visit this area: we won't use caching on this part
- Pages only depend on the URL, not on cookies: we'll use the URL as the cache key
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
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.