If your TWiki has a sidebar, top bar or bottom bar that has a lot of dynamic content it can slow down your whole TWiki browsing experience. For example, displaying topic children in a hierarchical tree using TreePlugin can be slow. In this case it can pay off to cache the included sidebar and update it in regular intervals.
This is a step by step guide on how to cache the WebLeftBar in the Sandbox web. The same can be applied to any other included topic in any web.
1. Create the cache directory:
Using the shell, create the attachment directory of the WebLeftBar where the cache file will be stored:
# cd twiki/pub
# mkdir Sandbox/WebLeftBar
# chown apache:apache Sandbox/WebLeftBar
The last command will set the ownership of the directory to the apache user. This user can vary based on the OS your TWiki is installed.
2. Install a scheduled task to update the cache:
On Linux, install a cron job to update the cache every 15 minutes. On other OSes check platform dependent documentation on scheduled tasks. It is important to do the scheduled task as the webserver user, which is apache for RHEL & CentOS. Example:
We change to the TWiki bin directory and call the TWiki view script. The script has admin privileges if called from the shell or a cron job, e.g. this works even if the topic is access restricted.
The topic parameter specifies what topic to cache; the raw=expandvariables parameter expands the TWiki variables and returns the topic content in raw format. We redirect that output to file cache.txt in the topic's attachment directory.
3. Make sidebar aware of cache:
Let's change the sidebar to deliver the cache by default, and the non-cached content when the cache is refreshed. Edit Sandbox.WebLeftBar and add the following - the ...sidebar content... indicates the current content:
The cron job adds a raw=expandvariables parameter during cache refresh. We use this do deliver topic content conditionally.
If the raw parameter is missing, e.g. in normal topic view, the first and second IF statement hide the dynamic sidebar content. The last IF statement includes the cache file. There is also a STARTINCLUDE that defines the starting point when including this topic. That is, in normal topic view and when included, the cached topic content is returned.
If the raw parameter is present, e.g. during cache refresh, the dynamic topic content is returned with variables expanded. The INCLUDE is suppressed - we do not want to cache the sidebar content twice.
That is it. When the sidebar is included, it in turn includes the cache. This setup can speed up your TWiki site considerably provided you have slow content in included topics.
TopMenuSkin - default skin, has documentation on how to enable a sidebar
I wrote this blog post in response to support question SID-01844: Setup a topic to be rendered in the background.
-- Peter Thoeny - 2013-12-27
Excellent! Thanks, Peter. I really like having that familiar tree structure of content for navigability. I've defined the following variables which enable my TWiki to crawl through a topic's children including attachments: