Files
smarty/docs/es/programmers/advanced-features/section-template-cache-handler-func.xml

158 lines
4.6 KiB
XML
Raw Normal View History

2004-10-07 18:50:52 +00:00
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="section.template.cache.handler.func">
<title>Funci<EFBFBD>n manipuladora de cache</title>
<para>
Como una alternativa al uso del mecanismo de caching por default basado
en archivo, usted puede especificar una funci<63>n habitual de manipulaci<63>n
de cache que ser<65> usada para leer, escribir y limpar archivos de cache.
2004-10-07 18:50:52 +00:00
</para>
<para>
Cree una funci<63>n en su aplicaci<63>n para que Smarty la use como un
manipulador de cache. Defina el nombre de la variable de clase en el
<link linkend="variable.cache.handler.func">$cache_handler_func</link>.
El Smarty ahora usara esta para manipular datos en el cache. El primer
par<61>metro es la acci<63>n, que puede ser uno de estos 'read', 'write' y
'clear'. El segundo par<61>metro es el objeto de Smarty. El tercer par<61>metro
es el contenido que esta en el cache. Sobre 'write', el Smarty pasa el
contenido en cache en estos par<61>metros. sobre 'read', el Smarty espera
que su funci<63>n acepte este par<61>metro por referencia y poblar estos con los
datos en cache. Sobre 'clear', el Smarty pasa una variable en cero desde
aqu<71> que esta no es usada. El cuarto par<61>metro es el nombre del archivo de
template(necesario para leer/escribir). El quinto par<61>metro es la cache_id
(opcional). El sexto par<61>metro es la compile_id (opcional).
</para>
<para>
NOTA: El ultimo par<61>metro ($exp_time) fue adicionado en el Smarty-2.6.0.
</para>
<example>
<title> ejemplo usando MySQL como una fuente de cache</title>
<programlisting role="php">
<![CDATA[
<?php
/*
ejemplo de uso:
include('Smarty.class.php');
include('mysql_cache_handler.php');
$smarty = new Smarty;
$smarty->cache_handler_func = 'mysql_cache_handler';
$smarty->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, &$smarty_obj, &$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;
// create unique 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':
// read cache from database
$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"]);
2004-10-07 18:50:52 +00:00
} else {
$cache_content = $row["CacheContents"];
2004-10-07 18:50:52 +00:00
}
$return = $results;
break;
case 'write':
// save cache to database
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':
// clear cache info
if(empty($cache_id) && empty($compile_id) && 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->_trigger_error_msg("cache_handler: query failed.");
}
$return = $results;
break;
default:
// error, unknown action
$smarty_obj->_trigger_error_msg("cache_handler: unknown action \"$action\"");
$return = false;
break;
}
mysql_close($link);
return $return;
}
?>
]]>
</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
-->