This commit is contained in:
didou
2004-04-13 12:14:17 +00:00
parent e066cf2984
commit af45ae6f07
4 changed files with 184 additions and 178 deletions

View File

@@ -2,22 +2,22 @@
<!-- $Revision$ --> <!-- $Revision$ -->
<sect1 id="caching.cacheable"> <sect1 id="caching.cacheable">
<title>Controlling Cacheability of Plugins' Output</title> <title>Controlling Cacheability of Plugins' Output</title>
<para> <para>
Since Smarty-2.6.0 plugins the cacheability of plugins can be declared Since Smarty-2.6.0 plugins the cacheability of plugins can be declared
when registering them. The third parameter to register_block, when registering them. The third parameter to register_block,
register_compiler_function and register_function is called register_compiler_function and register_function is called
<parameter>$cacheable</parameter> and defaults to true which is also <parameter>$cacheable</parameter> and defaults to true which is also
the behaviour of plugins in Smarty versions before 2.6.0 the behaviour of plugins in Smarty versions before 2.6.0
</para> </para>
<para> <para>
When registering a plugin with $cacheable=false the plugin is When registering a plugin with $cacheable=false the plugin is
called everytime the page is displayed, even if the page comes called everytime the page is displayed, even if the page comes
from the cache. The plugin function behaves a little like an from the cache. The plugin function behaves a little like an
<link linkend="plugins.inserts">insert</link> function. <link linkend="plugins.inserts">insert</link> function.
</para> </para>
<para> <para>
In contrast to <link linkend="language.function.insert">{insert}</link> In contrast to <link linkend="language.function.insert">{insert}</link>
the attributes to the plugins are not cached by default. They can be the attributes to the plugins are not cached by default. They can be
declared to be cached with the fourth parameter declared to be cached with the fourth parameter
@@ -25,9 +25,9 @@
is an array of attribute-names that should be cached, so the is an array of attribute-names that should be cached, so the
plugin-function get value as it was the time the page was written plugin-function get value as it was the time the page was written
to cache everytime it is fetched from the cache. to cache everytime it is fetched from the cache.
</para> </para>
<example> <example>
<title>Preventing a plugin's output from being cached</title> <title>Preventing a plugin's output from being cached</title>
<programlisting role="php"> <programlisting role="php">
<![CDATA[ <![CDATA[
@@ -62,16 +62,16 @@ index.tpl:
Time Remaining: {remain endtime=$obj->endtime} Time Remaining: {remain endtime=$obj->endtime}
]]> ]]>
</programlisting> </programlisting>
<para> <para>
The number of seconds till the endtime of $obj is reached changes on The number of seconds till the endtime of $obj is reached changes on
each display of the page, even if the page is cached. Since the each display of the page, even if the page is cached. Since the
endtime attribute is cached the object only has to be pulled from the endtime attribute is cached the object only has to be pulled from the
database when page is written to the cache but not on subsequent requests database when page is written to the cache but not on subsequent requests
of the page. of the page.
</para> </para>
</example> </example>
<example> <example>
<title>Preventing a whole passage of a template from being cached</title> <title>Preventing a whole passage of a template from being cached</title>
<programlisting role="php"> <programlisting role="php">
<![CDATA[ <![CDATA[
@@ -104,14 +104,14 @@ Now is: {"0"|date_format:"%D %H:%M:%S"}
{/dynamic} {/dynamic}
]]> ]]>
</programlisting> </programlisting>
</example> </example>
<para> <para>
When reloading the page you will notice that both dates differ. One When reloading the page you will notice that both dates differ. One
is "dynamic" one is "static". You can do everything between is "dynamic" one is "static". You can do everything between
{dynamic}...{/dynamic} and be sure it will not be cached like the rest {dynamic}...{/dynamic} and be sure it will not be cached like the rest
of the page. of the page.
</para> </para>
</sect1> </sect1>

View File

