mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-08 16:21:39 +01:00
185 lines
5.8 KiB
XML
185 lines
5.8 KiB
XML
|
|
<?xml version="1.0" encoding="utf-8"?>
|
||
|
|
<!-- $Revision$ -->
|
||
|
|
<!-- EN-Revision: 1.5 Maintainer: takagi Status: ready -->
|
||
|
|
<!-- CREDITS: mat-sh,daichi,joe -->
|
||
|
|
<sect1 id="section.template.cache.handler.func">
|
||
|
|
<title>キャッシュハンドラ関数</title>
|
||
|
|
<para>
|
||
|
|
デフォルトのファイルベースのキャッシュメカニズムの代替として、
|
||
|
|
キャッシュファイルの読み書きや破棄を直接行うキャッシュハンドラ関数を指定できます。
|
||
|
|
</para>
|
||
|
|
<para>
|
||
|
|
まず、アプリケーション内にSmartyがキャッシュハンドラとして使用するための関数を定義します。
|
||
|
|
そしてその関数名を <link linkend="variable.cache.handler.func">
|
||
|
|
<parameter>$cache_handler_func</parameter></link>
|
||
|
|
クラス変数に指定します。Smarty は、これを使用してキャッシュされたデータを処理します。
|
||
|
|
</para>
|
||
|
|
|
||
|
|
<itemizedlist>
|
||
|
|
<listitem><para>
|
||
|
|
第1パラメータはキャッシュの動作を表す文字列で、これは
|
||
|
|
<literal>read</literal>、<literal>write</literal> および
|
||
|
|
<literal>clear</literal> のいずれかとなります。
|
||
|
|
</para></listitem>
|
||
|
|
|
||
|
|
<listitem><para>
|
||
|
|
第2パラメータは Smarty オブジェクトです。
|
||
|
|
</para></listitem>
|
||
|
|
|
||
|
|
<listitem><para>
|
||
|
|
第3パラメータはキャッシュの内容です。
|
||
|
|
<literal>write</literal> の場合はキャッシュされたコンテンツが渡され、
|
||
|
|
<literal>read</literal> の場合は参照を受け取ってそこにキャッシュされたコンテンツを書き込み、
|
||
|
|
<literal>clear</literal> の場合はこのパラメータの値を使用しないのでダミーの変数が渡されます。
|
||
|
|
</para></listitem>
|
||
|
|
|
||
|
|
<listitem><para>
|
||
|
|
第4パラメータはテンプレートファイル名です('read'又は'write'の場合に必要)。
|
||
|
|
</para></listitem>
|
||
|
|
|
||
|
|
<listitem><para>
|
||
|
|
任意の第5パラメータは <parameter>$cache_id</parameter> です。
|
||
|
|
</para></listitem>
|
||
|
|
|
||
|
|
<listitem><para>
|
||
|
|
任意の第6パラメータは <link linkend="variable.compile.id">
|
||
|
|
<parameter>$compile_id</parameter></link> です。
|
||
|
|
</para></listitem>
|
||
|
|
|
||
|
|
<listitem><para>
|
||
|
|
最後の第7パラメータ <parameter>$exp_time</parameter>
|
||
|
|
は Smarty-2.6.0 で追加されました。
|
||
|
|
</para></listitem>
|
||
|
|
|
||
|
|
</itemizedlist>
|
||
|
|
|
||
|
|
<example>
|
||
|
|
<title>キャッシュソースとしてMySQLを使用する例</title>
|
||
|
|
<programlisting role="php">
|
||
|
|
<![CDATA[
|
||
|
|
<?php
|
||
|
|
/**************************************************
|
||
|
|
example usage:
|
||
|
|
|
||
|
|
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のホスト名・ユーザ名・パスワードを指定します
|
||
|
|
$db_host = 'localhost';
|
||
|
|
$db_user = 'myuser';
|
||
|
|
$db_pass = 'mypass';
|
||
|
|
$db_name = 'SMARTY_CACHE';
|
||
|
|
$use_gzip = false;
|
||
|
|
|
||
|
|
// ユニークなキャッシュ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")) {
|
||
|
|
// 記憶効率のために内容を圧縮します
|
||
|
|
$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;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
?>
|
||
|
|
]]>
|
||
|
|
</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
|
||
|
|
-->
|