<HTML ><HEAD ><TITLE >Cache Handler Function</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Smarty Manual" HREF="index.html"><LINK REL="UP" TITLE="Advanced Features" HREF="advanced.features.html"><LINK REL="PREVIOUS" TITLE="Output Filters" HREF="advanced.features.outputfilters.html"><LINK REL="NEXT" TITLE="Resources" HREF="template.resources.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Smarty Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="advanced.features.outputfilters.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 15. Advanced Features</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="template.resources.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="section.template.cache.handler.func" ></A >Cache Handler Function</H1 ><P > As an alternative to using the default file-based caching mechanism, you can specify a custom cache handling function that will be used to <TT CLASS="literal" >read</TT >, <TT CLASS="literal" >write</TT > and <TT CLASS="literal" >clear</TT > cached files. </P ><P > Create a function in your application that Smarty will use as a cache handler. Set the name of it in the <A HREF="variable.cache.handler.func.html" > <TT CLASS="parameter" ><I >$cache_handler_func</I ></TT ></A > class variable. Smarty will now use this to handle cached data. </P ><P ></P ><UL ><LI ><P > The first argument is the action, which will be one of <TT CLASS="literal" >read</TT >, <TT CLASS="literal" >write</TT > and <TT CLASS="literal" >clear</TT >. </P ></LI ><LI ><P > The second parameter is the Smarty object. </P ></LI ><LI ><P >The third parameter is the cached content. Upon a <TT CLASS="literal" >write</TT >, Smarty passes the cached content in these parameters. Upon a <TT CLASS="literal" >read</TT >, Smarty expects your function to accept this parameter by reference and populate it with the cached data. Upon a <TT CLASS="literal" >clear</TT >, pass a dummy variable here since it is not used. </P ></LI ><LI ><P > The fourth parameter is the <TT CLASS="parameter" ><I >name</I ></TT > of the template file, needed for read/write. </P ></LI ><LI ><P > The fifth parameter is the optional <TT CLASS="parameter" ><I >$cache_id</I ></TT >. </P ></LI ><LI ><P > The sixth is the optional <A HREF="variable.compile.id.html" > <TT CLASS="parameter" ><I >$compile_id</I ></TT ></A >. </P ></LI ><LI ><P > The seventh and last parameter <TT CLASS="parameter" ><I >$exp_time</I ></TT > was added in Smarty-2.6.0. </P ></LI ></UL ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7023" ></A ><P ><B >Example 15-5. Example using MySQL as a cache source</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**************************************************<br />example usage:<br /><br />include('Smarty.class.php');<br />include('mysql_cache_handler.php');<br /><br />$smarty = new Smarty;<br />$smarty->cache_handler_func = 'mysql_cache_handler';<br /><br />$smarty->display('index.tpl');<br /><br /><br />mysql database is expected in this format:<br /><br />create database SMARTY_CACHE;<br /><br />create table CACHE_PAGES(<br />CacheID char(32) PRIMARY KEY,<br />CacheContents MEDIUMTEXT NOT NULL<br />);<br /><br />**************************************************/<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">mysql_cache_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">$action</span><span style="color: #007700">, &</span><span style="color: #0000BB">$smarty_obj</span><span style="color: #007700">, &</span><span style="color: #0000BB">$cache_content</span><span style="color: #007700">, </span><span style="color: #0000BB">$tpl_file</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">$cache_id</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">$compile_id</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">$exp_time</span><span style="color: #007700">=</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />{<br /> </span><span style="color: #FF8000">// set db host, user and pass here<br /> </span><span style="color: #0000BB">$db_host </span><span style="color: #007700">= </span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$db_user </span><span style="color: #007700">= </span><span style="color: #DD0000">'myuser'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$db_pass </span><span style="color: #007700">= </span><span style="color: #DD0000">'mypass'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$db_name </span><span style="color: #007700">= </span><span style="color: #DD0000">'SMARTY_CACHE'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$use_gzip </span><span style="color: #007700">= </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /><br /> </span><span style="color: #FF8000">// create unique cache id<br /> </span><span style="color: #0000BB">$CacheID </span><span style="color: #007700">= </span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #0000BB">$tpl_file</span><span style="color: #007700">.</span><span style="color: #0000BB">$cache_id</span><span style="color: #007700">.</span><span style="color: #0000BB">$compile_id</span><span style="color: #007700">);<br /><br /> if(! </span><span style="color: #0000BB">$link </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_pconnect</span><span style="color: #007700">(</span><span style="color: #0000BB">$db_host</span><span style="color: #007700">, </span><span style="color: #0000BB">$db_user</span><span style="color: #007700">, </span><span style="color: #0000BB">$db_pass</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$smarty_obj</span><span style="color: #007700">-></span><span style="color: #0000BB">_trigger_error_msg</span><span style="color: #007700">(</span><span style="color: #DD0000">'cache_handler: could not connect to database'</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #0000BB">$db_name</span><span style="color: #007700">);<br /><br /> switch (</span><span style="color: #0000BB">$action</span><span style="color: #007700">) {<br /> case </span><span style="color: #DD0000">'read'</span><span style="color: #007700">:<br /> </span><span style="color: #FF8000">// read cache from database<br /> </span><span style="color: #0000BB">$results </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"select CacheContents from CACHE_PAGES where CacheID='$CacheID'"</span><span style="color: #007700">);<br /> if(!</span><span style="color: #0000BB">$results</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$smarty_obj</span><span style="color: #007700">-></span><span style="color: #0000BB">_trigger_error_msg</span><span style="color: #007700">(</span><span style="color: #DD0000">'cache_handler: query failed.'</span><span style="color: #007700">);<br /> }<br /> </span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$results</span><span style="color: #007700">,</span><span style="color: #0000BB">MYSQL_ASSOC</span><span style="color: #007700">);<br /><br /> if(</span><span style="color: #0000BB">$use_gzip </span><span style="color: #007700">&& </span><span style="color: #0000BB">function_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'gzuncompress'</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$cache_content </span><span style="color: #007700">= </span><span style="color: #0000BB">gzuncompress</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'CacheContents'</span><span style="color: #007700">]);<br /> } else {<br /> </span><span style="color: #0000BB">$cache_content </span><span style="color: #007700">= </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'CacheContents'</span><span style="color: #007700">];<br /> }<br /> </span><span style="color: #0000BB">$return </span><span style="color: #007700">= </span><span style="color: #0000BB">$results</span><span style="color: #007700">;<br /> break;<br /> case </span><span style="color: #DD0000">'write'</span><span style="color: #007700">:<br /> </span><span style="color: #FF8000">// save cache to database<br /><br /> </span><span style="color: #007700">if(</span><span style="color: #0000BB">$use_gzip </span><span style="color: #007700">&& </span><span style="color: #0000BB">function_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">"gzcompress"</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// compress the contents for storage efficiency<br /> </span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">gzcompress</span><span style="color: #007700">(</span><span style="color: #0000BB">$cache_content</span><span style="color: #007700">);<br /> } else {<br /> </span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">$cache_content</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">$results </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"replace into CACHE_PAGES values(<br /> '$CacheID',<br /> '"</span><span style="color: #007700">.</span><span style="color: #0000BB">addslashes</span><span style="color: #007700">(</span><span style="color: #0000BB">$contents</span><span style="color: #007700">).</span><span style="color: #DD0000">"')<br /> "</span><span style="color: #007700">);<br /> if(!</span><span style="color: #0000BB">$results</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$smarty_obj</span><span style="color: #007700">-></span><span style="color: #0000BB">_trigger_error_msg</span><span style="color: #007700">(</span><span style="color: #DD0000">'cache_handler: query failed.'</span><span style="color: #007700">);<br /> }<br /> </span><span style="color: #0000BB">$return </span><span style="color: #007700">= </span><span style="color: #0000BB">$results</span><span style="color: #007700">;<br /> break;<br /> case </span><span style="color: #DD0000">'clear'</span><span style="color: #007700">:<br /> </span><span style="color: #FF8000">// clear cache info<br /> </span><span style="color: #007700">if(empty(</span><span style="color: #0000BB">$cache_id</span><span style="color: #007700">) && empty(</span><span style="color: #0000BB">$compile_id</span><span style="color: #007700">) && empty(</span><span style="color: #0000BB">$tpl_file</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// clear them all<br /> </span><span style="color: #0000BB">$results </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">'delete from CACHE_PAGES'</span><span style="color: #007700">);<br /> } else {<br /> </span><span style="color: #0000BB">$results </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"delete from CACHE_PAGES where CacheID='$CacheID'"</span><span style="color: #007700">);<br /> }<br /> if(!</span><span style="color: #0000BB">$results</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$smarty_obj</span><span style="color: #007700">-></span><span style="color: #0000BB">_trigger_error_msg</span><span style="color: #007700">(</span><span style="color: #DD0000">'cache_handler: query failed.'</span><span style="color: #007700">);<br /> }<br /> </span><span style="color: #0000BB">$return </span><span style="color: #007700">= </span><span style="color: #0000BB">$results</span><span style="color: #007700">;<br /> break;<br /> default:<br /> </span><span style="color: #FF8000">// error, unknown action<br /> </span><span style="color: #0000BB">$smarty_obj</span><span style="color: #007700">-></span><span style="color: #0000BB">_trigger_error_msg</span><span style="color: #007700">(</span><span style="color: #DD0000">"cache_handler: unknown action \"$action\""</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$return </span><span style="color: #007700">= </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /> break;<br /> }<br /> </span><span style="color: #0000BB">mysql_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">$return</span><span style="color: #007700">;<br /><br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="advanced.features.outputfilters.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="template.resources.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Output Filters</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="advanced.features.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Resources</TD ></TR ></TABLE ></DIV ></BODY ></HTML >