@@ -2,14 +2,14 @@
<!-- $Revision$ --> <!-- $Revision$ -->
<sect1 id="caching.groups"> <sect1 id="caching.groups">
<title>Cache Groups</title> <title>Cache Groups</title>
<para> <para>
You can do more elaborate grouping by setting up cache_id groups. This is You can do more elaborate grouping by setting up cache_id groups. This is
accomplished by separating each sub-group with a vertical bar "|" in the accomplished by separating each sub-group with a vertical bar "|" in the
cache_id value. You can have as many sub-groups as you like. cache_id value. You can have as many sub-groups as you like.
</para> </para>
<example> <example>
<title>cache_id groups</title> <title>cache_id groups</title>
<programlisting role="php"> <programlisting role="php">
<![CDATA[ <![CDATA[
<?php <?php
require('Smarty.class.php'); require('Smarty.class.php');
@@ -27,20 +27,22 @@ $smarty->clear_cache(null,"sports");
$smarty->display('index.tpl',"sports|basketball"); $smarty->display('index.tpl',"sports|basketball");
?> ?>
]]> ]]>
</programlisting> </programlisting>
</example> </example>
<note> <note>
<title>Technical Note</title> <title>Technical Note</title>
<para> <para>
The cache grouping does NOT use the path to the template as any part of the The cache grouping does NOT use the path to the template as any part of the
cache_id. For example, if you have display('themes/blue/index.tpl'), you cache_id. For example, if you have display('themes/blue/index.tpl'), you
cannot clear the cache for everything under the "themes/blue" directory. If cannot clear the cache for everything under the "themes/blue" directory. If
you want to do that, you must group them in the cache_id, such as you want to do that, you must group them in the cache_id, such as
display('themes/blue/index.tpl','themes|blue'); Then you can clear the display('themes/blue/index.tpl','themes|blue'); Then you can clear the
caches for the blue theme with clear_cache(null,'themes|blue'); caches for the blue theme with clear_cache(null,'themes|blue');
</para> </para>
</note> </note>
</sect1>
</sect1>
<!-- Keep this comment at the end of the file <!-- Keep this comment at the end of the file
Local variables: Local variables:
mode: sgml mode: sgml

View File

@@ -2,13 +2,13 @@
<!-- $Revision$ --> <!-- $Revision$ -->
<sect1 id="caching.multiple.caches"> <sect1 id="caching.multiple.caches">
<title>Multiple Caches Per Page</title> <title>Multiple Caches Per Page</title>
<para> <para>
You can have multiple cache files for a single call to display() or You can have multiple cache files for a single call to display() or
fetch(). Let's say that a call to display('index.tpl') may have several fetch(). Let's say that a call to display('index.tpl') may have several
different output contents depending on some condition, and you want different output contents depending on some condition, and you want
separate caches for each one. You can do this by passing a cache_id as the separate caches for each one. You can do this by passing a cache_id as the
second parameter to the function call. second parameter to the function call.
</para> </para>
<example> <example>
<title>passing a cache_id to display()</title> <title>passing a cache_id to display()</title>
<programlisting role="php"> <programlisting role="php">
@@ -24,35 +24,35 @@ $my_cache_id = $_GET['article_id'];
$smarty->display('index.tpl',$my_cache_id); $smarty->display('index.tpl',$my_cache_id);
?> ?>
]]> ]]>
</programlisting> </programlisting>
</example> </example>
<para> <para>
Above, we are passing the variable $my_cache_id to display() as the Above, we are passing the variable $my_cache_id to display() as the
cache_id. For each unique value of $my_cache_id, a separate cache will be cache_id. For each unique value of $my_cache_id, a separate cache will be
generated for index.tpl. In this example, "article_id" was passed in the generated for index.tpl. In this example, "article_id" was passed in the
URL and is used as the cache_id. URL and is used as the cache_id.
</para> </para>
<note> <note>
<title>Technical Note</title> <title>Technical Note</title>
<para> <para>
Be very cautious when passing values from a client (web browser) into Be very cautious when passing values from a client (web browser) into
Smarty (or any PHP application.) Although the above example of using the Smarty (or any PHP application.) Although the above example of using the
article_id from the URL looks handy, it could have bad consequences. The article_id from the URL looks handy, it could have bad consequences. The
cache_id is used to create a directory on the file system, so if the user cache_id is used to create a directory on the file system, so if the user
decided to pass an extremely large value for article_id, or write a script decided to pass an extremely large value for article_id, or write a script
that sends random article_ids at a rapid pace, this could possibly cause that sends random article_ids at a rapid pace, this could possibly cause
problems at the server level. Be sure to sanitize any data passed in before problems at the server level. Be sure to sanitize any data passed in before
using it. In this instance, maybe you know the article_id has a length of using it. In this instance, maybe you know the article_id has a length of
10 characters and is made up of alpha-numerics only, and must be a valid 10 characters and is made up of alpha-numerics only, and must be a valid
article_id in the database. Check for this! article_id in the database. Check for this!
</para> </para>
</note> </note>
<para> <para>
Be sure to pass the same cache_id as the Be sure to pass the same cache_id as the
second parameter to <link linkend="api.is.cached">is_cached()</link> and second parameter to <link linkend="api.is.cached">is_cached()</link> and
<link linkend="api.clear.cache">clear_cache()</link>. <link linkend="api.clear.cache">clear_cache()</link>.
</para> </para>
<example> <example>
<title>passing a cache_id to is_cached()</title> <title>passing a cache_id to is_cached()</title>
<programlisting role="php"> <programlisting role="php">
<![CDATA[ <![CDATA[
@@ -65,21 +65,21 @@ $smarty->caching = true;
$my_cache_id = $_GET['article_id']; $my_cache_id = $_GET['article_id'];
if(!$smarty->is_cached('index.tpl',$my_cache_id)) { if(!$smarty->is_cached('index.tpl',$my_cache_id)) {
// No cache available, do variable assignments here. // No cache available, do variable assignments here.
$contents = get_database_contents(); $contents = get_database_contents();
$smarty->assign($contents); $smarty->assign($contents);
} }
$smarty->display('index.tpl',$my_cache_id); $smarty->display('index.tpl',$my_cache_id);
?> ?>
]]> ]]>
</programlisting> </programlisting>
</example> </example>
<para> <para>
You can clear all caches for a particular cache_id by passing null as the You can clear all caches for a particular cache_id by passing null as the
first parameter to clear_cache(). first parameter to clear_cache().
</para> </para>
<example> <example>
<title>clearing all caches for a particular cache_id</title> <title>clearing all caches for a particular cache_id</title>
<programlisting role="php"> <programlisting role="php">
<![CDATA[ <![CDATA[
@@ -95,13 +95,15 @@ $smarty->clear_cache(null,"sports");
$smarty->display('index.tpl',"sports"); $smarty->display('index.tpl',"sports");
?> ?>
]]> ]]>
</programlisting> </programlisting>
</example> </example>
<para> <para>
In this manner, you can "group" your caches together by giving them the In this manner, you can "group" your caches together by giving them the
same cache_id. same cache_id.
</para> </para>
</sect1>
</sect1>
<!-- Keep this comment at the end of the file <!-- Keep this comment at the end of the file
Local variables: Local variables:
mode: sgml mode: sgml

