gavo.web.caching module

A simple caching system for nevow pages.

The basic idea is to monkeypatch the request object in order to snarf content and headers.

class gavo.web.caching.CacheItemBuilder(finishAction)[source]

Bases: object

an aggregator for web pages as they are written.

On successful page generation an function is called with the request and the content written as arguments.

addContent(data)[source]
finish(request)[source]
class gavo.web.caching.CachedPage(content, headers, lastModified)[source]

Bases: Resource

A piece of cached content.

This is built with the content to return, the headers, and a unix timestamp for the last modification time (as applicable). This is enough for it to work as a nevow resource (i.e., run a renderHTTP method.

For cache management, this has a lastUsed unix timestamp that is bumped for each renderHTTP call, and a size attribute that gives the length of the content.

render(request)[source]

Render a given resource. See L{IResource}’s render method.

I delegate to methods of self with the form ‘render_METHOD’ where METHOD is the HTTP that was used to make the request. Examples: render_GET, render_HEAD, render_POST, and so on. Generally you should implement those methods instead of overriding this one.

render_METHOD methods are expected to return a byte string which will be the rendered page, unless the return value is C{server.NOT_DONE_YET}, in which case it is this class’s responsibility to write the results using C{request.write(data)} and then call C{request.finish()}.

Old code that overrides render() directly is likewise expected to return a byte string or NOT_DONE_YET.

@see: L{IResource.render}

class gavo.web.caching.PageCache(maxSize=10000000)[source]

Bases: object

a cache for resources already served out.

This is kept per-RD, and it is removed when an RD is removed. It is allowed to grow to a certain size determined at construction time; it will stop adding new items when it’s full (this is under the assumption that something has gone wrong and something tries to cache things we shouldn’t cache).

When you add things here, make sure the key actually gives everything that might influence a response. Below, we make sure we don’t cache anything that has parameters, depends on the user. Compression, content negotiation, would have to be dealt with separately.

The key actually used is determined in getFromRDCache – and we should try hard to keep the key making logic local to that function.

add(key, cachedPage)[source]

enters content into the cache if it still fits.

cachedPage is assomed to have a content attribute that will be taken as a proxy for the size.

gavo.web.caching.enterIntoCacheAs(destKey, destDict)[source]

returns a finishAction that enters a page into destDict under key.

gavo.web.caching.getFromRDCache(request, rd, segments)[source]

returns a cached resource for segments on rd.

This will also instrument the request to enter the result into the cache. Hence, do not use this unless you are certain that the request is cacheable. Otherwise, use getFromServiceCache, which contains some sanity checks.

gavo.web.caching.getFromServiceCache(request, service, rendC, segments)[source]

returns a cached resource for service request if applicable, None otherwise.

Requests with arguments or a user info are never cacheable; we don’t look at content negotiation, though, so make sure whatever is content-negotiated isn’t marked cacheable by the renderer class rendC.

For cacheable requests for resources not in the cache, the function also instruments the request such that the rendered page is cached.

Cacheable pages also cause request’s lastModified to be set.

gavo.web.caching.instrumentRequestForCaching(request, finishAction)[source]

changes request such that finishAction is called with the request and the content written for a successful page render.