File: node8.html

package info (click to toggle)
cherrypy 0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,324 kB
  • ctags: 1,759
  • sloc: python: 14,411; sh: 6,915; perl: 2,472; makefile: 76
file content (238 lines) | stat: -rw-r--r-- 8,977 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>7. How to use caching</title>
<META NAME="description" CONTENT="7. How to use caching">
<META NAME="keywords" CONTENT="howto">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="howto.css" type='text/css'>
<link rel="first" href="howto.html">
<link rel="contents" href="contents.html" title="Contents">

<LINK REL="next" HREF="node9.html">
<LINK REL="previous" HREF="node7.html">
<LINK REL="up" HREF="howto.html">
<LINK REL="next" HREF="node9.html">
<meta name='aesop' content='information'>
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node7.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A HREF="howto.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A HREF="node9.html"><img src="../icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy HowTo</td>
<td><A HREF="node1.html"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node7.html">6. How to use</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="howto.html">CherryPy HowTo</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node9.html">8. How can webdesigners</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>

<UL CLASS="ChildLinks">
<LI><A href="node8.html#SECTION008100000000000000000">7.1 Introduction to caching</a>
<LI><A href="node8.html#SECTION008200000000000000000">7.2 Caching with CherryPy</a>
<UL>
<LI><A href="node8.html#SECTION008210000000000000000">7.2.1 Where are pages stored ?</a>
<LI><A href="node8.html#SECTION008220000000000000000">7.2.2 How does it know if a page is already in the cache ?</a>
<LI><A href="node8.html#SECTION008230000000000000000">7.2.3 How do I control which pages I want to cache or not ?</a>
<LI><A href="node8.html#SECTION008240000000000000000">7.2.4 How do I control when the cache is purged ?</a>
</ul></ul>
<!--End of Table of Child-Links-->
<HR>

<H1><A NAME="SECTION008000000000000000000">
7. How to use caching</A>
</H1>

<H1><A NAME="SECTION008100000000000000000">
7.1 Introduction to caching</A>
</H1>
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.

<P>
Here is how caching works:

<UL>
<LI>The first time someone requests a page, the server builds the page normally and returns the page to the client
</LI>
<LI>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.
</LI>
<LI>The next time someone requests the same page, the server uses the copy it made instead of rebuilding the page.
</LI>
<LI>When the copy expires, the server builds a new version of the page.
</LI>
</UL>

<P>

<H1><A NAME="SECTION008200000000000000000">
7.2 Caching with CherryPy</A>
</H1>

<H2><A NAME="SECTION008210000000000000000">
7.2.1 Where are pages stored ?</A>
</H2>
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.

<P>

<H2><A NAME="SECTION008220000000000000000">
7.2.2 How does it know if a page is already in the cache ?</A>
</H2>
The page that a server returns to a client usually depends on 3 parameters:

<UL>
<LI>The URL that the client requested
</LI>
<LI>The parameters that the client sent (via a GET or a POST)
</LI>
<LI>The cookies that the client sent
</LI>
</UL>
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.

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

<P>

<H2><A NAME="SECTION008230000000000000000">
7.2.3 How do I control which pages I want to cache or not ?</A>
</H2>
It is very easy. All you have to do is use <var>initRequest</var> or <var>initNonStaticRequest</var> to set a couple of
special variables if you want to use caching:

<UL>
<LI><var>request.cacheKey</var>:
this is the cache key as described in the previous section
</LI>
<LI><var>request.cacheExpire</var>:
This is the time the cached version of the page will expire
</LI>
</UL>
Let's take an example:

<UL>
<LI>You have a web site with 3 main parts: /part1, /part2 and /part3
</LI>
<LI>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.
</LI>
<LI>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.
</LI>
<LI>Pages under /part3 are very fast to build, and not many people visit this area: we won't use caching on this part
</LI>
<LI>Pages only depend on the URL, not on cookies: we'll use the URL as the cache key
</LI>
</UL>
Here is what the <var>initNonStaticRequest</var> special function will look like:
<div class="verbatim"><pre>
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
</pre></div>
That's all ! Just restart your server and the pages will be cached

<P>

<H2><A NAME="SECTION008240000000000000000">
7.2.4 How do I control when the cache is purged ?</A>
</H2>
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).

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

<P>
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.

<P>
To control how often CherryPy will purge the cache, use a parameter called <var>flushCacheDelay</var> in the section <var>cache</var>
of the config file:
<div class="verbatim"><pre>
[cache]
flushCacheDelay=10 # In minutes
</pre></div>
The default value for the <var>flushCachDelay</var> is zero, which means that the cache will never be flushed.

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

<P>

<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node7.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A HREF="howto.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A HREF="node9.html"><img src="../icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy HowTo</td>
<td><A HREF="node1.html"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node7.html">6. How to use</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="howto.html">CherryPy HowTo</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node9.html">8. How can webdesigners</A>
<hr>
<span class="release-info">Release 0.10, documentation updated on 19 March 2004.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>