[Zope] RE: [Zope-dev] Caching

Evan Simpson evan@4-am.com
Sun, 12 Sep 1999 11:18:55 -0500


Rob Page wrote:

> > "<!--#cache--> <!--#foo bar--> <!--#/cache-->", where foo bar
> > can be an SQL call..
>
> SQL Methods have a tunable parameter for cacheing results on the
> Advanced tab.
>
> For other kinds of objects the ZODB takes pains to keep frequently used
> objects in memory.  Objects are retired after they've been untouched for
> some time.

How about *result* cacheing for non-SQL objects?  On numerous occasions I
have wished that I could tell Zope to cache the output of a document or
method across requests.  Then I would want to clear the cached value either
after some elapsed time, or on-demand as the underlying data changed.  Even
better would be the ability to write a method which decides on a
per-request basis when to clear the cache.

I'm tempted to try using PythonMethod globals as cache, but I'm a little
worried about concurrency issues.  Something like:

global cache
now = ZopeTime()
if cache is not None:
    cached_on, txt = cache
    if now >= cached_on + 0.5: # every half-day
        cache = None
if cache is None:
    txt = myDTMLDoc(_, REQUEST)
    cache = (now, txt)
return txt

The worst that should happen is wasted concurrent attempts to refresh the
cache, right?