| 148 |
lars |
1 |
Setting Up Caching {#caching.setting.up}
|
|
|
2 |
==================
|
|
|
3 |
|
|
|
4 |
The first thing to do is enable caching by setting
|
|
|
5 |
[`$caching`](#variable.caching) to one of
|
|
|
6 |
`Smarty::CACHING_LIFETIME_CURRENT` or `Smarty::CACHING_LIFETIME_SAVED`.
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
<?php
|
|
|
10 |
require('Smarty.class.php');
|
|
|
11 |
$smarty = new Smarty;
|
|
|
12 |
|
|
|
13 |
// uses the value of $smarty->cacheLifetime() to determine
|
|
|
14 |
// the number of seconds a cache is good for
|
|
|
15 |
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
|
|
|
16 |
|
|
|
17 |
$smarty->display('index.tpl');
|
|
|
18 |
?>
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
With caching enabled, the function call to `display('index.tpl')` will
|
|
|
23 |
render the template as usual, but also saves a copy of its output to a
|
|
|
24 |
file (a cached copy) in the [`$cache_dir`](#variable.cache.dir). On the
|
|
|
25 |
next call to `display('index.tpl')`, the cached copy will be used
|
|
|
26 |
instead of rendering the template again.
|
|
|
27 |
|
|
|
28 |
> **Note**
|
|
|
29 |
>
|
|
|
30 |
> The files in the [`$cache_dir`](#variable.cache.dir) are named similar
|
|
|
31 |
> to the template name. Although they end in the `.php` extension, they
|
|
|
32 |
> are not intended to be directly executable. Do not edit these files!
|
|
|
33 |
|
|
|
34 |
Each cached page has a limited lifetime determined by
|
|
|
35 |
[`$cache_lifetime`](#variable.cache.lifetime). The default value is 3600
|
|
|
36 |
seconds, or one hour. After that time expires, the cache is regenerated.
|
|
|
37 |
It is possible to give individual caches their own expiration time by
|
|
|
38 |
setting [`$caching`](#variable.caching) to
|
|
|
39 |
`Smarty::CACHING_LIFETIME_SAVED`. See
|
|
|
40 |
[`$cache_lifetime`](#variable.cache.lifetime) for more details.
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
<?php
|
|
|
44 |
require('Smarty.class.php');
|
|
|
45 |
$smarty = new Smarty;
|
|
|
46 |
|
|
|
47 |
// retain current cache lifetime for each specific display call
|
|
|
48 |
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);
|
|
|
49 |
|
|
|
50 |
// set the cache_lifetime for index.tpl to 5 minutes
|
|
|
51 |
$smarty->setCacheLifetime(300);
|
|
|
52 |
$smarty->display('index.tpl');
|
|
|
53 |
|
|
|
54 |
// set the cache_lifetime for home.tpl to 1 hour
|
|
|
55 |
$smarty->setCacheLifetime(3600);
|
|
|
56 |
$smarty->display('home.tpl');
|
|
|
57 |
|
|
|
58 |
// NOTE: the following $cache_lifetime setting will not work when $caching
|
|
|
59 |
// is set to Smarty::CACHING_LIFETIME_SAVED.
|
|
|
60 |
// The cache lifetime for home.tpl has already been set
|
|
|
61 |
// to 1 hour, and will no longer respect the value of $cache_lifetime.
|
|
|
62 |
// The home.tpl cache will still expire after 1 hour.
|
|
|
63 |
$smarty->setCacheLifetime(30); // 30 seconds
|
|
|
64 |
$smarty->display('home.tpl');
|
|
|
65 |
?>
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
If [`$compile_check`](#variable.compile.check) is enabled (default),
|
|
|
70 |
every template file and config file that is involved with the cache file
|
|
|
71 |
is checked for modification. If any of the files have been modified
|
|
|
72 |
since the cache was generated, the cache is immediately regenerated.
|
|
|
73 |
This is a computational overhead, so for optimum performance set
|
|
|
74 |
[`$compile_check`](#variable.compile.check) to FALSE.
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
<?php
|
|
|
78 |
require('Smarty.class.php');
|
|
|
79 |
$smarty = new Smarty;
|
|
|
80 |
|
|
|
81 |
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
|
|
|
82 |
$smarty->setCompileCheck(false);
|
|
|
83 |
|
|
|
84 |
$smarty->display('index.tpl');
|
|
|
85 |
?>
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
If [`$force_compile`](#variable.force.compile) is enabled, the cache
|
|
|
90 |
files will always be regenerated. This effectively disables caching,
|
|
|
91 |
however this also seriously degrades performance.
|
|
|
92 |
[`$force_compile`](#variable.force.compile) is meant to be used for
|
|
|
93 |
[debugging](#chapter.debugging.console) purposes. The appropriate way to
|
|
|
94 |
disable caching is to set [`$caching`](#variable.caching) to
|
|
|
95 |
Smarty::CACHING\_OFF.
|
|
|
96 |
|
|
|
97 |
The [`isCached()`](#api.is.cached) function can be used to test if a
|
|
|
98 |
template has a valid cache or not. If you have a cached template that
|
|
|
99 |
requires something like a database fetch, you can use this to skip that
|
|
|
100 |
process.
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
<?php
|
|
|
104 |
require('Smarty.class.php');
|
|
|
105 |
$smarty = new Smarty;
|
|
|
106 |
|
|
|
107 |
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
|
|
|
108 |
|
|
|
109 |
if(!$smarty->isCached('index.tpl')) {
|
|
|
110 |
// No cache available, do variable assignments here.
|
|
|
111 |
$contents = get_database_contents();
|
|
|
112 |
$smarty->assign($contents);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$smarty->display('index.tpl');
|
|
|
116 |
?>
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
You can keep parts of a page dynamic (disable caching) with the
|
|
|
121 |
[`{nocache}{/nocache}`](#language.function.nocache) block function, the
|
|
|
122 |
[`{insert}`](#language.function.insert) function, or by using the
|
|
|
123 |
`nocache` parameter for most template functions.
|
|
|
124 |
|
|
|
125 |
Let\'s say the whole page can be cached except for a banner that is
|
|
|
126 |
displayed down the side of the page. By using the
|
|
|
127 |
[`{insert}`](#language.function.insert) function for the banner, you can
|
|
|
128 |
keep this element dynamic within the cached content. See the
|
|
|
129 |
documentation on [`{insert}`](#language.function.insert) for more
|
|
|
130 |
details and examples.
|
|
|
131 |
|
|
|
132 |
You can clear all the cache files with the
|
|
|
133 |
[`clearAllCache()`](#api.clear.all.cache) function, or individual cache
|
|
|
134 |
files [and groups](#caching.groups) with the
|
|
|
135 |
[`clearCache()`](#api.clear.cache) function.
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
<?php
|
|
|
139 |
require('Smarty.class.php');
|
|
|
140 |
$smarty = new Smarty;
|
|
|
141 |
|
|
|
142 |
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
|
|
|
143 |
|
|
|
144 |
// clear only cache for index.tpl
|
|
|
145 |
$smarty->clearCache('index.tpl');
|
|
|
146 |
|
|
|
147 |
// clear out all cache files
|
|
|
148 |
$smarty->clearAllCache();
|
|
|
149 |
|
|
|
150 |
$smarty->display('index.tpl');
|
|
|
151 |
?>
|
|
|
152 |
|
|
|
153 |
|