View File

@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="iso-8859-1"?> <?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ --> <!-- $Revision$ -->
<sect1 id="caching.setting.up"> <sect1 id="caching.setting.up">
<title>Setting Up Caching</title> <title>Setting Up Caching</title>
<para> <para>
The first thing to do is enable caching. This is done by setting <link The first thing to do is enable caching. This is done by setting <link
linkend="variable.caching">$caching</link> = true (or 1.) linkend="variable.caching">$caching</link> = true (or 1.)
</para> </para>
<example> <example>
<title>enabling caching</title> <title>enabling caching</title>
<programlisting role="php"> <programlisting role="php">
<![CDATA[ <![CDATA[
<?php <?php
require('Smarty.class.php'); require('Smarty.class.php');
@@ -19,34 +19,34 @@ $smarty->caching = true;
$smarty->display('index.tpl'); $smarty->display('index.tpl');
?> ?>
]]> ]]>
</programlisting> </programlisting>
</example> </example>
<para> <para>
With caching enabled, the function call to display('index.tpl') will render With caching enabled, the function call to display('index.tpl') will render
the template as usual, but also saves a copy of its output to a file (a the template as usual, but also saves a copy of its output to a file (a
cached copy) in the <link linkend="variable.cache.dir">$cache_dir</link>. cached copy) in the <link linkend="variable.cache.dir">$cache_dir</link>.
Upon the next call to display('index.tpl'), the cached copy will be used Upon the next call to display('index.tpl'), the cached copy will be used
instead of rendering the template again. instead of rendering the template again.
</para> </para>
<note> <note>
<title>Technical Note</title> <title>Technical Note</title>
<para> <para>
The files in the $cache_dir are named similar to the template name. The files in the $cache_dir are named similar to the template name.
Although they end in the ".php" extention, they are not really executable Although they end in the ".php" extention, they are not really executable
php scripts. Do not edit these files! php scripts. Do not edit these files!
</para> </para>
</note> </note>
<para> <para>
Each cached page has a limited lifetime determined by <link Each cached page has a limited lifetime determined by <link
linkend="variable.cache.lifetime">$cache_lifetime</link>. The default value linkend="variable.cache.lifetime">$cache_lifetime</link>. The default value
is 3600 seconds, or 1 hour. After that time expires, the cache is is 3600 seconds, or 1 hour. After that time expires, the cache is
regenerated. It is possible to give individual caches their own expiration regenerated. It is possible to give individual caches their own expiration
time by setting $caching = 2. See the documentation on <link time by setting $caching = 2. See the documentation on <link
linkend="variable.cache.lifetime">$cache_lifetime</link> for details. linkend="variable.cache.lifetime">$cache_lifetime</link> for details.
</para> </para>
<example> <example>
<title>setting cache_lifetime per cache</title> <title>setting cache_lifetime per cache</title>
<programlisting role="php"> <programlisting role="php">
<![CDATA[ <![CDATA[
<?php <?php
require('Smarty.class.php'); require('Smarty.class.php');
@@ -70,18 +70,18 @@ $smarty->cache_lifetime = 30; // 30 seconds
$smarty->display('home.tpl'); $smarty->display('home.tpl');
?> ?>
]]> ]]>
</programlisting> </programlisting>
</example> </example>
<para> <para>
If <link linkend="variable.compile.check">$compile_check</link> is enabled, If <link linkend="variable.compile.check">$compile_check</link> is enabled,
every template file and config file that is involved with the cache file is 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 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 cache was generated, the cache is immediately regenerated. This is a slight
overhead so for optimum performance, leave $compile_check set to false. overhead so for optimum performance, leave $compile_check set to false.
</para> </para>
<example> <example>
<title>enabling $compile_check</title> <title>enabling $compile_check</title>
<programlisting role="php"> <programlisting role="php">
<![CDATA[ <![CDATA[
<?php <?php
require('Smarty.class.php'); require('Smarty.class.php');
@@ -93,24 +93,24 @@ $smarty->compile_check = true;
$smarty->display('index.tpl'); $smarty->display('index.tpl');
?> ?>
]]> ]]>
</programlisting> </programlisting>
</example> </example>
<para> <para>
If <link linkend="variable.force.compile">$force_compile</link> is enabled, If <link linkend="variable.force.compile">$force_compile</link> is enabled,
the cache files will always be regenerated. This effectively turns off the cache files will always be regenerated. This effectively turns off
caching. $force_compile is usually for debugging purposes only, a more caching. $force_compile is usually for debugging purposes only, a more
efficient way of disabling caching is to set <link efficient way of disabling caching is to set <link
linkend="variable.caching">$caching</link> = false (or 0.) linkend="variable.caching">$caching</link> = false (or 0.)
</para> </para>
<para> <para>
The <link linkend="api.is.cached">is_cached()</link> function The <link linkend="api.is.cached">is_cached()</link> function
can be used to test if a template has a valid cache or not. If you have a 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 cached template that requires something like a database fetch, you can use
this to skip that process. this to skip that process.
</para> </para>
<example> <example>
<title>using is_cached()</title> <title>using is_cached()</title>
<programlisting role="php"> <programlisting role="php">
<![CDATA[ <![CDATA[
<?php <?php
require('Smarty.class.php'); require('Smarty.class.php');
@@ -119,34 +119,34 @@ $smarty = new Smarty;
$smarty->caching = true; $smarty->caching = true;
if(!$smarty->is_cached('index.tpl')) { if(!$smarty->is_cached('index.tpl')) {
// No cache available, do variable assignments here. // No cache available, do variable assignments here.
$contents = get_database_contents(); $contents = get_database_contents();
$smarty->assign($contents); $smarty->assign($contents);
} }
$smarty->display('index.tpl'); $smarty->display('index.tpl');
?> ?>
]]> ]]>
</programlisting> </programlisting>
</example> </example>
<para> <para>
You can keep parts of a page dynamic with the <link You can keep parts of a page dynamic with the <link
linkend="language.function.insert">insert</link> template function. Let's linkend="language.function.insert">insert</link> template function. Let's
say the whole page can be cached except for a banner that is displayed down say the whole page can be cached except for a banner that is displayed down
the right side of the page. By using an insert function for the banner, you the right side of the page. By using an insert function for the banner, you
can keep this element dynamic within the cached content. See the can keep this element dynamic within the cached content. See the
documentation on <link linkend="language.function.insert">insert</link> for documentation on <link linkend="language.function.insert">insert</link> for
details and examples. details and examples.
</para> </para>
<para> <para>
You can clear all the cache files with the <link You can clear all the cache files with the <link
linkend="api.clear.all.cache">clear_all_cache()</link> function, or linkend="api.clear.all.cache">clear_all_cache()</link> function, or
individual cache files (or groups) with the <link individual cache files (or groups) with the <link
linkend="api.clear.cache">clear_cache()</link> function. linkend="api.clear.cache">clear_cache()</link> function.
</para> </para>
<example> <example>
<title>clearing the cache</title> <title>clearing the cache</title>
<programlisting role="php"> <programlisting role="php">
<![CDATA[ <![CDATA[
<?php <?php
require('Smarty.class.php'); require('Smarty.class.php');
@@ -163,9 +163,11 @@ $smarty->clear_cache('index.tpl');
$smarty->display('index.tpl'); $smarty->display('index.tpl');
?> ?>
]]> ]]>
</programlisting> </programlisting>
</example> </example>
</sect1>
</sect1>
<!-- Keep this comment at the end of the file <!-- Keep this comment at the end of the file
Local variables: Local variables:
mode: sgml mode: sgml