| 1 |
Caching placeholders and #cache |
| 2 |
=============================== |
| 3 |
|
| 4 |
|
| 5 |
Dynamic placeholder - no cache |
| 6 |
------------------------------ |
| 7 |
|
| 8 |
|
| 9 |
The template: |
| 10 |
|
| 11 |
:: |
| 12 |
|
| 13 |
Dynamic variable: $voom |
| 14 |
|
| 15 |
The command line and the output: |
| 16 |
|
| 17 |
:: |
| 18 |
|
| 19 |
% voom='Voom!' python x.py --env |
| 20 |
Dynamic variable: Voom! |
| 21 |
|
| 22 |
The generated code: |
| 23 |
|
| 24 |
:: |
| 25 |
|
| 26 |
write('Dynamic variable: ') |
| 27 |
write(filter(VFS(SL,"voom",1))) # generated from '$voom' at line 1, col 20. |
| 28 |
write('\n') |
| 29 |
|
| 30 |
Just what we expected, like any other dynamic placeholder. |
| 31 |
|
| 32 |
Static placeholder |
| 33 |
------------------ |
| 34 |
|
| 35 |
|
| 36 |
The template: |
| 37 |
|
| 38 |
:: |
| 39 |
|
| 40 |
Cached variable: $*voom |
| 41 |
|
| 42 |
The command line and output: |
| 43 |
|
| 44 |
:: |
| 45 |
|
| 46 |
% voom='Voom!' python x.py --env |
| 47 |
Cached variable: Voom! |
| 48 |
|
| 49 |
The generated code, with line numbers: |
| 50 |
|
| 51 |
:: |
| 52 |
|
| 53 |
1 write('Cached variable: ') |
| 54 |
2 ## START CACHE REGION: at line, col (1, 19) in the source. |
| 55 |
3 RECACHE = True |
| 56 |
4 if '19760169' not in self._cacheData: |
| 57 |
5 pass |
| 58 |
6 else: |
| 59 |
7 RECACHE = False |
| 60 |
8 if RECACHE: |
| 61 |
9 orig_trans = trans |
| 62 |
10 trans = cacheCollector = DummyTransaction() |
| 63 |
11 write = cacheCollector.response().write |
| 64 |
12 write(filter(VFS(SL,"voom",1))) # generated from '$*voom' at line 1, |
| 65 |
# col 19. |
| 66 |
13 trans = orig_trans |
| 67 |
14 write = trans.response().write |
| 68 |
15 self._cacheData['19760169'] = cacheCollector.response().getvalue() |
| 69 |
16 del cacheCollector |
| 70 |
17 write(self._cacheData['19760169']) |
| 71 |
18 ## END CACHE REGION |
| 72 |
|
| 73 |
19 write('\n') |
| 74 |
|
| 75 |
That one little star generated a whole lotta code. First, instead |
| 76 |
of an ordinary {VFS} lookup (searchList) lookup, it converted the |
| 77 |
placeholder to a lookup in the {.\_cacheData} dictionary. Cheetah |
| 78 |
also generated a unique key ({'19760169'}) for our cached item - |
| 79 |
this is its cache ID. |
| 80 |
|
| 81 |
Second, Cheetah put a pair of if-blocks before the {write}. The |
| 82 |
first (lines 3-7) determine whether the cache value is missing or |
| 83 |
out of date, and sets local variable {RECACHE} true or false. This |
| 84 |
stanza may look unnecessarily verbose - lines 3-7 could be |
| 85 |
eliminated if line 8 was changed to |
| 86 |
|
| 87 |
:: |
| 88 |
|
| 89 |
if '19760169' not in self._cacheData: |
| 90 |
|
| 91 |
- but this model is expandable for some of the cache features we'll |
| 92 |
see below. |
| 93 |
|
| 94 |
The second if-block, lines 8-16, do the cache updating if |
| 95 |
necessary. Clearly, the programmer is trying to stick as close to |
| 96 |
normal (dynamic) workflow as possible. Remember that {write}, even |
| 97 |
though it looks like a local function, is actually a method of a |
| 98 |
file-like object. So we create a temporary file-like object to |
| 99 |
divert the {write} object into, then read the result and stuff it |
| 100 |
into the cache. |
| 101 |
|
| 102 |
Timed-refresh placeholder |
| 103 |
------------------------- |
| 104 |
|
| 105 |
|
| 106 |
The template: |
| 107 |
|
| 108 |
:: |
| 109 |
|
| 110 |
Timed cache: $*.5m*voom |
| 111 |
|
| 112 |
The command line and the output: |
| 113 |
|
| 114 |
:: |
| 115 |
|
| 116 |
% voom='Voom!' python x.py --env |
| 117 |
Timed cache: Voom! |
| 118 |
|
| 119 |
The generated method's docstring: |
| 120 |
|
| 121 |
:: |
| 122 |
|
| 123 |
""" |
| 124 |
This is the main method generated by Cheetah |
| 125 |
This cache will be refreshed every 30.0 seconds. |
| 126 |
""" |
| 127 |
|
| 128 |
The generated code: |
| 129 |
|
| 130 |
:: |
| 131 |
|
| 132 |
1 write('Timed cache: ') |
| 133 |
2 ## START CACHE REGION: at line, col (1, 15) in the source. |
| 134 |
3 RECACHE = True |
| 135 |
4 if '55048032' not in self._cacheData: |
| 136 |
5 self.__cache55048032__refreshTime = currentTime() + 30.0 |
| 137 |
6 elif currentTime() > self.__cache55048032__refreshTime: |
| 138 |
7 self.__cache55048032__refreshTime = currentTime() + 30.0 |
| 139 |
8 else: |
| 140 |
9 RECACHE = False |
| 141 |
10 if RECACHE: |
| 142 |
11 orig_trans = trans |
| 143 |
12 trans = cacheCollector = DummyTransaction() |
| 144 |
13 write = cacheCollector.response().write |
| 145 |
14 write(filter(VFS(SL,"voom",1))) # generated from '$*.5m*voom' at |
| 146 |
# line 1, col 15. |
| 147 |
15 trans = orig_trans |
| 148 |
16 write = trans.response().write |
| 149 |
17 self._cacheData['55048032'] = cacheCollector.response().getvalue() |
| 150 |
18 del cacheCollector |
| 151 |
19 write(self._cacheData['55048032']) |
| 152 |
20 ## END CACHE REGION |
| 153 |
|
| 154 |
21 write('\n') |
| 155 |
|
| 156 |
This code is identical to the static cache example except for the |
| 157 |
docstring and the first if-block. (OK, so the cache ID is different |
| 158 |
and the comment on line 14 is different too. Big deal.) |
| 159 |
|
| 160 |
Each timed-refresh cache item has a corrsponding private attribute |
| 161 |
{.\_\_cache########\_\_refreshTime} giving the refresh time in |
| 162 |
ticks (=seconds since January 1, 1970). The first if-block (lines |
| 163 |
3-9) checks whether the cache value is missing or its update time |
| 164 |
has passed, and if so, sets {RECACHE} to true and also schedules |
| 165 |
another refresh at the next interval. |
| 166 |
|
| 167 |
The method docstring reminds the user how often the cache will be |
| 168 |
refreshed. This information is unfortunately not as robust as it |
| 169 |
could be. Each timed-cache placeholder blindly generates a line in |
| 170 |
the docstring. If all refreshes are at the same interval, there |
| 171 |
will be multiple identical lines in the docstring. If the refreshes |
| 172 |
are at different intervals, you get a situation like this: |
| 173 |
|
| 174 |
:: |
| 175 |
|
| 176 |
""" |
| 177 |
This is the main method generated by Cheetah |
| 178 |
This cache will be refreshed every 30.0 seconds. |
| 179 |
This cache will be refreshed every 60.0 seconds. |
| 180 |
This cache will be refreshed every 120.0 seconds. |
| 181 |
""" |
| 182 |
|
| 183 |
The docstring tells only that "something" will be refreshed every |
| 184 |
60.0 seconds, but doesn't reveal { which} placeholder that is. Only |
| 185 |
if you know the relative order of the placeholders in the template |
| 186 |
can you figure that out. |
| 187 |
|
| 188 |
Timed-refresh placeholder with braces |
| 189 |
------------------------------------- |
| 190 |
|
| 191 |
|
| 192 |
This example is the same but with the long placeholder syntax. It's |
| 193 |
here because it's a Cheetah FAQ whether to put the cache interval |
| 194 |
inside or outside the braces. (It's also here so I can look it up |
| 195 |
because I frequently forget.) The answer is: outside. The braces go |
| 196 |
around only the placeholder name (and perhaps some output-filter |
| 197 |
arguments.) |
| 198 |
|
| 199 |
The template: |
| 200 |
|
| 201 |
:: |
| 202 |
|
| 203 |
Timed with {}: $*.5m*{voom} |
| 204 |
|
| 205 |
The output: |
| 206 |
|
| 207 |
:: |
| 208 |
|
| 209 |
Timed with {}: Voom! |
| 210 |
|
| 211 |
The generated code differs only in the comment. Inside the |
| 212 |
cache-refresh if-block: |
| 213 |
|
| 214 |
:: |
| 215 |
|
| 216 |
write(filter(VFS(SL,"voom",1))) # generated from '$*.5m*{voom}' at line 1, |
| 217 |
#col 17. |
| 218 |
|
| 219 |
If you try to do it this way: |
| 220 |
|
| 221 |
:: |
| 222 |
|
| 223 |
Timed with {}: ${*.5m*voom} ## Wrong! |
| 224 |
|
| 225 |
you get: |
| 226 |
|
| 227 |
:: |
| 228 |
|
| 229 |
Timed with {}: ${*.5m*voom} |
| 230 |
|
| 231 |
``${`` is not a valid placeholder, so it gets treated as ordinary |
| 232 |
text. |
| 233 |
|
| 234 |
#cache |
| 235 |
------ |
| 236 |
|
| 237 |
|
| 238 |
The template: |
| 239 |
|
| 240 |
:: |
| 241 |
|
| 242 |
#cache |
| 243 |
This is a cached region. $voom |
| 244 |
#end cache |
| 245 |
|
| 246 |
The output: |
| 247 |
|
| 248 |
:: |
| 249 |
|
| 250 |
This is a cached region. Voom! |
| 251 |
|
| 252 |
The generated code: |
| 253 |
|
| 254 |
:: |
| 255 |
|
| 256 |
1 ## START CACHE REGION: at line, col (1, 1) in the source. |
| 257 |
2 RECACHE = True |
| 258 |
3 if '23711421' not in self._cacheData: |
| 259 |
4 pass |
| 260 |
5 else: |
| 261 |
6 RECACHE = False |
| 262 |
7 if RECACHE: |
| 263 |
8 orig_trans = trans |
| 264 |
9 trans = cacheCollector = DummyTransaction() |
| 265 |
10 write = cacheCollector.response().write |
| 266 |
11 write('This is a cached region. ') |
| 267 |
12 write(filter(VFS(SL,"voom",1))) # generated from '$voom' at line 2, |
| 268 |
# col 27. |
| 269 |
13 write('\n') |
| 270 |
14 trans = orig_trans |
| 271 |
15 write = trans.response().write |
| 272 |
16 self._cacheData['23711421'] = cacheCollector.response().getvalue() |
| 273 |
17 del cacheCollector |
| 274 |
18 write(self._cacheData['23711421']) |
| 275 |
19 ## END CACHE REGION |
| 276 |
|
| 277 |
This is the same as the {$\*voom} example, except that the plain |
| 278 |
text around the placeholder is inside the second if-block. |
| 279 |
|
| 280 |
#cache with timer and id |
| 281 |
------------------------ |
| 282 |
|
| 283 |
|
| 284 |
The template: |
| 285 |
|
| 286 |
:: |
| 287 |
|
| 288 |
#cache timer='.5m', id='cache1' |
| 289 |
This is a cached region. $voom |
| 290 |
#end cache |
| 291 |
|
| 292 |
The output: |
| 293 |
|
| 294 |
:: |
| 295 |
|
| 296 |
This is a cached region. Voom! |
| 297 |
|
| 298 |
The generated code is the same as the previous example except the |
| 299 |
first if-block: |
| 300 |
|
| 301 |
:: |
| 302 |
|
| 303 |
RECACHE = True |
| 304 |
if '13925129' not in self._cacheData: |
| 305 |
self._cacheIndex['cache1'] = '13925129' |
| 306 |
self.__cache13925129__refreshTime = currentTime() + 30.0 |
| 307 |
elif currentTime() > self.__cache13925129__refreshTime: |
| 308 |
self.__cache13925129__refreshTime = currentTime() + 30.0 |
| 309 |
else: |
| 310 |
RECACHE = False |
| 311 |
|
| 312 |
#cache with test: expression and method conditions |
| 313 |
-------------------------------------------------- |
| 314 |
|
| 315 |
|
| 316 |
The template: |
| 317 |
|
| 318 |
:: |
| 319 |
|
| 320 |
#cache test=$isDBUpdated |
| 321 |
This is a cached region. $voom |
| 322 |
#end cache |
| 323 |
|
| 324 |
|
| 325 |
The template: |
| 326 |
|
| 327 |
:: |
| 328 |
|
| 329 |
#cache id='cache1', test=($isDBUpdated or $someOtherCondition) |
| 330 |
This is a cached region. $voom |
| 331 |
#end cache |
| 332 |
|
| 333 |
The output: |
| 334 |
|
| 335 |
:: |
| 336 |
|
| 337 |
This is a cached region. Voom! |
| 338 |
|
| 339 |
The first if-block in the generated code: |
| 340 |
|
| 341 |
:: |
| 342 |
|
| 343 |
RECACHE = True |
| 344 |
if '36798144' not in self._cacheData: |
| 345 |
self._cacheIndex['cache1'] = '36798144' |
| 346 |
elif (VFS(SL,"isDBUpdated",1) or VFS(SL,"someOtherCondition",1)): |
| 347 |
RECACHE = True |
| 348 |
else: |
| 349 |
RECACHE = False |
| 350 |
|
| 351 |
The second if-block is the same as in the previous example. If you |
| 352 |
leave out the {()} around the test expression, the result is the |
| 353 |
same, although it may be harder for the template maintainer to |
| 354 |
read. |
| 355 |
|
| 356 |
You can even combine arguments, although this is of questionable |
| 357 |
value. |
| 358 |
|
| 359 |
The template: |
| 360 |
|
| 361 |
:: |
| 362 |
|
| 363 |
#cache id='cache1', timer='30m', test=$isDBUpdated or $someOtherCondition |
| 364 |
This is a cached region. $voom |
| 365 |
#end cache |
| 366 |
|
| 367 |
The output: |
| 368 |
|
| 369 |
:: |
| 370 |
|
| 371 |
This is a cached region. Voom! |
| 372 |
|
| 373 |
The first if-block: |
| 374 |
|
| 375 |
:: |
| 376 |
|
| 377 |
RECACHE = True |
| 378 |
if '88939345' not in self._cacheData: |
| 379 |
self._cacheIndex['cache1'] = '88939345' |
| 380 |
self.__cache88939345__refreshTime = currentTime() + 1800.0 |
| 381 |
elif currentTime() > self.__cache88939345__refreshTime: |
| 382 |
self.__cache88939345__refreshTime = currentTime() + 1800.0 |
| 383 |
elif VFS(SL,"isDBUpdated",1) or VFS(SL,"someOtherCondition",1): |
| 384 |
RECACHE = True |
| 385 |
else: |
| 386 |
RECACHE = False |
| 387 |
|
| 388 |
We are planning to add a {'varyBy'} keyword argument in the future |
| 389 |
that will allow separate cache instances to be created for a |
| 390 |
variety of conditions, such as different query string parameters or |
| 391 |
browser types. This is inspired by ASP.net's varyByParam and |
| 392 |
varyByBrowser output caching keywords. Since this is not |
| 393 |
implemented yet, I cannot provide examples here. |
| 394 |
|
| 395 |
|