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 not self._cacheData.has_key('19760169'):
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 RECHARGE true or false.
This stanza may look unnecessarily verbose - lines 3-7 could be eliminated if
line 8 was changed to
if not self._cacheData.has_key('19760169'):
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.