[Zope-Coders] Lazy items in REQUEST
Matthew T. Kromer
matt@zope.com
Tue, 06 Nov 2001 14:57:13 -0500
When doing an automatic traversal hook for the Session Tracking product
in Zope 2.5; the traversal hook goes to call a Session Data Manager to
get the current session, then stuffs this result object into
REQUEST.SESSION.
It occured to Chris and I that this is a wee bit expensive, since it
happens on EVERY REQUEST; you'd rather defer the penalty of actually
retrieving the session data until it is used.
To that end, I extended the REQUEST object to take a fifth class of
element, lazy items.
When REQUEST.set_lazy(name, callable) is called, it registers a lazy
callable with the key "name" into the REQUEST object. Should the
REQUEST object be asked for this name, and it not be found during the
normal search path, the lazy objects are checked. Then, if the name
exists in the lazy object area, its value is checked to see if it is
callable. If it is callable, it's invoked -- but regardless, the result
value is then stored in the request normally, and the lazy key is deleted.
In this way, for instance
<dtml-var REQUEST>
does not incur the penalty of looking up the session, but
<dtml-with REQUEST>
<dtml-var SESSION>
</dtml-with>
does.
However, I only modified the __getitem__, and keys methods, and added
the set_lazy method -- looks like everything else is working (items()
and values() use keys() and get()).
I'm soliciting feedback...