Files
smarty/docs/id/programmers/advanced-features/section-template-cache-handler-func.xml
2007-09-18 17:45:46 +00:00

192 lines
5.3 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<sect1 id="section.template.cache.handler.func">
<title>Fungsi Pengendali Cache</title>
<para>
Sebagai alternatif untuk menggunakan mekanisme cache berbasis-file
standar, anda bisa menetapkan fungsi penanganan cache kustom yang
akan digunakan untuk
<literal>membaca</literal>, <literal>menulis</literal> dan
<literal>membersihkan</literal> file yang di-cache.
</para>
<para>
Buat sebuah fungsi dalam aplikasi anda yang akan digunakan Smarty
sebagai pengendali cache. Set namanya dalam variabel kelas
<link linkend="variable.cache.handler.func">
<parameter>$cache_handler_func</parameter></link>
. Smarty
sekarang akan menggunakan ini untuk menangani data yang di-cache.
</para>
<itemizedlist>
<listitem><para>
Argumen pertama adalah aksi, yang akan berupa salah satu dari
<literal>read</literal>, <literal>write</literal> dan
<literal>clear</literal>.
</para></listitem>
<listitem><para>
Parameter kedua adalah obyek Smarty.
</para></listitem>
<listitem><para>Paremeter ketiga adalah konten yang di-cache.
Setelah <literal>menulis</literal>, Smarty mengoper konten yang
di-cache dalam parameter ini. Setelah <literal>membaca</literal>,
Smarty mengharapkan fungsi anda untuk menerima parameter ini
dengan referensi dan menempatkannya dengan data yang di-cache.
Setelah <literal>membersihkan</literal>, mengoper variabel dummy
di sini karena ia tidak digunakan.
</para></listitem>
<listitem><para>
Parameter ke empat adalah <parameter>nama</parameter> file template
,
diperlukan untuk membaca/menulis.
</para></listitem>
<listitem><para>
Parameter ke lima adalah opsional <parameter>$cache_id</parameter>.
</para></listitem>
<listitem><para>
Parameter ke enam adalah opsional <link linkend="variable.compile.id">
<parameter>$compile_id</parameter></link>.
</para></listitem>
<listitem><para>
Parameter ke tujuh dan parameter terakhir <parameter>$exp_time</parameter>
ditambahkan dalam Smarty-2.6.0.
</para></listitem>
</itemizedlist>
<example>
<title>Contoh menggunakan MySQL sebagai sumber cache</title>
<programlisting role="php">
<![CDATA[
<?php
/**************************************************
contoh pemakaian:
include('Smarty.class.php');
include('mysql_cache_handler.php');
$smarty = new Smarty;
$smarty->cache_handler_func = 'mysql_cache_handler';
$smarty->display('index.tpl');
database mysql diharapkan dalam format ini:
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 dan sandi di sini
$db_host = 'localhost';
$db_user = 'myuser';
$db_pass = 'mypass';
$db_name = 'SMARTY_CACHE';
$use_gzip = false;
// buat id cache unik
$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':
// baca cache dari 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']);
} else {
$cache_content = $row['CacheContents'];
}
$return = $results;
break;
case 'write':
// simpan cache ke 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:
// salah, aksi tidak dikenal
$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
-->