缓存处理函数

    在缺省的基于文件的缓存机制以外,你可以指定一个定制缓存处理函数,可以用来读、写、清除缓存文件。

    在你的应用中创建一个函数以便Smarty用来作为缓存处理器。在$cache_handler_func类变量中设置它的名字。Smarty将使用该函数来处理缓存数据。

例子15-5. 使用MySQL作为缓存源的例子

<?php
/**************************************************
示范用法:

include('Smarty.class.php');
include('mysql_cache_handler.php');

$smarty = new Smarty;
$smarty->cache_handler_func = 'mysql_cache_handler';

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


MySQL数据库的格式为:

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)
{
    
// 设置数据库主机、用户和密码
    
$db_host 'localhost';
    
$db_user 'myuser';
    
$db_pass 'mypass';
    
$db_name 'SMARTY_CACHE';
    
$use_gzip false;

    
// 设置唯一的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':
            
// 从数据库中读入缓存
            
$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_content gzuncompress($row['CacheContents']);
            } else {
                
$cache_content $row['CacheContents'];
            }
            
$return $results;
            break;
        case 
'write':
            
// 将缓存保存至数据库

            
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':
            
// 清除缓存
            
if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
                
// 全部清除
                
$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:
            
// 错误,未知的动作
            
$smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\"");
            
$return false;
            break;
    }
    
mysql_close($link);
    return 
$return;

}

?>