cache_handler and cleare_cache
| From: | Marcin Staniszczak | Date: | Sat, 12 Jul 2003 10:58:33 +0000 |
| Subject: | cache_handler and cleare_cache | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-9875@lists.php.net to get a copy of this message | ||
Hi,
I have problem with my code. I write this code:
$dbConnection = null;
function templateIinitialize($db) {
global $dbConnection;
$dbConnection = $db;
$template = new Smarty();
$template->cache_handler_func = 'db_cache_handler';
$template->caching = true;
$template->compile_dir = ES_ROOT_PATH.'Templates\templates_c';
$template->cache_lifetime = 31104000;
$template->compile_check = false;
$template->register_resource("db",
array("db_get_template","db_get_timestamp","db_get_secure","db_get_trusted")
);
return $template;
}
//==========================================================================
===
//%%PRIVATE
//==========================================================================
===
function db_cache_handler($action, &$smarty_obj, &$cache_content,
$tpl_file=null, $cache_id=null, $compile_id=null) {
global $dbConnection;
$IDCache = md5($tpl_file.$cache_id.$compile_id);
switch($action) {
case 'read':
if(!($record = $dbConnection->Execute(SQL_ReadCache($IDCache,
CFG_DB_PREFIX.TABLE_CACHE_PAGES))))
$smarty_obj->_trigger_error_msg('cache_handler: query
failed. Error no'.$dbConnection->ErrorNo());
if($record->fields['ret']!=0) {
if(CFG_TPL_CACHE_USE_GZIP && function_exists('gzuncompress'))
$cache_content =
gzuncompress($record->fields['CacheContents'], 16777215);
else
$cache_content = $record->fields['CacheContents'];
}
return $record;
break;
case 'write':
$contents = $cache_content;
if(CFG_TPL_CACHE_USE_GZIP && function_exists('gzcompress'))
$contents = gzcompress($cache_content);
if(!($record = $dbConnection->Execute(SQL_ReadCache($IDCache,
CFG_DB_PREFIX.TABLE_CACHE_PAGES))))
$smarty_obj->_trigger_error_msg('cache_handler: query
failed. Error no'.$dbConnection->ErrorNo());
if($record->fields['ret']!=0) {
if(!($record =
$dbConnection->Execute(SQL_UpdateCache($IDCache,
$dbConnection->Quote($contents), CFG_DB_PREFIX.TABLE_CACHE_PAGES))))
$smarty_obj->_trigger_error_msg('cache_handler: query
failed. Error no'.$dbConnection->ErrorNo());
} else {
if(!($record =
$dbConnection->Execute(SQL_InsertCache($IDCache,
$dbConnection->Quote($contents), CFG_DB_PREFIX.TABLE_CACHE_PAGES))))
$smarty_obj->_trigger_error_msg('cache_handler: query
failed. Error no'.$dbConnection->ErrorNo());
}
return $record;
break;
case 'clear':
if(empty($cache_id) && empty($compile_id) && empty($tpl_file)) {
if(!($record =
$dbConnection->Execute(SQL_ClearAllCache(CFG_DB_PREFIX.TABLE_CACHE_PAGES))))
$smarty_obj->_trigger_error_msg('cache_handler: query
failed. Error no'.$dbConnection->ErrorNo());
} else {
if(!($record =
$dbConnection->Execute(SQL_ClearCache($IDCache,
CFG_DB_PREFIX.TABLE_CACHE_PAGES))))
_trigger_error_msg('cache_handler: query failed. Error
no'.$dbConnection->ErrorMsg());
}
return $record;
break;
default:
return false;
break;
}
}
function db_get_template($tpl_name, &$tpl_source, &$smarty_obj) {
global $dbConnection;
if(!($record =
$dbConnection->Execute(SQL_GetTemplate($dbConnection->Quote($tpl_name),
CFG_DB_PREFIX.TABLE_TEMPLATES))))
return false;
if($record->fields['ret']==1) {
$tpl_source = stripslashes($record->fields['Source']);
return true;
} else {
return false;
}
}
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) {
global $dbConnection;
if(!($record =
$dbConnection->Execute(SQL_GetTimestamp($dbConnection->Quote($tpl_name),
CFG_DB_PREFIX.TABLE_TEMPLATES))))
return false;
if($record->fields['ret']==1) {
$tpl_timestamp = $record->fields['ModyficationTime'];
return true;
} else
return false;
}
function db_get_secure($tpl_name, &$smarty_obj) {
return true;
}
function db_get_trusted($tpl_name, &$smarty_obj) {
return true;
}
function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
{
trigger_error("Config_File error: $error_msg", $error_type);
}
Now, when I use it, it's all ok. But if I write:
$dbConnection = DBConnect();
$authorization = new cAuthorization($dbConnection);
$template = templateIinitialize($dbConnection);
$template->assign('test',1);
$template->display('db:test.tpl', 'test|1);
in second PHP file:
$dbConnection = DBConnect();
$authorization = new cAuthorization($dbConnection);
$template = templateIinitialize($dbConnection);
$template->assign('test',2);
$template->display('db:test.tpl', 'test|2);
and now i 3 PHP file:
$dbConnection = DBConnect();
$authorization = new cAuthorization($dbConnection);
$template = templateIinitialize($dbConnection);
$template->clear_cache('db:test.tpl', 'test');
or
$template->clear_cache(null, 'test');
it doesn't work. Cache (for 'test|1' and 'test|2') isn't cleared. Why?
Can
you help me?
Marcin