Option to have a static cache for page content

Currently i have a big Page with a lot of macro content (my own macro).
It takes arround 5 seconds to open the page. (everytime i open the page my macro gets executed). And no, a cache macro can’t help here. The content of the macro only changes if the content has also been completely edited. Not after a period of time.Thereotically I could also write the result of the macro statically through a scheduler. However, the problem here is that the page content also contains the result of the macro.
Example of my macro:

{{includedoc id=“8989” /}}

Leads to an execution of an api access.

[[8989: Some example doc, wit a big description, which should not be included in the search >> http://...../docs?system=82367437238745437&doc=8989]]

Is there a way to save and display a static version of the page when saving the page? But without influencing the search? Or is there a way to give Solr a different search source text for this page?

1 Like

You could configure the cache macro with a very high time period. Alternatively, if your macro is implemented in Java, you could also add a cache internally in the macro.

Another option, if you only use that macro on few pages, you could also configure the rendering cache for those pages. Again, you can simply configure a high cache duration.

@MichaelHamann thanks for the answer :slight_smile: I still have questions regarding the two topics. I’m still a bit confused, especially when it comes to development.

Java Development

In general, however, I have two questions about development.

  1. Macro Cache in java: I currently use my own solution (However, I’m not sure whether this applies at all. I am using a static variable here.), is there a simelar solution like “cached” from this wiki: WikiMacroTutorial but in java?
  2. ExtendingMacro “Macro preparation”. I don’t quite understand that yet? Generally it is said here that it is only executed once and you can put things there? How exactly does it work now? Examples:
    2.1. Page is saved → “prepare” → the content is saved → page is called → “execute” for each macro?
    2.2. Page is called up → “prepare” but only once per page call → “execute” for each macro?
    2.3 Page is called up → “prepare” for each macro → “execute” for each macro?

But here is also an example with: “this.contentParser.prepareContentWiki(macroBlock);” What exactly does it do then? With point 2.1, the content would be saved pre-rendered beforehand couldn’t I also use it to save my content? And if so, how? I could then query my api during prepare and save it in the macroblock. This should then be very performant even without a cache, because I then only have to render the information in execute without querying.
But with 2.2 it would make no sense as each macro has its own content. So I’ll rule that out.
2.3 Would be pointless, then I can just write everything in execute.

Page Rendering Cache

Is there also a way to configure this via the gui? It can happen that it changes from time to time, and I would hate to have to restart the server every time.

  1. I have currently tested prepare, it doesn’t improve performance. So i think it doesn’t work? I need to look further.

prepare:

public void prepare(MacroBlock macroBlock) throws MacroPreparationException {
        String id = macroBlock.getParameter("id");
        if (id != null && !id.isEmpty()) {
            try {
                id = generateUrlString(path);
            } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException
                    | InvalidKeySpecException | NoSuchPaddingException | IOException e) {
                throw new MacroPreparationException(e.getMessage(), e);
            }
            macroBlock.setAttribute("testattr.idurl", id);
        }

        this.macroContentParser.prepareContentWiki(macroBlock);
    }

execute:

String id = parameters.getId();

        Object url = macroBlock.getAttribute("testattr.idurl");

        if (url == null) {
            try {
                id = generateUrlString(id);
            } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException
                    | InvalidKeySpecException | NoSuchPaddingException | IOException e) {
                throw new MacroExecutionException(e.getMessage(), e);
            }
            macroBlock.setAttribute("testattr.idurl", id);
        } else {
            String strurl = url.toString();
            if (strurl.isEmpty()) {
                try {
                    id = generateUrlString(id);
                } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException
                        | NoSuchAlgorithmException
                        | InvalidKeySpecException | NoSuchPaddingException | IOException e) {
                    throw new MacroExecutionException(e.getMessage(), e);
                }
                macroBlock.setAttribute("testattr.idurl", id);
            } else {
                id = strurl;
            }
        }

@MichaelHamann I think I can just wrap my macro in a cache macro? Right? or is there a java api directly from xwiki? The other question about prepare, is there any documentation?

I’d recommend to try to make your macro async and either wrap it with the {{async}} macro, you’ll benefit from caching for free, see https://extensions.xwiki.org/xwiki/bin/view/Extension/Async%20Macro/ or if you want your macro to be always async/cached and not let that up to the user, then doing it in java. See https://extensions.xwiki.org/xwiki/bin/view/Extension/Rendering%20Module/Async/

Here’s an example of an Async macro: jira/jira-macro/jira-macro-platform/src/main/java/org/xwiki/contrib/jira/config/internal/AsyncJIRAMacro.java at master · xwiki-contrib/jira · GitHub

@vmassol good idea. I try to make it async. :slight_smile:

Can you perhaps give me a tip regarding the import?

The import org.xwiki.rendering.async cannot be resolved

import org.xwiki.rendering.async.internal.block.AbstractBlockAsyncRenderer;

I have not found anything in the maven repo in the direction of async.
https://mvnrepository.com/artifact/org.xwiki.rendering

    <dependency>
      <groupId>org.xwiki.rendering</groupId>
      <artifactId>xwiki-platform-rendering-async-default</artifactId>
      <version>${platform.version}</version>
    </dependency>
Missing artifact org.xwiki.rendering:xwiki-platform-rendering-async-default:jar:${platform.version}

EDIT:

Found it: “org.xwiki.platform”

    <dependency>
      <groupId>org.xwiki.platform</groupId>
      <artifactId>xwiki-platform-rendering-async-default</artifactId>
      <version>${rendering.version}</version>
    </dependency>