[Twisted-web] feedback on svn updates

Andrea Arcangeli andrea at cpushare.com
Fri Mar 11 05:54:58 MST 2005


1) The cached branch on top of svn trunk generates an exception in
CachedSerializer, unfortunately I didn't save the exception. Since I
don't strictly need the tags.cached, I left only the rend.Page cache
below in my tree:

Index: Nevow/nevow/rend.py
===================================================================
--- Nevow/nevow/rend.py	(revision 1334)
+++ Nevow/nevow/rend.py	(working copy)
@@ -30,6 +30,7 @@
 from nevow import flat
 from nevow.util import log
 from nevow import util
+from nevow import url
 
 import formless
 from formless import iformless
@@ -381,6 +382,8 @@
         self.children[name] = child
     
 
+_CACHE = {}
+
 class Page(Fragment, ConfigurableFactory, ChildLookupMixin):
     """A page is the main Nevow resource and renders a document loaded
     via the document factory (docFactory).
@@ -394,8 +397,37 @@
     afterRender = None
     addSlash = None
 
+    cache = False
+    lifetime = -1
+    __lastCacheRendering = 0
+
     flattenFactory = lambda self, *args: flat.flattenFactory(*args)
 
+    def hasCache(self, ctx):
+        if not self.cache:
+            return
+
+        _now = now() # run gettimeofday only once
+        timeout = _now > self.__lastCacheRendering + self.lifetime and \
+                  self.lifetime >= 0
+        c = self.lookupCache(ctx)
+        if timeout or c is None:
+            # stop other renders
+            self.__lastCacheRendering = _now
+            c = None
+        return c
+    def cacheRendered(self, ctx, c):
+        if not self.cache:
+            return
+        # overwrite the deferred with the data
+        self.storeCache(ctx, c)
+    def cacheIDX(self, ctx):
+        return str(url.URL.fromContext(ctx))
+    def storeCache(self, ctx, c):
+        _CACHE[self.cacheIDX(ctx)] = c
+    def lookupCache(self, ctx):
+        return _CACHE.get(self.cacheIDX(ctx))
+
     def renderHTTP(self, ctx):
         if self.beforeRender is not None:
             return util.maybeDeferred(self.beforeRender,ctx).addCallback(
@@ -421,11 +453,18 @@
             if self.afterRender is not None:
                 return util.maybeDeferred(self.afterRender,ctx)
 
-        if self.buffered:
+        c = self.hasCache(ctx)
+        if c is not None:
+            finishRequest()
+            return c
+
+        if self.buffered or self.cache:
             io = StringIO()
             writer = io.write
             def finisher(result):
-                request.write(io.getvalue())
+                c = io.getvalue()
+                self.cacheRendered(ctx, c)
+                request.write(c)
                 return util.maybeDeferred(finishRequest).addCallback(lambda r: result)
         else:
             writer = request.write
@@ -507,7 +546,6 @@
             else:
                 ## Use the redirectAfterPost url
                 ref = str(redirectAfterPost)
-            from nevow import url
             refpath = url.URL.fromString(ref)
             magicCookie = '%s%s%s' % (now(),request.getClientIP(),random.random())
             refpath = refpath.replace('_nevow_carryover_', magicCookie)


2) self.locateConfigurable returns a deferred, so I changed my code to
get the retval via addCallback, so this is fine.

3) The renderForms has changed layout with <legend>. You can see the
layout of the new code (already online) here
https://www.cpushare.com/verify_email/. Now I don't dislike this change
but I would like to keep the legend at the center of the page (not on
the left), is that possible with some css tweak? I tried to do that in
the fieldset but that screwed the alignment of everything else too. I
only want the legend to be centered instead. Hints?

Thanks and keep up the great work.



More information about the Twisted-web mailing list