Cache Handler Function
緩沖處理函數(shù)
As an alternative to using the default file-based caching mechanism, you can
specify a custom cache handling function that will be used to read, write
and clear cached files.
作為一個可選擇使用的默認基于文本的緩沖機制,你可以定制或者自定義緩沖處理函數(shù)來進行讀、寫、清除緩沖文件。
Create a function in your application that Smarty will use as a cache handler.
Set the name of it in the $cache_handler_func class variable. Smarty will now use this to handle cached data. The first argument
is the action, which will be one of 'read', 'write' and 'clear'. The second
parameter is the Smarty object. The third parameter is the cached content.
Upon a write, Smarty passes the cached content in these parameters. Upon a
'read', Smarty expects your function to accept this parameter by reference
and populate it with the cached data. Upon a 'clear', pass a dummy variable
here since it is not used. The fourth parameter is the name of the template
file (needed for read/write), the fifth parameter is the cache_id (optional),
and the sixth is the compile_id (optional).
如果要在應(yīng)用程序下建立一個函數(shù),SMARTY將會使用一個緩沖處理。設(shè)置這個函數(shù)名為 $cache
handler func 中類變量名。Smarty將使用這個函數(shù)來處理緩沖數(shù)據(jù)。第一個參數(shù)不管是‘讀’,‘寫’,‘清除’,都是動態(tài)的。第二個參數(shù)是
Smarty 對象。第三個參數(shù)是緩沖的內(nèi)容。在進行寫操作之前,Smarty 傳遞這些緩沖內(nèi)容給這些參量。在進行讀操作之前,Smarty預(yù)處理那些緩沖數(shù)據(jù)相關(guān)或者封裝好了的函數(shù)。在進行清除操作之前,從他沒有使用起,就傳遞一個虛擬變量。第四個參數(shù)是模板文件名(需要讀和寫的文件),第五個函數(shù)是緩沖ID(額外選項),然后第六個參數(shù)是編譯的ID(額外選項)。
Note: The last parameter ($exp_time) was added in Smarty-2.6.0.
注意:最新的參數(shù)名 ($exp_time) 在Smarty-2.6.0中才加入的。
Example 15-5. example using MySQL as a cache source
<?php
function mysql_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null)
{
// set db host, user and pass here
$db_host = 'localhost';
$db_user = 'myuser';
$db_pass = 'mypass';
$db_name = 'SMARTY_CACHE';
$use_gzip = false;
// create unique cache id
$CacheID = md5($tpl_file.$cache_id.$compile_id);
if(! $link = mysql_pconnect($db_host, $db_user, $db_pass)) {
$smarty_obj->_trigger_error_msg("cache_handler: could not connect to database");
return false;
}
mysql_select_db($db_name);
switch ($action) {
case 'read':
// save cache to database
$results = mysql_query("select CacheContents from CACHE_PAGES where CacheID='$CacheID'");
if(!$results) {
$smarty_obj->_trigger_error_msg("cache_handler: query failed.");
}
$row = mysql_fetch_array($results,MYSQL_ASSOC);
if($use_gzip && function_exists("gzuncompress")) {
$cache_contents = gzuncompress($row["CacheContents"]);
} else {
$cache_contents = $row["CacheContents"];
}
$return = $results;
break;
case 'write':
// save cache to database
if($use_gzip && function_exists("gzcompress")) {
// compress the contents for storage efficiency
$contents = gzcompress($cache_content);
} else {
$contents = $cache_content;
}
$results = mysql_query("replace into CACHE_PAGES values(
'$CacheID',
'".addslashes($contents)."')
");
if(!$results) {
$smarty_obj->_trigger_error_msg("cache_handler: query failed.");
}
$return = $results;
break;
case 'clear':
// clear cache info
if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
// clear them all
$results = mysql_query("delete from CACHE_PAGES");
} else {
$results = mysql_query("delete from CACHE_PAGES where CacheID='$CacheID'");
}
if(!$results) {
$smarty_obj->_trigger_error_msg("cache_handler: query failed.");
}
$return = $results;
break;
default:
// error, unknown action
$smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\"");
$return = false;
break;
}
mysql_close($link);
return $return;
}
?>
|
|