From 72ca37bd83b4e38e94608d04346f86113315a9b5 Mon Sep 17 00:00:00 2001 From: mohrt Date: Thu, 1 Feb 2001 17:40:59 +0000 Subject: [PATCH] updated docs for caching, added clear_all_cache() directive --- FAQ | 36 ++++++++++++- Smarty.class.php | 16 ++++++ docs.sgml | 115 +++++++++++++++++++++++++++++++++--------- libs/Smarty.class.php | 16 ++++++ 4 files changed, 157 insertions(+), 26 deletions(-) diff --git a/FAQ b/FAQ index 82b89b5c..57d8f88e 100644 --- a/FAQ +++ b/FAQ @@ -21,8 +21,42 @@ A: Smarty reads the template files and creates PHP scripts from them. Once the template files again. If you change a template file, Smarty will recreate the PHP script for it. All this is done automatically by Smarty. Template designers never need to mess with the generated PHP scripts or even - know of their existance. + know of their existance. (NOTE: you can turn off this compile checking step + in Smarty for increased performance.) +Q: Why can't I just use APC (or Zend Cache)? +A: Smarty and these cache solutions have nothing in common. What APC does is + caches compiled bytecode of your PHP scripts in shared memory or in a file. + This speeds up server response and saves the compilation step. Smarty + creates PHP scripts (which APC will cache nicely) and also has it's own + internal caching mechanism for the output of the template contents. For + example, if you have a template that requires several database queries, + Smarty can cache this output, saving the need to call the database every + time. APC cannot help you here. Smarty and APC (or Zend Cache) actually + complement each other nicely. If performance is of the utmost importance, we + would recommend using one of these with any PHP application, using Smarty or + not. + +Q: Is Smarty faster than ? +A: This would mostly depend on the other template engine, but as a + general rule of thumb: Without a PHP caching solution like APC or + Zend Cache, Smarty is most likely as fast, or possibly slower. With + APC, Smarty is mostly like as fast or much faster. The reason is + this: Smarty generates PHP scripts from your templates. The more + templates your application has, the more PHP scripts Smarty + generates. This in turn requires more time for the PHP parser to + compile the PHP scripts. With APC, this compilation step is cached. + So as the complexity of the templates increase, the performance + savings go up accordingly. Also, most other template solutions parse + the template files on each invocation. The more complex the + templates are, the longer they take to parse them. + + The above comparison assumes that you are not using Smarty's + built-in ability to cache templates. If you are, that makes this + comparison pretty useless since Smarty will basically be displaying + static content instead of generating templates, which of course will + be magnitudes faster. + Q: Do you have a mailing list? A: Yes. Subscribe by sending an e-mail to subscribe-smarty@lists.ispi.net. This is also archived at http://marc.theaimsgroup.com/ under www/smarty diff --git a/Smarty.class.php b/Smarty.class.php index 0c235884..6a6fb2f6 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -256,6 +256,22 @@ class Smarty return $results; } +/*======================================================================*\ + Function: clear_all_cache() + Purpose: clear all the cached template files. +\*======================================================================*/ + + function clear_all_cache() + { + while($curr_file = readdir($this->cache_dir)) { + if ($curr_file == '.' || $curr_file == '..') + continue; + if(substr($curr_file,-4) == '.che') + unlink($curr_file); + } + } + + /*======================================================================*\ Function: compile() Purpose: called to compile the templates diff --git a/docs.sgml b/docs.sgml index ac282815..d97e83af 100644 --- a/docs.sgml +++ b/docs.sgml @@ -116,6 +116,7 @@ although this may not be needed (nor recommended) since the engine is so customizable. + Built-in caching support (new in 1.3.0) How Smarty works @@ -130,10 +131,16 @@ Caching - Our initial intention was to build caching into Smarty. However, - since the template engine is actually executing PHP scripts instead of - parsing template files, the need for a cache was dramatically reduced. - We may implement this in a future version of Smarty as the need arises. + Smarty can cache the output of your generated templates. By default + this is disabled. If you enable caching, Smarty will store a copy + of the generated template output, and use this until the copy + expires, regenerating a new one. If your templates generate the + same content over and over, using the cache will result in huge + performance gains. The default cache expire time is one hour, and + can be configured from the class. The exception to the rule is the + insert tag. Anything + generated by the insert tag is not cached, but run dynamically on + every invocation, even within cached content. @@ -157,7 +164,9 @@ Each installation of a Smarty application minimally needs a templates directory and a compiled templates directory. If you use configuration files you will also need a directory for those. By default these are - named "templates", and "templates_c" and "configs" respectively. + named "templates", and "templates_c" and "configs" respectively. If you + plan on using caching, you will need to create a "cache" directory, also + with permission to write files. Copy the Smarty.class.php, Smarty.addons.php and Config_File.class.php @@ -178,6 +187,10 @@ gtar -zxvf Smarty-1.0.tar.gz mkdir templates_c chown nobody:nobody templates_c +chmod 700 templates_c +mkdir cache +chown nobody:nobody cache +chmod 700 cache @@ -200,19 +213,51 @@ chown nobody:nobody templates_c Upon each invocation of the PHP application, Smarty recursively traverses the template directory and its subdirectories and - searches for all the files with the template extension that - have changed (later time stamp) since the last time they were - compiled. For each one that has changed, it recompiles that - template. By default this variable is set to true. The compile - check has very minimal impact on the application performance. - However, once an application is put into production and it is - initially compiled, the compile_check step is no longer needed. - You may set this to "false" *after* the initial compile. Then - Smarty will no longer check for changed template files. Note - that if you change this to "false" and a template file is - changed, you will *not* see the change since the template will - not get recompiled. Set it to "true", invoke the application, - then set it back to "false". + searches for templates that have changed (later time stamp) + since the last time they were compiled. For each one that has + changed, it recompiles that template. By default this variable + is set to true. Once an application is put into production and + it is initially compiled, the compile_check step is no longer + needed. Be sure to set $compile_check to "false" to improve + performance! Note that if you change this to "false" and a + template file is changed, you will *not* see the change since + the template will not get recompiled. See $force_compile + + + + $force_compile + + This forces Smarty to compile all templates on every + invocation. This setting overrides $compile_check. By default + this is disabled. This is handy for development and debugging. + It should never be used in a production environment. + + + + $caching + + This tells Smarty whether or not to cache the output of the + templates. By default this is set to false. If your templates + generate the same content over and over, it is advisable to + turn on caching. This will result significant performance + gains. Smarty uses a combination of the template being called + and the URL ($PHP_SELF) for the cache indentification. This way + you can have multiple caches for the same template. NOTE: If + you ever turn caching off after templates are compiled, be sure + to recompile all of your templates once. Otherwise you may get + undesireable results. This was added to Smarty 1.3.0. + + + + $cache_lifetime + + This is the length of time in seconds that a template cache is + valid. Once this time has expired, the cache will be + regenerated. $caching must be set to "true" for this setting to + work. You can also force the cache to expire with clear_all_cache. This was + added to Smarty 1.3.0. @@ -226,10 +271,18 @@ chown nobody:nobody templates_c $compile_dir This is the name of the directory where compiled templates are - located. By default this is "./templates_c". NOTE: this was + located. By default this is "./templates_c". This was added to Smarty version 1.2.1. + + $cache_dir + + This is the name of the directory where template caches are + located. By default this is "./cache". This was + added to Smarty version 1.3.0. + + $compile_dir_ext @@ -375,6 +428,19 @@ chown nobody:nobody templates_c This clears the values of all assigned variables. + + clear_all_cache + + + void clear_all_cache + + + + + This clears the entire template cache. This was added to Smarty + 1.3.0. + + get_template_vars @@ -815,7 +881,7 @@ Intro = """This is a value that spans more - + insert The insert tag in Smarty serves a special purpose. You may @@ -856,11 +922,10 @@ Intro = """This is a value that spans more and display the returned results in place of the insert tag. - Another thing to keep in mind for the insert tag is caching. Smarty does not - currently support caching but if that is eventually implemented, insert - tags will not be cached. They will run dynamically every time the page - is created. This works good for things like banners, polls, live weather, - user feedback areas, etc. + A note on caching: If you have caching turned on, insert tags will + not be cached. They will run dynamically every time the page is + created. This works good for things like banners, polls, live + weather, user feedback areas, etc. diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 0c235884..6a6fb2f6 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -256,6 +256,22 @@ class Smarty return $results; } +/*======================================================================*\ + Function: clear_all_cache() + Purpose: clear all the cached template files. +\*======================================================================*/ + + function clear_all_cache() + { + while($curr_file = readdir($this->cache_dir)) { + if ($curr_file == '.' || $curr_file == '..') + continue; + if(substr($curr_file,-4) == '.che') + unlink($curr_file); + } + } + + /*======================================================================*\ Function: compile() Purpose: called to compile the templates