create new tag
, view all tags

How to cache a sidebar for better performance

2013-12-27 - 01:08:15 by PeterThoeny in Deployment
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:

5,20,35,50 * * * * (cd /var/www/twiki/bin; ./view topic=Sandbox.WebLeftBar raw=expandvariables > ../pub/Sandbox/WebLeftBar/cache.txt)

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:

 "defined 'raw'"
 else="<div style='display: none;'>"
...sidebar content...
 "defined 'raw'"
 "defined 'raw'"

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.



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:

   * Set LB_QUERY_ATTACHMENTS = %ATTACHMENTLIST{topic="%DEFAULT%" format="%PREFIX% * icon:%PUBURL%/%SYSTEMWEB%/TWikiDocGraphics/attachfile.gif $fileName"}%
   * Set LB_QUERY_CHILDREN = %SEARCH{"parent.name='%DEFAULT%'" type="query" nonoise="true" format="%PREFIX% * $percntSPACEOUT{\"$topic\"}$percnt$n$percntLB_QUERY_CHILDREN{\"$topic\" PREFIX=\"%PREFIX% \"}$percnt$n$percntLB_QUERY_ATTACHMENTS{\"$topic\" PREFIX=\" %PREFIX%\"}$percnt"}% 

Then, in WebLeftBar I can choose which topics to display in an expandable tree:

%TREEBROWSER{"file" title="users" shared="left" noroot="true"}% %LB_QUERY_CHILDREN{"UserList" PREFIX=" "}%

-- Michael Sprague - 2013-12-27


Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2014-05-16 - PeterThoeny

Twitter Delicious Facebook Digg Google Bookmarks E-mail LinkedIn Reddit StumbleUpon    
  • Help
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.