new cache implementation

Jacob Smullyan <[email protected]> Thu, 13 Oct 2005 14:01:21 -0400
Newsgroups gmane.comp.web.skunkweb
Message-ID <[email protected]>
Hi all --

I am now developing (again) a generalized, standalone version of the
skunkweb cache here (in the skunk.cache package, ignore the rest,
which is decidedly pre-alpha):

  http://svn.berlios.de/viewcvs/skunkweb/trunk/skunk4/

This is a general-purpose library and has no skunkweb dependencies of
any kind (although it is easy to integrate with skunkweb, and I'm
using it in that context).  Folks who want a caching layer for PyDO
might be interested.

There are no docs yet, so here is a brief introduction.

The cache has the very similar caching semantics as the AE.Cache, but
generalized to all callables, rather than just components.  You can
invoke the cache by passing a callable + various arguments to its
"call" method, but for most purposes it is easiest to take a cache
instance and wrap it in a decorator, and use it for memoizing
functions:

  cacheInstance=DiskCache('/path/to/cache')
  cache=CacheDecorator(cacheInstance, 
                       defaultExpiration="30s", 
                       defaultPolicy=YES)

  @cache(expiration="4m")
  def foo():
      return time.time()

  x=foo()
  y=foo()
  assert x==y
  z=foo(cache=FORCE)
  assert z > y

The decorated function takes 3 additional optional arguments: cache
(in deference to the keyword used in the <:component:> tag, but I may
rename this to "policy", as it is elsewhere in the new api),
expiration, and ondefer; these override arguments passed to the cache
decorator, which in turn override the decorator instance's defaults.
Also, if a function decides internally not to be cached, it can raise
a BypassCache exception with a return value.

Deferal obviously requires that cache call info needs to be saved and
processed by something later, so the cache package itself doesn't
implement that; if you want to defer, pass a callable to ondefer that
stores the data (in a list or Queue, probably), and work it off in
some way appropriate to your application.  (Typically, you'd set a
default defer handler in the decorator.)

There are currently 3 different cache backends -- disk, memory, and
memcached.  It is possible with the disk and memory backends to
invalidate the cache for an entire function, and the operation is
rapid in both cases (although the disk will require vacuuming later).
This effectively solves SkunkWeb's old cache clearing problem,
especially in combination with currying ("partial" in python 2.5).

Cheers,

js 

-- 
Jacob Smullyan
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDTqDxuqamFyFXXLIRApm6AJ43ncSDrdCIdPumEipfSQEzBvbuTgCgv2Ge
5bKfGyX6bp+y+rX41F8lmWM=
=Li07
-----END PGP SIGNATURE-----