Static Page Caching
From FuseWiki
ci77Za <a href="http://ibzgbxvekrih.com/">ibzgbxvekrih</a>, [url=http://xdrmqqttcvxk.com/]xdrmqqttcvxk[/url], [link=http://woivgotiokwg.com/]woivgotiokwg[/link], http://jgthsveisejd.com/
Contents |
[edit] How to use static page caching
[edit] Enabling the Static Cache
1. To enable the static cache, add the line below to the file config/Common.conf.php
define('STATIC_CACHE_ENABLED', true);
2. Then, in config/routes.conf.php, add the 'static_cache' route option for each page you want to cache, like so:
FuseURIRouter::route_connect( 'Services/{id}', array(
'action' => 'view',
'controller' => 'Service',
'static_cache' => true,
'requirements' => array(
'id' => '/\d+/',
)
)
);
This page will now be cached in the static/ directory of your site, and the cached version will be displayed as long as those files exist.
[edit] Disabling the static cache when certain conditions are true
Oftentimes it is the case that under certain conditions (e.g. an admin login), you do not want the page to be rendered from cache, but rather to be rendered dynamically. You can add a global callback function to determine when to ignore the cached page. If the callback function returns false, the static cache is ignored, even when 'static_cache' is set to true. Example:
1. in config/Common.conf.php, add:
define ('STATIC_CACHE_CHECK_CALLBACK', 'do_static_cache');
function do_static_cache() {
//if this function returns false, the cached file will be ignored, and the page will not be re-cached
if ( $_COOKIE['admin_logged_in'] ) {
return false;
}
else {
return true;
}
}
Note: If the static_cache key in the route options array is itself an array, and that array has a key of always with a nonzero value, the static cache will always be used, regardless of the outcome of the STATIC_CACHE_CHECK_CALLBACK_FUNCTION. See below for more details.
[edit] Route Options for the Static Cache
The static_cache route option can either be set to true, which will enable static caching with the default options, or it can be an array that accepts the following keys as options:
- always - always use the cached version of the page if it exists, regardless of the return value for the global STATIC_CACHE_CHECK_CALLBACK function
- check_callbacks - one or more functions that need to return true for this page to be cached. Note that these individual callbacks WILL override always
Example:
FuseURIRouter::route_connect( 'Services/{id}', array(
'action' => 'view',
'controller' => 'Service',
'static_cache' => array( 'always' => true, 'check_callbacks' => array('only_if_i_return_true') )
'requirements' => array(
'id' => '/\d+/',
)
)
);
[edit] Flushing the cache / re-caching pages
[edit] Flushing automatically by controller methods
By default, in the FuseDataController, pages are re-cached when the add, edit, delete, or sortable_update methods are called. This is done via the static_cache_flush member of the controller, which looks like this by default:
public $static_cache_flush =
array(
'add' => array( 'method' => array('show_list', 'view'), 'id' => 'id*' ),
'edit' => array( 'method' => array('show_list', 'view'), 'id' => 'id*' ),
'delete' => array( 'method' => array('show_list', 'view'), 'id' => 'id*' ),
'sortable_update' => array( 'method' => 'show_list', 'id' => 'id*' )
);
the key for each entry in the static_cache_flush is the method name to apply the flush rules to, with the value set to an array of options. The options array can take the following keys:
- method - A single method names or an array of method names. Any cache files generated by the methods here will be cleared when the main method is called. In our example above, the 'add' method would clear any cache files generated by the 'show_list' or 'view' methods
- id - The name of the controller parameter that identifies a particular cache file. For example, if the add() method is called in our ServiceController with the 'id' controller parameter set to 5, the options above will clear Service-View-5.cache and Service-List-5.cache. Adding a "*" at the end ensures that Files such as Service-View-5-Page2.cache will also be cleared.
[edit] Flushing Manually
You can use the StaticFile/StaticFile class to clear the cache manually.
Example for manually flushing all files generated for the 'service' with an id of '5', generated by the view() method:
StaticFile::Flush_by_controller_method('Service', 'view', '5*');
