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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
Caching placeholders and #cache
===============================
Dynamic placeholder - no cache
------------------------------
The template:
::
Dynamic variable: $voom
The command line and the output:
::
% voom='Voom!' python x.py --env
Dynamic variable: Voom!
The generated code:
::
write('Dynamic variable: ')
write(filter(VFS(SL,"voom",1))) # generated from '$voom' at line 1, col 20.
write('\n')
Just what we expected, like any other dynamic placeholder.
Static placeholder
------------------
The template:
::
Cached variable: $*voom
The command line and output:
::
% voom='Voom!' python x.py --env
Cached variable: Voom!
The generated code, with line numbers:
::
1 write('Cached variable: ')
2 ## START CACHE REGION: at line, col (1, 19) in the source.
3 RECACHE = True
4 if '19760169' not in self._cacheData:
5 pass
6 else:
7 RECACHE = False
8 if RECACHE:
9 orig_trans = trans
10 trans = cacheCollector = DummyTransaction()
11 write = cacheCollector.response().write
12 write(filter(VFS(SL,"voom",1))) # generated from '$*voom' at line 1,
# col 19.
13 trans = orig_trans
14 write = trans.response().write
15 self._cacheData['19760169'] = cacheCollector.response().getvalue()
16 del cacheCollector
17 write(self._cacheData['19760169'])
18 ## END CACHE REGION
19 write('\n')
That one little star generated a whole lotta code. First, instead
of an ordinary {VFS} lookup (searchList) lookup, it converted the
placeholder to a lookup in the {.\_cacheData} dictionary. Cheetah
also generated a unique key ({'19760169'}) for our cached item -
this is its cache ID.
Second, Cheetah put a pair of if-blocks before the {write}. The
first (lines 3-7) determine whether the cache value is missing or
out of date, and sets local variable {RECACHE} true or false. This
stanza may look unnecessarily verbose - lines 3-7 could be
eliminated if line 8 was changed to
::
if '19760169' not in self._cacheData:
- but this model is expandable for some of the cache features we'll
see below.
The second if-block, lines 8-16, do the cache updating if
necessary. Clearly, the programmer is trying to stick as close to
normal (dynamic) workflow as possible. Remember that {write}, even
though it looks like a local function, is actually a method of a
file-like object. So we create a temporary file-like object to
divert the {write} object into, then read the result and stuff it
into the cache.
Timed-refresh placeholder
-------------------------
The template:
::
Timed cache: $*.5m*voom
The command line and the output:
::
% voom='Voom!' python x.py --env
Timed cache: Voom!
The generated method's docstring:
::
"""
This is the main method generated by Cheetah
This cache will be refreshed every 30.0 seconds.
"""
The generated code:
::
1 write('Timed cache: ')
2 ## START CACHE REGION: at line, col (1, 15) in the source.
3 RECACHE = True
4 if '55048032' not in self._cacheData:
5 self.__cache55048032__refreshTime = currentTime() + 30.0
6 elif currentTime() > self.__cache55048032__refreshTime:
7 self.__cache55048032__refreshTime = currentTime() + 30.0
8 else:
9 RECACHE = False
10 if RECACHE:
11 orig_trans = trans
12 trans = cacheCollector = DummyTransaction()
13 write = cacheCollector.response().write
14 write(filter(VFS(SL,"voom",1))) # generated from '$*.5m*voom' at
# line 1, col 15.
15 trans = orig_trans
16 write = trans.response().write
17 self._cacheData['55048032'] = cacheCollector.response().getvalue()
18 del cacheCollector
19 write(self._cacheData['55048032'])
20 ## END CACHE REGION
21 write('\n')
This code is identical to the static cache example except for the
docstring and the first if-block. (OK, so the cache ID is different
and the comment on line 14 is different too. Big deal.)
Each timed-refresh cache item has a corrsponding private attribute
{.\_\_cache########\_\_refreshTime} giving the refresh time in
ticks (=seconds since January 1, 1970). The first if-block (lines
3-9) checks whether the cache value is missing or its update time
has passed, and if so, sets {RECACHE} to true and also schedules
another refresh at the next interval.
The method docstring reminds the user how often the cache will be
refreshed. This information is unfortunately not as robust as it
could be. Each timed-cache placeholder blindly generates a line in
the docstring. If all refreshes are at the same interval, there
will be multiple identical lines in the docstring. If the refreshes
are at different intervals, you get a situation like this:
::
"""
This is the main method generated by Cheetah
This cache will be refreshed every 30.0 seconds.
This cache will be refreshed every 60.0 seconds.
This cache will be refreshed every 120.0 seconds.
"""
The docstring tells only that "something" will be refreshed every
60.0 seconds, but doesn't reveal { which} placeholder that is. Only
if you know the relative order of the placeholders in the template
can you figure that out.
Timed-refresh placeholder with braces
-------------------------------------
This example is the same but with the long placeholder syntax. It's
here because it's a Cheetah FAQ whether to put the cache interval
inside or outside the braces. (It's also here so I can look it up
because I frequently forget.) The answer is: outside. The braces go
around only the placeholder name (and perhaps some output-filter
arguments.)
The template:
::
Timed with {}: $*.5m*{voom}
The output:
::
Timed with {}: Voom!
The generated code differs only in the comment. Inside the
cache-refresh if-block:
::
write(filter(VFS(SL,"voom",1))) # generated from '$*.5m*{voom}' at line 1,
#col 17.
If you try to do it this way:
::
Timed with {}: ${*.5m*voom} ## Wrong!
you get:
::
Timed with {}: ${*.5m*voom}
``${`` is not a valid placeholder, so it gets treated as ordinary
text.
#cache
------
The template:
::
#cache
This is a cached region. $voom
#end cache
The output:
::
This is a cached region. Voom!
The generated code:
::
1 ## START CACHE REGION: at line, col (1, 1) in the source.
2 RECACHE = True
3 if '23711421' not in self._cacheData:
4 pass
5 else:
6 RECACHE = False
7 if RECACHE:
8 orig_trans = trans
9 trans = cacheCollector = DummyTransaction()
10 write = cacheCollector.response().write
11 write('This is a cached region. ')
12 write(filter(VFS(SL,"voom",1))) # generated from '$voom' at line 2,
# col 27.
13 write('\n')
14 trans = orig_trans
15 write = trans.response().write
16 self._cacheData['23711421'] = cacheCollector.response().getvalue()
17 del cacheCollector
18 write(self._cacheData['23711421'])
19 ## END CACHE REGION
This is the same as the {$\*voom} example, except that the plain
text around the placeholder is inside the second if-block.
#cache with timer and id
------------------------
The template:
::
#cache timer='.5m', id='cache1'
This is a cached region. $voom
#end cache
The output:
::
This is a cached region. Voom!
The generated code is the same as the previous example except the
first if-block:
::
RECACHE = True
if '13925129' not in self._cacheData:
self._cacheIndex['cache1'] = '13925129'
self.__cache13925129__refreshTime = currentTime() + 30.0
elif currentTime() > self.__cache13925129__refreshTime:
self.__cache13925129__refreshTime = currentTime() + 30.0
else:
RECACHE = False
#cache with test: expression and method conditions
--------------------------------------------------
The template:
::
#cache test=$isDBUpdated
This is a cached region. $voom
#end cache
The template:
::
#cache id='cache1', test=($isDBUpdated or $someOtherCondition)
This is a cached region. $voom
#end cache
The output:
::
This is a cached region. Voom!
The first if-block in the generated code:
::
RECACHE = True
if '36798144' not in self._cacheData:
self._cacheIndex['cache1'] = '36798144'
elif (VFS(SL,"isDBUpdated",1) or VFS(SL,"someOtherCondition",1)):
RECACHE = True
else:
RECACHE = False
The second if-block is the same as in the previous example. If you
leave out the {()} around the test expression, the result is the
same, although it may be harder for the template maintainer to
read.
You can even combine arguments, although this is of questionable
value.
The template:
::
#cache id='cache1', timer='30m', test=$isDBUpdated or $someOtherCondition
This is a cached region. $voom
#end cache
The output:
::
This is a cached region. Voom!
The first if-block:
::
RECACHE = True
if '88939345' not in self._cacheData:
self._cacheIndex['cache1'] = '88939345'
self.__cache88939345__refreshTime = currentTime() + 1800.0
elif currentTime() > self.__cache88939345__refreshTime:
self.__cache88939345__refreshTime = currentTime() + 1800.0
elif VFS(SL,"isDBUpdated",1) or VFS(SL,"someOtherCondition",1):
RECACHE = True
else:
RECACHE = False
We are planning to add a {'varyBy'} keyword argument in the future
that will allow separate cache instances to be created for a
variety of conditions, such as different query string parameters or
browser types. This is inspired by ASP.net's varyByParam and
varyByBrowser output caching keywords. Since this is not
implemented yet, I cannot provide examples here.
|