| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | <?xml version="1.0" encoding="iso-8859-1"?> | 
					
						
							|  |  |  | <!-- $Revision$ --> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |   <sect1 id="caching.setting.up"> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |    <title>Setting Up Caching</title> | 
					
						
							|  |  |  |    <para> | 
					
						
							| 
									
										
										
										
											2006-05-04 19:22:07 +00:00
										 |  |  |     The first thing to do is enable caching by setting <link | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     linkend="variable.caching"> | 
					
						
							|  |  |  |     <parameter>$caching</parameter></link><literal> = 1 (or 2)</literal>. | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  |    </para> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |    <example> | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     <title>Enabling caching</title> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     <programlisting role="php"> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | <![CDATA[ | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | require('Smarty.class.php'); | 
					
						
							|  |  |  | $smarty = new Smarty; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-04 19:22:07 +00:00
										 |  |  | $smarty->caching = 1; | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | $smarty->display('index.tpl'); | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | ]]> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     </programlisting> | 
					
						
							|  |  |  |    </example> | 
					
						
							|  |  |  |    <para> | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     With caching enabled, the function call to | 
					
						
							|  |  |  |     <literal>display('index.tpl')</literal> will render | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     the template as usual, but also saves a copy of its output to a file (a | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     cached copy) in the | 
					
						
							|  |  |  |     <link linkend="variable.cache.dir"><parameter>$cache_dir</parameter></link>. | 
					
						
							|  |  |  |     On the next call to <literal>display('index.tpl')</literal>, the cached copy | 
					
						
							|  |  |  |     will be used instead of rendering the template again. | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |    </para> | 
					
						
							|  |  |  |    <note> | 
					
						
							|  |  |  |     <title>Technical Note</title> | 
					
						
							|  |  |  |     <para> | 
					
						
							| 
									
										
										
										
											2005-05-23 15:43:01 +00:00
										 |  |  |      The files in the | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |      <link linkend="variable.cache.dir"><parameter>$cache_dir</parameter></link> | 
					
						
							| 
									
										
										
										
											2005-05-23 15:43:01 +00:00
										 |  |  |      are named similar to the template name. | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |      Although they end in the <filename>.php</filename> extention, they are not | 
					
						
							|  |  |  |      intended to be directly executable. Do not edit these files! | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     </para> | 
					
						
							|  |  |  |    </note> | 
					
						
							|  |  |  |    <para> | 
					
						
							|  |  |  |     Each cached page has a limited lifetime determined by <link | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     linkend="variable.cache.lifetime"><parameter>$cache_lifetime</parameter></link>. | 
					
						
							|  |  |  |     The default value is 3600 seconds ie an hour. After that time expires, the | 
					
						
							|  |  |  |     cache is regenerated. It is possible to give individual caches their own | 
					
						
							|  |  |  |     expiration time by setting | 
					
						
							|  |  |  |     <link linkend="variable.caching"><parameter>$caching</parameter></link><literal>=2</literal>. | 
					
						
							| 
									
										
										
										
											2005-09-13 16:14:31 +00:00
										 |  |  |     See <link | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     linkend="variable.cache.lifetime"><parameter>$cache_lifetime</parameter></link> | 
					
						
							|  |  |  |     for more details. | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |    </para> | 
					
						
							|  |  |  |    <example> | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     <title>Setting $cache_lifetime per cache</title> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     <programlisting role="php"> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | <![CDATA[ | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | require('Smarty.class.php'); | 
					
						
							|  |  |  | $smarty = new Smarty; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $smarty->caching = 2; // lifetime is per cache | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // set the cache_lifetime for index.tpl to 5 minutes | 
					
						
							|  |  |  | $smarty->cache_lifetime = 300; | 
					
						
							|  |  |  | $smarty->display('index.tpl'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // set the cache_lifetime for home.tpl to 1 hour | 
					
						
							|  |  |  | $smarty->cache_lifetime = 3600; | 
					
						
							|  |  |  | $smarty->display('home.tpl'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NOTE: the following $cache_lifetime setting will not work when $caching = 2. | 
					
						
							|  |  |  | // The cache lifetime for home.tpl has already been set | 
					
						
							|  |  |  | // to 1 hour, and will no longer respect the value of $cache_lifetime. | 
					
						
							|  |  |  | // The home.tpl cache will still expire after 1 hour. | 
					
						
							|  |  |  | $smarty->cache_lifetime = 30; // 30 seconds | 
					
						
							|  |  |  | $smarty->display('home.tpl'); | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | ]]> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     </programlisting> | 
					
						
							|  |  |  |    </example> | 
					
						
							|  |  |  |    <para> | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     If <link linkend="variable.compile.check"> | 
					
						
							|  |  |  |     <parameter>$compile_check</parameter></link> is enabled, | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     every template file and config file that is involved with the cache file is | 
					
						
							|  |  |  |     checked for modification. If any of the files have been modified since the | 
					
						
							|  |  |  |     cache was generated, the cache is immediately regenerated. This is a slight | 
					
						
							| 
									
										
										
										
											2006-05-04 19:22:07 +00:00
										 |  |  |     overhead so for optimum performance, set | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     <link linkend="variable.compile.check"><parameter>$compile_check</parameter> | 
					
						
							|  |  |  |     </link> to &false;. | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |    </para> | 
					
						
							|  |  |  |    <example> | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     <title>Enabling $compile_check</title> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     <programlisting role="php"> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | <![CDATA[ | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | require('Smarty.class.php'); | 
					
						
							|  |  |  | $smarty = new Smarty; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-04 19:22:07 +00:00
										 |  |  | $smarty->caching = 1; | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | $smarty->compile_check = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $smarty->display('index.tpl'); | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | ]]> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     </programlisting> | 
					
						
							|  |  |  |    </example> | 
					
						
							|  |  |  |    <para> | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     If <link linkend="variable.force.compile"> | 
					
						
							|  |  |  |     <parameter>$force_compile</parameter></link> is enabled, | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     the cache files will always be regenerated. This effectively turns off | 
					
						
							| 
									
										
										
										
											2005-05-23 15:43:01 +00:00
										 |  |  |     caching. | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     <link linkend="variable.force.compile"><parameter>$force_compile</parameter> | 
					
						
							|  |  |  |     </link> is usually for | 
					
						
							|  |  |  |     <link linkend="chapter.debugging.console">debugging</link> | 
					
						
							|  |  |  |     purposes only, a more  efficient way of disabling caching is to set <link | 
					
						
							|  |  |  |     linkend="variable.caching"><parameter>$caching</parameter> | 
					
						
							|  |  |  |     </link><literal> = 0</literal>. | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |    </para> | 
					
						
							|  |  |  |    <para> | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     The <link linkend="api.is.cached"><varname>is_cached()</varname></link> | 
					
						
							|  |  |  |     function | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     can be used to test if a template has a valid cache or not. If you have a | 
					
						
							|  |  |  |     cached template that requires something like a database fetch, you can use | 
					
						
							|  |  |  |     this to skip that process. | 
					
						
							|  |  |  |    </para> | 
					
						
							|  |  |  |    <example> | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     <title>Using is_cached()</title> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     <programlisting role="php"> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | <![CDATA[ | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | require('Smarty.class.php'); | 
					
						
							|  |  |  | $smarty = new Smarty; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-04 19:22:07 +00:00
										 |  |  | $smarty->caching = 1; | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if(!$smarty->is_cached('index.tpl')) { | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     // No cache available, do variable assignments here. | 
					
						
							|  |  |  |     $contents = get_database_contents(); | 
					
						
							|  |  |  |     $smarty->assign($contents); | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $smarty->display('index.tpl'); | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | ]]> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     </programlisting> | 
					
						
							|  |  |  |    </example> | 
					
						
							|  |  |  |    <para> | 
					
						
							|  |  |  |     You can keep parts of a page dynamic with the <link | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     linkend="language.function.insert"><varname>{insert}</varname></link> | 
					
						
							|  |  |  |     template function. Let's | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     say the whole page can be cached except for a banner that is displayed down | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     the side of the page. By using the | 
					
						
							|  |  |  |     <link linkend="language.function.insert"><varname>{insert}</varname></link> | 
					
						
							| 
									
										
										
										
											2005-09-13 16:14:31 +00:00
										 |  |  |     function for the banner, you | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     can keep this element dynamic within the cached content. See the | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     documentation on | 
					
						
							|  |  |  |     <link linkend="language.function.insert"><varname>{insert}</varname></link> | 
					
						
							|  |  |  |     for more details and examples. | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |    </para> | 
					
						
							|  |  |  |    <para> | 
					
						
							|  |  |  |     You can clear all the cache files with the <link | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     linkend="api.clear.all.cache"><varname>clear_all_cache()</varname></link> | 
					
						
							|  |  |  |     function, or | 
					
						
							|  |  |  |     individual cache files | 
					
						
							|  |  |  |     <link linkend="caching.groups">and groups</link> | 
					
						
							| 
									
										
										
										
											2005-09-13 16:14:31 +00:00
										 |  |  |     with the <link | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     linkend="api.clear.cache"><varname>clear_cache()</varname></link> function. | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |    </para> | 
					
						
							|  |  |  |    <example> | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  |     <title>Clearing the cache</title> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     <programlisting role="php"> | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | <![CDATA[ | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | require('Smarty.class.php'); | 
					
						
							|  |  |  | $smarty = new Smarty; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-04 19:22:07 +00:00
										 |  |  | $smarty->caching = 1; | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // clear only cache for index.tpl | 
					
						
							|  |  |  | $smarty->clear_cache('index.tpl'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-09-27 00:54:40 +00:00
										 |  |  | // clear out all cache files | 
					
						
							|  |  |  | $smarty->clear_all_cache(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | $smarty->display('index.tpl'); | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | ]]> | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  |     </programlisting> | 
					
						
							|  |  |  |    </example> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   </sect1> | 
					
						
							| 
									
										
										
										
											2005-05-27 16:25:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-04-13 11:47:32 +00:00
										 |  |  | <!-- 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 | 
					
						
							| 
									
										
										
										
											2004-04-13 12:14:17 +00:00
										 |  |  | --> |