linux smarty 缓存,Smarty缓冲处理函数

Cache Handler Function

缓冲处理函数

作为一个可选择使用的默认基于文本的缓冲机制,你可以定制或者自定义缓冲处理函数来进行读、写、清除缓冲文件。

如果要在应用程序下建立一个函数,SMARTY将会使用一个缓冲处理。设置这个函数名为 $cache handler func 中类变量名。Smarty将使用这个函数来处理缓冲数据。第一个参数不管是‘读’,‘写’,‘清除’,都是动态的。第二个参数是 Smarty 对象。第三个参数是缓冲的内容。在进行写操作之前,Smarty 传递这些缓冲内容给这些参量。在进行读操作之前,Smarty预处理那些缓冲数据相关或者封装好了的函数。在进行清除操作之前,从他没有使用起,就传递一个虚拟变量。第四个参数是模板文件名(需要读和写的文件),第五个函数是缓冲ID(额外选项),然后第六个参数是编译的ID(额外选项)。

Note: The last parameter ($exp_time) was added in Smarty-2.6.0.

注意:最新的参数名 ($exp_time) 在Smarty-2.6.0中才加入的。

/*

example usage:

// wwww.yiibai.com

include('Smarty.class.php');

include('mysql_cache_handler.php');

$smarty = new Smarty;

$smarty->cache_handler_func = 'mysql_cache_handler';

$smarty->display('index.tpl');

mysql database is expected in this format:

create database SMARTY_CACHE;

create table CACHE_PAGES(

CacheID char(32) PRIMARY KEY,

CacheContents MEDIUMTEXT NOT NULL

);

*/

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;

}

?>

¥ 我要打赏

纠错/补充

收藏

下一篇:哥,这回真没有了

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值