[Moin-user] Caching generated content from a parser
Paul Moore
p.f.moore at gmail.com
Fri Dec 2 15:45:35 EST 2011
I'm writing a parser which generates content (an image) based on the
text provided to the parser - something like the Google Chart or
Graphviz parsers. I'm trying to decide where would be the best place
to store the generated content. The Google Chart parser uses the
cache, which feels like the best option to me. (The Graphviz parser's
use of attachments feels like it exposes too many of the gory details
to the user for me).
It's easy to write the code to cache the image:
def format(self, formatter):
key = cache.key(...)
if not cache.exists(self.request, key):
# Generate data
cache.put(self.request, key=key, data=data, filename="image.png")
self.request.write(formatter.image(src=cache.url(self.request, key)))
At the moment, I generate the key using self.raw (the raw data from
the formatter) but that means that every time the data changes, a new
cache entry is created and the old one is never removed.
What would be *much* better would be if I could generate a unique ID
for each region on the page using my formatter, then I could
regenerate the *same* cache entry when the data changed. Is that
possible? Can a formatter see its context like this? (I seem to recall
once seeing some code that set a counter on the request object, but I
can't get anything like that to work...)
Outside of the "pure" parser activities, I have some housekeeping
concerns. A very active wiki could end up generating lots of images,
and while disk is cheap, image files are larger than text files, and
I'd like some means of keeping disk usage reasonable. There are two
aspects to this:
1. I don't know enough about the MoinMoin caching system to know when
caches are cleared, and how. Are page-level cached items cleared when
the page cache is cleared? (If so, that's a point against using the
cache action, as that uses a wiki-level cache).
2. I don't quite know *when* I would age cache entries out yet, but my
instincts say that when a new version of the page is created would be
a reasonable time. Ideally, I'd check at that stage what images are
cached for the page, and delete any that related to an older version
of the page and hadn't been used "recently". (Getting the details
right might be fiddly, but that's the general idea). Is there a way of
setting some code to run when a new version of a page is created like
this? (And if so, is it possible to associate it with the formatter
somehow, so that it only runs for pages using my formatter?)
Pointers to documentation would be great - as would suggestions as to
how to find my way through the relevant bits of the source.
Thanks,
Paul
PS I have a feeling all of this would be much easier in Moin 2, with
its mimetype items, but I get the feeling that Moin 2 is still a
reasonable way away from being ready for production use, so I think I
have to stick with 1.9 for this :-(
More information about the Moin-user
mailing list