On 05/26/2012 08:52 AM, Paul Moore wrote:
  I am writing a macro that performs a relatively
expensive calculation
 to generate content for display (a PNG image) based on user input. The
 basic macro works fine, but I'd like to cache the generated content to
 save recalculating it when I don't need to.
 My idea is that I generate a SHA1 digest of the macro content, and
 persistently cache the output generated for that input. I'm thinking
 of using an LRU cache to avoid keeping massive amounts of data that'll
 never get used again. The question I have is where I can put the cache
 so that it is saved with the page, and I can access and update it from
 the macro code. I suspect that the answer is to attach an object to
 the page somehow, but I've been looking round the documentation and
 can't find any examples of how I might do this (TBH, I'm struggling to
 find my way round the documentation, so it may be that I've missed
 something obvious - if so, then by all means point me to the right
 place :-))
 As the object is a PNG image, I'm currently base64-encoding it, and
 using a data URL to display it. But if it's possible to cache the data
 in such a way that the raw data is (or can be made) directly
 addressible via a URL, that would be even better.
 Can anyone help?
 Thanks,
 Paul. 
Some resources that you can use:
https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwi…
is the proper way of creating caches. You declare an @Inject dependency
on that, then use the createNewCache method (if you don't plan on
running a cluster, or don't think that the cached resources should be
shared inside a cluster, then it's slightly better for performance to
call createNewLocalCache). You can then use this cache to store some
entries.
https://github.com/xwiki/xwiki-commons/blob/master/xwiki-commons-core/xwiki…
is going to give you the location of the temporary storage directory.
Combine this with
https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwi…
and you have a nice way of serving image files stored on the disk to the
HTTP client. Unfortunately, this way you have to implement your own LRU
functionality and make sure you clean up files, otherwise you'll keep
filling up the disk with temporary files.
--
Sergiu Dumitriu
http://purl.org/net/sergiu/