最新公司平台部门提出说smarty文件读取过于频繁,磁盘IO太大了。
查看了一些smarty的手册,支持 $cache handler func 可以自定义缓存处理函数
参见:http://www.itdoc.cn/manual/smarty/section.template.cache.handler.func.html
实例中给出了一个数据库封装了,read,write,clear三个动作。
这样使用memcache封装一下也很好实现的。
看了国外人写了一个先附上,他这边没有考虑分布式(如果需要自己完善一下)
function memcache_cache_handler($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null) {
// ref to the memcache object
$m = $GLOBALS['memcached_res'];
// the key to store cache_ids under, used for clearing
$key = 'smarty_caches';
// check memcache object
if (get_class($m) != 'memcached') {
$smarty_obj->trigger_error('cache_handler: $GLOBALS[\'memcached_res\'] is not a memcached object');
return false;
}
// unique cache id
$cache_id = md5($tpl_file.$cache_id.$compile_id);
switch ($action) {
case 'read':
// grab the key from memcached
$contents = $m->get($cache_id);
// use compression
if($smarty_obj->use_gzip && function_exists("gzuncompress")) {
$cache_content = gzuncompress($contents);
} else {
$cache_content = $contents;
}
$return = true;
break;
case 'write':
// use compression
if($smarty_obj->use_gzip && function_exists("gzcompress")) {
$contents = gzcompress($cache_content);
} else {
$contents = $cache_content;
}
// add the cache_id to the $key string
$caches = $m->get($key);
if (!is_array($caches)) {
$caches = array($cache_id);
$m->set($key, $caches);
} else if (!in_array($cache_id, $caches)) {
array_push($caches, $cache_id);
$m->set($key, $caches);
}
// store the value in memcached
$stored = $m->set($cache_id, $contents);
if(!$stored) {
$smarty_obj->trigger_error("cache_handler: set failed.");
}
$return = true;
break;
case 'clear':
if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
// get all cache ids
$caches = $m->get($key);
if (is_array($caches)) {
$len = count($caches);
for ($i=0; $i<$len; $i++) {
// assume no errors
$m->delete($caches[$i]);
}
// delete the cache ids
$m->delete($key);
$result = true;
}
} else {
$result = $m->delete($cache_id);
}
if(!$result) {
$smarty_obj->trigger_error("cache_handler: query failed.");
}
$return = true;
break;
default:
// error, unknown action
$smarty_obj->trigger_error("cache_handler: unknown action \"$action\"");
$return = false;
break;
}
return $return;
}
这些还没有真正上线运行,不知道他更新是否及时,年后会考虑部署。
参考资料:
http://www.itdoc.cn/manual/smarty/section.template.cache.handler.func.html
http://swag.dk/swag/kode/
http://lists.danga.com/pipermail/memcached/2007-September/005218.html
http://www.smarty.net/forums/viewtopic.php?t=10115
摘自:http://hi.baidu.com/lnnujxxy/blog/item/f7f610039d757e83d43f7c23.html
2010年6月21日 星期一
2008年8月27日 星期三
Using a Singleton with Smarty (syntax and usage questions)
$smarty = MySmarty::getInstance();
$smarty->testing(); #does work
class MySmarty
{
public static function getInstance()
{
if (self :: $instance === NULL)
{
self :: $instance = new MySmarty();
include('Smarty.class.php' );
self :: $smarty = New Smarty;
self :: $smarty -> caching = false;
self :: $smarty -> template_dir = $SMARTY_TEMPLATE_PATH."/templates/";
self :: $smarty -> compile_dir = $SMARTY_TEMPLATE_PATH."/templates_c/";
self :: $smarty -> config_dir = $SMARTY_TEMPLATE_PATH."/configs/";
self :: $smarty -> cache_dir = $SMARTY_TEMPLATE_PATH."/cache/";
self :: $smarty -> assign('app_name', 'HTML Email');
self :: $smarty -> compile_check = true;
self :: $smarty -> debugging = false;
}
return self :: $instance;
}
private function __clone()//do not allow clone
{
}
static private $smarty = false;
static private $instance = NULL;
// Example method
public function testing()
{
echo 'Testing!';
}
}
摘自:http://www.phpinsider.com/smarty-forum/viewtopic.php?t=13781&highlight=static
訂閱:
文章 (Atom)