Files
smarty/docs/pt_BR/programmers/advanced-features/section-template-cache-handler-func.xml
2004-04-13 11:47:32 +00:00

154 lines
4.5 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="section.template.cache.handler.func">
<title>Função Manipuladora de Cache</title>
<para>
Como uma alternativa ao uso do mecanismo de caching padrão baseado em arquivo, você pode
especificar uma função de manipulação de cache customizada que será usada para ler, escrever
e limpar arquivos de cache.
</para>
<para>
Crie uma função em sua aplicação que o Smarty usará como um manipulador de cache. Defina o
nome dela na variável de classe
<link linkend="variable.cache.handler.func">$cache_handler_func</link>.
O Smarty agora usará esta para manipular dados no cache. O primeiro argumento é a ação,
que é um desses 'read', 'write' e 'clear'. O segundo parâmetro é o objeto do Smarty. O
terceiro parâmetro é o conteúdo que está no cache.
No write, o Smarty passa o conteúdo em
cache nestes parâmetros. No 'read', o Smarty espera sua função aceitar este parâmetro por
referência e preenche ele com os dados em cache. No 'clear', passa uma variável simulacra aqui
visto que ela não é usada. O quarto parâmetro
é o nome do arquivo de template (necessário para
ler/escrever), o quinto parâmetro é a cache_id (opcional),
e o sexto é a compile_id (opcional).
</para>
<para>
Note que: O último parâmetro ($exp_time)foi adicionado no Smarty-2.6.0.
</para>
<example>
<title>exemplo usando MySQL como uma fonte de cache</title>
<programlisting>
&lt;?php
/*
exemplo de uso:
include('Smarty.class.php');
include('mysql_cache_handler.php');
$smarty = new Smarty;
$smarty-&gt;cache_handler_func = 'mysql_cache_handler';
$smarty-&gt;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, &amp;$smarty_obj, &amp;$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;
// cria um cache id unico
$CacheID = md5($tpl_file.$cache_id.$compile_id);
if(! $link = mysql_pconnect($db_host, $db_user, $db_pass)) {
$smarty_obj-&gt;_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-&gt;_trigger_error_msg("cache_handler: query failed.");
}
$row = mysql_fetch_array($results,MYSQL_ASSOC);
if($use_gzip &amp;&amp; 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 &amp;&amp; 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-&gt;_trigger_error_msg("cache_handler: query failed.");
}
$return = $results;
break;
case 'clear':
// clear cache info
if(empty($cache_id) &amp;&amp; empty($compile_id) &amp;&amp; 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-&gt;_trigger_error_msg("cache_handler: query failed.");
}
$return = $results;
break;
default:
// error, unknown action
$smarty_obj-&gt;_trigger_error_msg("cache_handler: unknown action \"$action\"");
$return = false;
break;
}
mysql_close($link);
return $return;
}
?&gt;</programlisting>
</example>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->