Re: [SMARTY] cache+dynamic site - i don't get it...

From: Date: Wed, 01 May 2002 23:12:25 +0000
Subject: Re: [SMARTY] cache+dynamic site - i don't get it...
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-3969@lists.php.net to get a copy of this message
from http://www.zend.com/zend/tut/tutsweatpart1.php: Once you have your base application in place, there in one additional powerful feature of the Smarty template system you can use to significantly improve you applications performance: server side caching. This is implemented by using $tpl->caching = true; to enable caching and using $tpl->cache_lifetime = 86400; to set the duration of time the page should be cached (in seconds). In addition, you can specify a cache name to store multiple versions of the page in the cache. In this example, you need a separate copy per view and depending if you are an administator of the application. The $cache_num = $PHP_SELF; code creates a single cache for this script and adding the cache name to the Smarty display ($tpl->display('links.tpl', $cache_num);) uses it if caching is enabled. To make sure you are actually saving time, check to see if your template is cached before running your expensive database queries: if ( !$tpl->is_cached($template, $cache_num) ) { //expensive queries here... } $tpl->display($template, $cache_num); You may want to have some of the content of your page be dynamic, even though you would like to have most of the page cached. Smarty allows for this using the insert function. You can observe this behavior in the script presented, the html comment will show you the time at which the page was generated (<!-- page cached on {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"} --> ) but the visible greeting will always display the current time (<p>Page Generated on {insert name=get_date format="%A, %B %d, %Y %X"} ). ----- Original Message ----- From: "k" <szkaroly@freemail.hu> To: <smarty-general@lists.php.net> Sent: Wednesday, May 01, 2002 6:00 PM Subject: [SMARTY] cache+dynamic site - i don't get it... > [posted this to the php.smarty.general newsgroup, but did not show up - but sorry if it shows up two times] > > Hi, > > I have started using Smarty a few days ago. I've used PHP and other template systems before. > So - I think - I've understood the main concept of Smarty, and I can create pages with templates. > > My problem is that I don't understand the "relation" of the cache feature and dynamic content. > > The way I use Smarty now is the following: > > 1. gather data from database(s), put it in arrays, etc. > 2. assign data to Smarty/template variables [assign()] > 3. use display() to send the html out. > > My question: if I turn caching on, the 1st step above (get data from mysql database, put it in arrays, etc) will still be executed? > (My goal is to run this part only once, only when data has changed.) > If it's still executed, how sould I write the code, to avoid redundant db access? > > I know I can give a cache ID to the display() function, based on the GET vars (query_string), I've found the "insert" function (and use it for the banner), but things just don't come together in my head :O > > [A few notes about my site. I hope You don't mind if I explain it's > structure a bit... > > I. static header > II. a box with list of various items (names + values) > III. search form + search result box > IV. static footer > > > I. and IV. are OK, these are in header and footer.tpl. > > II. contains data that only changes a few times in a month. So I really > don't want to generate it on every visit (pageview). > I was thinking about generating it with another script, and use it as a > (sub)template, but that would not be the "Smarty way". > > III.: search results and detailed information. > This part can change with every pageview. > > So, this site is something like a link-index page: you can click on > categories, than you get the links in that category, and you can view the description of an item with another click. > > Category names don't change every day (but change sometimes), OTOH the > results box have to be generated on the fly.] > > TIA, and excuse me for the long post. > > regards, > > k > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.smarty.general (#3969) next »