aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/smarty/smarty/docs/programmers/caching/caching-setting-up.md
blob: bc9d2ad9efcc6d0c8abf1451040dbe8d207f21ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
Setting Up Caching {#caching.setting.up}
==================

The first thing to do is enable caching by setting
[`$caching`](#variable.caching) to one of
`Smarty::CACHING_LIFETIME_CURRENT` or `Smarty::CACHING_LIFETIME_SAVED`.


    <?php
    require('Smarty.class.php');
    $smarty = new Smarty;

    // uses the value of $smarty->cacheLifetime() to determine
    // the number of seconds a cache is good for
    $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

    $smarty->display('index.tpl');
    ?>

        

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 cached copy) in the [`$cache_dir`](#variable.cache.dir). On the
next call to `display('index.tpl')`, the cached copy will be used
instead of rendering the template again.

> **Note**
>
> The files in the [`$cache_dir`](#variable.cache.dir) are named similar
> to the template name. Although they end in the `.php` extension, they
> are not intended to be directly executable. Do not edit these files!

Each cached page has a limited lifetime determined by
[`$cache_lifetime`](#variable.cache.lifetime). The default value is 3600
seconds, or one hour. After that time expires, the cache is regenerated.
It is possible to give individual caches their own expiration time by
setting [`$caching`](#variable.caching) to
`Smarty::CACHING_LIFETIME_SAVED`. See
[`$cache_lifetime`](#variable.cache.lifetime) for more details.


    <?php
    require('Smarty.class.php');
    $smarty = new Smarty;

    // retain current cache lifetime for each specific display call
    $smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);

    // set the cache_lifetime for index.tpl to 5 minutes
    $smarty->setCacheLifetime(300);
    $smarty->display('index.tpl');

    // set the cache_lifetime for home.tpl to 1 hour
    $smarty->setCacheLifetime(3600);
    $smarty->display('home.tpl');

    // NOTE: the following $cache_lifetime setting will not work when $caching
    // is set to Smarty::CACHING_LIFETIME_SAVED.
    // 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->setCacheLifetime(30); // 30 seconds
    $smarty->display('home.tpl');
    ?>

        

If [`$compile_check`](#variable.compile.check) is enabled (default),
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 computational overhead, so for optimum performance set
[`$compile_check`](#variable.compile.check) to FALSE.


    <?php
    require('Smarty.class.php');
    $smarty = new Smarty;

    $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
    $smarty->setCompileCheck(false);

    $smarty->display('index.tpl');
    ?>

        

If [`$force_compile`](#variable.force.compile) is enabled, the cache
files will always be regenerated. This effectively disables caching,
however this also seriously degrades performance.
[`$force_compile`](#variable.force.compile) is meant to be used for
[debugging](#chapter.debugging.console) purposes. The appropriate way to
disable caching is to set [`$caching`](#variable.caching) to
Smarty::CACHING\_OFF.

The [`isCached()`](#api.is.cached) function 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.


    <?php
    require('Smarty.class.php');
    $smarty = new Smarty;

    $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

    if(!$smarty->isCached('index.tpl')) {
        // No cache available, do variable assignments here.
        $contents = get_database_contents();
        $smarty->assign($contents);
    }

    $smarty->display('index.tpl');
    ?>

        

You can keep parts of a page dynamic (disable caching) with the
[`{nocache}{/nocache}`](#language.function.nocache) block function, the
[`{insert}`](#language.function.insert) function, or by using the
`nocache` parameter for most template functions.

Let\'s say the whole page can be cached except for a banner that is
displayed down the side of the page. By using the
[`{insert}`](#language.function.insert) function for the banner, you can
keep this element dynamic within the cached content. See the
documentation on [`{insert}`](#language.function.insert) for more
details and examples.

You can clear all the cache files with the
[`clearAllCache()`](#api.clear.all.cache) function, or individual cache
files [and groups](#caching.groups) with the
[`clearCache()`](#api.clear.cache) function.


    <?php
    require('Smarty.class.php');
    $smarty = new Smarty;

    $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

    // clear only cache for index.tpl
    $smarty->clearCache('index.tpl');

    // clear out all cache files
    $smarty->clearAllCache();

    $smarty->display('index.tpl');
    ?>