RE: [SMARTY] Cache result from db-query?
| From: | Peter Billen | Date: | Tue, 04 Mar 2003 17:15:04 +0000 |
| Subject: | RE: [SMARTY] Cache result from db-query? | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-8395@lists.php.net to get a copy of this message | ||
If I understand well, you make an 'extra cache file', called topTen.tpl,
only containing the serialized array, returned by your database query?
This might work, but aren't you 'abusing' the caching functions of
smarty this way? I mean: actually, you don't cache template stuff, you
just use the smarty caching functions to cache an array, containing data
that needs to be parsed within a template. Doesn't this cause a lot of
overhead, because smarty 'thinks' you are caching parsed template files
instead of plain data?
Maybe it's better to cache the parsed template, instead of the data
only.
In your way, smarty still has to loop through your array (maybe that's
not so bad) + I think, since you are using the smarty cache functions
for purposes it isn't made for, you could have some overhead.
However, don't mark me on my words, since I don't have any experience
with smarty & caching itself.
Peter
-----Original Message-----
From: Eskil Keskikangas [mailto:eskil@funplanet.com]
Sent: dinsdag 4 maart 2003 17:24
To: smarty-general@lists.php.net
Subject: RE: [SMARTY] Cache result from db-query?
I did like this. I don't know if it's a good way to solve the problem,
but
it works prefect for me.
The script:
$smarty->caching = 2;
if (!$smarty->is_cached("topTen.tpl",$cat)) {
$data = $qe->getAllThingsFromCategory();
$smarty->assign("topData",serialize($data->itemsArray));
}
$smarty->cache_lifetime = 600;
$topTen = $smarty->fetch("topTen.tpl",$cat);
$result = unserialize($topTen);
$smarty->assign("result", $result);
$smarty->caching = 0;
$smarty->display('index.tpl');
Template (topTen.tpl):
{$topData}
Template (index.tpl):
{foreach item=item from=$result}
{$item.productName}{$item.productPrice}
{/foreach}
This way I can cache the result from the query in a serialize array in
the
templatefile.
Please give me some feedback to this. Is there a better way to do this?
BG
Eskil
At 15:09 2003-03-04 +0100, Eskil Keskikangas wrote:
>Will this work?
>
>function smarty_function_top_ten_products($params, &$smarty) {
>
> extract($params);
>
> $smarty->caching = 2;
>
> if (!$smarty->is_cached("TORGET/topTen.tpl")) {
> $data = rand(0,10);
> $smarty->assign("topData",$data);
> }
>
> $smarty->cache_lifetime = 180;
>
> return $smarty->fetch("TORGET/topTen.tpl");
>}
>
>What will become cached when i define "caching = 2" in the function?
>
>/Eskil
>
>At 14:51 2003-03-04 +0100, Peter Billen wrote:
>>forgot url:
>>
>>http://smarty.php.net/manual/en/language.function.insert.php
>>
>>Peter
>>
>>-----Original Message-----
>>From: Peter Billen [mailto:peter@clueless.be]
>>Sent: dinsdag 4 maart 2003 14:39
>>To: smarty-general@lists.php.net
>>Subject: RE: [SMARTY] Cache result from db-query?
>>
>>I am not used to caching & smarty, but I think it's impossible to
cache
>>database queries with smarty. The only thing that smarty (should?)
cache
>>is output of 'data'.
>>
>>So I think it's only possible to cache a parsed template (or parts of
>>it), so next time, smarty does not have to re-loop the whole
>>datastructures again.
>>
>>However, I could be wrong!
>>
>>You may want to look to the 'insert' function, in order to cache parts
>>of a template (and not the whole template itself).
>>
>>Peter
>>
>>-----Original Message-----
>>From: Eskil Keskikangas [mailto:eskil@funplanet.com]
>>Sent: dinsdag 4 maart 2003 14:32
>>To: smarty-general@lists.php.net
>>Subject: [SMARTY] Cache result from db-query?
>>
>>Hi,
>>
>>I want to cache a small part of my output. It's a toplist from a
>>mysql-db
>>that results in a slow query. But I don't understand how to do this.
If
>>I
>>enable cache won't all output get cached then? Give me a hint please.
:)
>>
>>BG
>>Eskil
>>
>>
>>--
>>Smarty General Mailing List (http://smarty.php.net/)
>>To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>>--
>>Smarty General Mailing List (http://smarty.php.net/)
>>To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>>--
>>Smarty General Mailing List (http://smarty.php.net/)
>>To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>--
>Smarty General Mailing List (http://smarty.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php