[Twisted-web] html cache with timeout

Andrea Arcangeli andrea at cpushare.com
Tue Feb 1 20:23:20 MST 2005


On Wed, Feb 02, 2005 at 03:48:40AM +0100, Andrea Arcangeli wrote:
> rendering if I do something like tags.cached(..)[webform.renderForms()],
> I'm going to try it in a few minutes ;). If I can optimize the forms
> with this cache it'll be great.

Works great indeed ;). In the first form where I tested it, the
rendering time improved from 263msec, to 145msec. In some other form
with huge RequiredChoice this should be even more dramatic. If we do the
same thing for the xml rendering of the headers I'll like be able to
take the time down to 40msec or less.

Still this is nothing if compared to the httprendering cache (httprender
turns the rendering down to 5msec). So I'm most certainly going to use
both httprendering for the high traffic dynamic (but temporary static)
data, and the stan cache for the forms and other bits that allows
partial caching but that have always dynamic data inside.

Here below a very small improvement over your code. This way by default
if you don't pass the lifetime parameter it means "cache forever", and
that's exactly how I'm going to use it. This conforms with the
httprender API that also cache forever with a value == 0. Plus it
microoptimizes away one gettimeofday for each rendering.

If one is skilled enough to use rebuild (I'm not yet ;), he can sure do
a _CACHE = {} too, so I expect most usages will not pass the lifetime
and they will use a "forever" cache.

BTW, if we keep using the name _CACHE for all caches it'll be easier to
find all bits we have to flush to create an API just for rebuild or
manhole, to flush all caches.

--- ./nevow/flat/flatstan.py.~1~	2005-02-02 03:51:27.000000000 +0100
+++ ./nevow/flat/flatstan.py	2005-02-02 04:06:18.000000000 +0100
@@ -233,8 +233,9 @@ def SlotSerializer(original, context):
 _CACHE = {}
 def CachedSerializer(original, context):
     cached = _CACHE.get(original.name, None)
-    life = now()-original.lifetime
-    if cached and cached[0] > life:
+    _now = now()
+    life = _now-original.lifetime
+    if cached and (cached[0] > life or not original.lifetime):
         yield cached[1]
         return
     io = StringIO()
@@ -265,7 +266,7 @@ def CachedSerializer(original, context):
  
             yield d, lambda result: ''    
     result = io.getvalue()
-    _CACHE[original.name] = (now(), result)
+    _CACHE[original.name] = (_now, result)
     yield result
 
 def ContextSerializer(original, context):



More information about the Twisted-web mailing list