Brief Description
The number of database queries is reduced by several times due to caching of dynamic snippets.
Installation
You should create the directory
/assets/plugins/cacheaccelerator.
In directory
cacheaccelerator you should create directory cache
(
/assets/plugins/cacheaccelerator/cache).
For both directories the
chmod 777 has to be set
Afterwards from downloaded archive you should copy the file
fileCache.php to the directory
/assets/plugins/cacheaccelerator
After that in the manager MODx choose
Elements ->
Elements management ->
Snippets ->
New Snippet.
Create a new snippet called
CacheAccelerator. The contents of file
CacheAccelerator_snippet.php should be copied there.
The created snippet should be saved. After this you ought to go to inlay
Plugins and click
Create plugin. The new plugin should be called
CacheAcceleratorClear. Copy the contents of file
CacheAcceleratorClear.plugin.txt.
Attention! After copying the plug-in contents go to inlay
System events, where you should set the check button for event
OnCacheUpdate and
OnSiteRefresh from the section
Cache Service Events! Push the button
Save. The installation of plugin is over.
The usage of CacheAccelerator
CacheAccelerator can be used just for all snitteps and can cache all issues. The usage of CacheAccelerator is illustrated with to examples bellow.
The calling of Dito is the following:
[!CacheAccelerator?
&snippetToCache=`Ditto`
&cacheId=`News`
&parents=`1`
&display=`10`
&paginate=`1`
&paginateAlwaysShowLinks=`1`
!]
Instead of calling Ditto the snippet CacheAccelerator is called. The name of cashed snippet (in this case Ditto) is specified in parameter
snippetToCache.
Next, there is
cacheId parameter. Its purpose is to divide the cashed content when there are several cashed snippets per one page. For example, the left menu, the last news block and rolling news. It can have any value that is correct for cached content.
Example of Jot calling:
[!CacheAccelerator?
&snippetToCache=`Jot`
&cacheId=`Comments`
&dropCacheField=`JotForm||post;true;2||;publish;2||;unpublish;2||;delete;2||;edit;2`
&noCacheGroups=`Site Admins`
&customfields=`name,email`
&pagination=`10`
&badwords=`*****`
&canmoderate=`Site Admins`
&captcha=`1`!]
!]
The cached snippet is also specified in parameter
snippetToCache.
Parameter
cacheId specifies the identifier of cashed content on given page.
Here are also two more parameters
dropCacheField and
noCacheGroups. Let’s regard them more detailed. News blocks and comments rolling differ from each other. News blocks are added with manager MODx and addition of any news item leads to cache clearing, while comments rolling let any user to create post. But in order make this post be available for other users, the cache CacheAccelerator has to be cleared. This activity may be necessary not only for Jot, but also for all other snippets, which are desired to be cached, but also this cache should be updated after concrete users actions.
This aim is achieved with parameter
dropCacheField. It includes the list of conditions for cache CacheAccelerator. The field name can be used as condition. If this name is detected in GET or POST queries, the cache will be updated. This condition can be also a comparison. You should enumerate field, value, the comparison method divided from each other with semicolumn. If the field name is empty, all existed fields are exposed to comparison.
Example:
&dropCacheField=`JotForm||post;true;2||;delete;2`
Here is specified the following:
- If query includes field JotForm
- If field post exist and its value is true
- If any of fields has value delete
- If any of these conditions is true, the cache will be cleared.
When any new message is posted to Jot, a new form is transmitted. This form contains field
JotForm. So after posting such message the cache will be cleared and information is kept to be timely. After the first query of this page cache will be created again and all other following queries will be processed from cache.
Parameter
noCacheGroups contains the list which is divided with ||.
This list consists of users groups. For these users the data are got from cache and snippet is executed each time when call occurred. As instance, let’s regard the moderator forms. This form and web-user form are different. Moderator form has control buttons unlike user form. But If this form is stored in cache, all other users will be able to get moderator form which doesn’t correspond to there queries.
noCacheRoles – the manager roles list for which the caching processing is not applied. E.g. `Administrator||Editor`
Cache can be cleared from call:
From page:
[!CacheAccelerator? &clearCache=`1`!]
From snippet:
$modx->runSnippet("CacheAccelerator", array("clearCache" => 1))
All placeholders specified by cache snippet are also cached and are notified for calling from cache.
Example:
[!CacheAccelerator?&snippetToCache=`Ditto`&cacheId=`News`]]
Showing [ start ] - [ stop ] from [ total ] News<br>
[ previous ] [ pages ] [ next ]<br>
If the plugin
PHx is installed, the placeholders could be cut.
In this case for page navigation usage snippet
include can be applied. (
http://code.google.com/p/modx-shopkeeper/downloads/detail?name=include_snippet.zip):
[!include? &placeholder=`pages`!]
If you don’t want provide cache processing for those users who are authorized in manager MODx, change snippet code:
To
if($noCacheRoles)
$nocache = isUserRole(explode("||",$noCacheRoles)) ? 2 : $nocache;
else
$nocache = $modx->checkSession() ? 2 : $nocache;
The parameters:
snippetToCache – the snippets name for caching. E. g. ‘Ditto’.
cacheId – the cached snipped identifier inside the page. E. g. ‘News’.
dropCacheField – the list of fields and conditions for them, by which the cache is reset. E. g. `JotForm||post;true;2`
noCacheGroups – the list of web-users groups for which the caching is not applied. E.g. `admins||moderators`
noCacheRoles – the manager roles list for which the caching processing is not applied. E.g. `Administrator||Editor`
checkURL – create separate cache for different URL (1|0). Is enabled by default. It is recommended to enable it for snippets that use the page navigation.
clearCache – setting it to 1 leads to forced cache clearing.
The conditions list:
1 != Not equal
2 = Equal
3 < less than
4 > greater than
5 <= less or equal
6 >= greater of equal
7 Contains
8 Not contains