On 26 May 2012 14:02, Ludovic Dubost <ludovic(a)xwiki.com> wrote:
Paul
Check the cache macro which you can use inside your own macro
http://extensions.xwiki.org/xwiki/bin/view/Extension/Cache+Macro
Interesting, thanks. I'm not quite sure how it would work in practice,
it seems that I'd end up with macros nested up to 3 times, at which
point my brain explodes :-)
It might help if I show the content of my current macro here:
{{groovy}}
import java.security.*
// Calculate SHA1 of content - the idea is that we can avoid
regenerating if we've calculated this output before
def content = xcontext.macro.content
def messageDigest = MessageDigest.getInstance("SHA1")
messageDigest.update(content.getBytes())
def contentSHA1 = new BigInteger(1,
messageDigest.digest()).toString(16).padLeft(40, '0')
// Generate the output - ideally, only if we don't have saved output
for this SHA1
def cmd = 'generate-png.exe'
def proc = cmd.execute()
proc << content
proc.out.close()
def png = proc.in.bytes
def out = png.encodeBase64().toString()
println "{{html}}"
println "<img src=\"data:image/png;base64,$out\"/>"
println "{{/html}}"
{{/groovy}}
It's running that executable that I want to skip - it will always give
the same output for the same input. The output should be persistent
(over restarts of XWiki, ideally). As a concrete example of
generate-png.exe, think of the Graphviz "dot" command. For any given
graph, the PNG won't change. Essentially, that's what I'm implementing
here (longer term, the idea would be that the precise command to
generate the image would be configurable).
I've looked at the PlantUML and Graphviz extensions, but I don't
really follow how they work. On the other hand, they seem pretty
complex compared to the code above, so I'm not sure how much
understanding them would help.
Paul.