`
free_bird816
  • 浏览: 198311 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

用memcatche代替smarty的文件缓存 转

阅读更多

smarty可以通过cache_handler_func更改缓存方式,具体例子可以先看看官网:http://www.smarty.net/manual/en/section.template.cache.handler.func.php


官网的例子是用mysql方式,可以修改一下,改为用memcache的方式进行缓存,代码如下:
function memcache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null)
{
$use_gzip = false;
// create unique cache id
$CacheID = md5($tpl_file.$cache_id.$compile_id);
$memcache = new Memcache;
$memcache->addServer('192.168.56.201',11211); //根据你实际的memcache集群设定
$memcache->addServer('192.168.56.202',11211);
$memcache->addServer('192.168.56.203',11211);
$memcache->addServer('192.168.56.204',11211);


switch ($action) {
case 'read':
// get cache from memcache
$getContents = $memcache->get($CacheID);
if(!$getContents) {
$smarty_obj->trigger_error("memcache_handler: get failed.");
}
if($use_gzip && function_exists("gzuncompress")) {
$cache_contents = gzuncompress($getContents);
} else {
$cache_contents = $getContents;
}
$return = $getContents;
break;
case 'write':
// save cache to memcache
if($use_gzip && function_exists("gzcompress")) {
// compress the contents for storage efficiency
$contents = gzcompress($cache_content);
} else {
$contents = $cache_content;
}
$writeContents = $memcache->set($CacheID,$contents);
if(!$writeContents) {
$smarty_obj->trigger_error("memcache_handler: set failed.");
}
$return = $writeContents;
break;
case 'clear':
// clear cache info
if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
// clear them all
$clearCache = $memcache->flush();
} else {
$clearCache = $memcache->delete($cache_id);
}
if(!$clearCache) {
$smarty_obj->trigger_error("memcache_handler: clear failed.");
}
$return = $clearCache;
break;
default:
// error, unknown action
$smarty_obj->trigger_error("cache_handler: unknown action \"$action\"");
$return = false;
break;
}


return $return;
}
 
值得注意的是:smarty2.6.x用cache_handler_func后有一个问题,不论是mysql或者是memcache或者是其他等,无论设定lifetime多长,程序每次运行都会更新cache,未达到缓存效果,不知是否smarty的bug,查看了一下smarty的源代码,发现读缓存的那个函数里没有返回缓存内容,因此程序未命中内容,所以会执行写缓存操作,导致每次都更新缓存,解决方法是打开core.read_cache_file.php的37行修改为:
$params['results'] = call_user_func_array($smarty->cache_handler_func,
                             array('read', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id']
分享到:
评论

相关推荐

    smarty两种局部缓存方法和自定义生成静态文件的例子

    smarty两种局部缓存方法和自定义生成静态文件的例子,smarty版本是2.6.26

    PHP100视频教程 30:PHP模板引擎Smarty缓存应用

     //缓存时间2、Smarty缓存的使用和清除 $smarty->display('cache.tpl', cache_id); //创建带ID的缓存 $smarty->clear_all_cache(); //清除所有缓存 $smarty->clear_cache('index.htm'); //清除index.tpl的缓存...

    (第30讲) PHP模板引擎Smarty缓存应用

    1、Smarty缓存的配置 2、Smarty缓存的使用和清除 3、Smarty局部缓存 4、MYSQL与Smarty的应用 暂时30张视频讲解,有空再上传!

    SMarty 3 文件包

    smarty3,最新的smarty文件,用于搭建smarty环境。

    PHP模板引擎Smarty的缓存用法_.docx

    PHP模板引擎Smarty的缓存用法_.docx

    PHP100视频教程 30:PHP模板引擎Smarty缓存应用.rar

    2、Smarty缓存的使用和清除  $smarty->display('cache.tpl', cache_id); //创建带ID的缓存  $smarty->clear_all_cache(); //清除所有缓存  $smarty->clear_cache('index.htm'); //清除index.tpl的缓存  $...

    Smarty局部缓存的几种方法简介

    很多时候我们在用smarty时,并不希望缓存整个页面,例如天气、股票等这类即时更新的信息。那么我们可以让其不缓存,这篇文章主要介绍了Smarty局部缓存方法,需要的朋友可以参考下

    php模板解析类文件缓存

    php类smarty解析的过程以及文件缓存

    smarty模板中文手册

    3. 缓存技术:Smarty选用的一种缓存技术,它可以将用户最终看到的HTML文件缓存成一个静态的HTML页,当设定Smarty的cache属性为true时,在Smarty设定的cachetime期内将用户的WEB请求直接转换到这个静态的HTML文件中来...

    smarty缓存用法分析

    主要介绍了smarty缓存用法,以实例形式分析了smarty缓存的特点及使用技巧,非常具有实用价值,需要的朋友可以参考下

    smarty缓存部分参考

    一、使用缓存 二、清除缓存 三、使用自定义缓存方式 四、局部关闭缓存

    PHP文件缓存smarty模板应用实例分析

    主要介绍了PHP文件缓存smarty模板应用方法,结合实例形式较为详细的分析了smarty模板缓存的相关使用技巧,需要的朋友可以参考下

    smarty模板文件更新

    smarty模板文件更新core.write_file.php文件,解决无法生成编译文件的问题

    Smarty PHP模板引擎 2.6.25(安装文件)

    Smarty库文件 :(在/libs/目录里的库文件) Smarty.class.php Smarty_Compiler.class.php Config_File.class.php debug.tpl /core/*.php (all of them) /plugins/*.php (all of them)

    smarty文件包

    smarty,大家都懂的,需要的拿走,不客气

    smarty商品分页

    使用smarty实现商品的分页 smarty模板 缓存

    php模版 smarty安装文件

    php模版 smarty安装文件 php模版 smarty安装文件

    smarty安装与入门

    smarty安装及初级使用 在PHP的世界里已经出现了各式各样的模板类,但就功能和速度来说Smarty还是一直处于领先地位,因为Smarty的功能相对强大,所以使用起来比其他一些模板类稍显复杂了一点。现在就用30分钟让您...

    Smarty_smarty_

    Smarty 模板引擎教程,内容主要有基本语法、变量、组合修改器、内建函数、自定义函数、配置文件、控制台调试、缓存、插件扩展、使用技巧和经验等,比较详细的一个中文手册

    smarty手册.chm

    18. Tips & Tricks [使用技巧和经验] Blank Variable Handling [空白变量处理] Default Variable Handling [默认变量处理] Passing variable title to header template [传递变量标题给头模板] Dates [日期] ...

Global site tag (gtag.js) - Google Analytics