Re: Please help on caching [example]
| From: | Brian E . Lozier | Date: | Tue, 01 Jan 2002 01:57:06 +0000 |
| Subject: | Re: Please help on caching [example] | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-2460@lists.php.net to get a copy of this message | ||
Andrei, in response to your previous email:
> // fetch the right template to include it
> $tpl->assign('Content', $tpl->fetch('index.tpl', $REQUEST_URI));
> $tpl->display('main.tpl');
Well, also put $tpl->caching = false before the display() call.
As you can see from the message below, I tried this. If you click the link, you will see that the
caching is
not working correctly. You can see the source of the pages in question here:
http://www.massassi.org/saberworks/source.php
The problem is (as I mentioned below, but I will restate in case I wasn't clear) that the cache
gets
regenerated based on something to do with 'main.tpl' - it's very strange. Here are
the steps I take that
lead me to believe there's a big problem:
1. clear the cache of all pages completely
2. access index.php - behavior: it didn't find a cache for ('index.tpl',
$REQUEST_URI) so it generated one
and displayed it
3. access viewcomments.php?type=news&resourceID=51 - behaviour: it didn't find a cache for
('viewcomments.tpl', $REQUEST_URI) so it generated one and displayed it
4. access index.php again - behavior: it didn't find a cache so regenerates it! This is the
problem... it
should have found the previous cache and displayed it
5. RELOAD index.php - it finds the cache and displays it
6. RELOAD index.php - it finds the cache and displays it
7. access a completely different site
8. access index.php again - it finds the cache and displays it
9. access viewcomments.php?type=news&resourceID=51 again - it doesn't find the cache!
argh. So it has to
recreate it
10. access index.php again - can't find the cache so regenerates it
So what seems to be happening is that it's basing the regeneration of a cached template
(index.tpl,
viewcomments.tpl) on where I last accessed main.tpl from. Which is very, very strange.
Shouldn't the cache
have absolutely no bearing on main.tpl since I turn off caching every time before calling
display('main.tpl')?
This seems to be the last stumbling block... any help would be greatly appreciated.
Brian
----- Original Message -----
From: Brian E. Lozier <brian@massassi.net>
To: smarty@lists.ispi.net
Date: 12/28/01 3:39:43 PM
>
>Example:
>http://www.massassi.org/saberworks/
>
>If it says 'not cached' at the top, it's telling you that it's not fetching
>the cached content for
>('index.tpl', $REQUEST_URI). Now, everything should be cached, because I clicked on
>it all and it's saving
>for a day. (everything being the two scripts I've converted, the index.php and the
>viewcomments.php). So
>now, if you don't see 'not cached' you are getting a cached file. Then go view a
>comments. That should
have
>been cached as well (I've clicked all of them just now). Now click back to the main.
>OOOPS! it's not
cached
>again for some reason.
>
>You can see all the templates in /saberworks/templates/ ('main.tpl',
>'index.tpl', and 'viewcomments.tpl' are
>what we're dealing with now).
>
>You can see the source of both here:
>http://www.massassi.org/saberworks/source.php
>
>The problem now is that when I finally use all those cheap hacks to get the caching working
>semi-correctly,
>the thing is regenerating cache files after I access separate pages that use the same
>'main.tpl' which makes
>no sense to me.
>
>----- Original Message -----
>From: Brian E. Lozier <brian@massassi.net>
>To: smarty@lists.ispi.net
>Date: 12/28/01 3:21:36 PM
>
>>
>>So you're saying to define inside the main.tpl which template I'm supposed to
>>include based on a variable I
>>pass in? Isn't this putting too much testing inside the template considering I have
>>dozens of pages and
>>countless combinations of IDs I'm passing in to generate content?
>>
>>For example, I have a news article. Any user can post comments on that article. So you
>>pass in the
>>articleID and the viewcomments.php page does a bunch of database calls and returns the array
>>which I assign
>>to a section. Well the cache_id works as $REQUEST_URI *IF* I have a separate template
>>defining each page.
>>Such as viewcomments.tpl and addcomment.tpl. But since my design all resides in main.tpl,
>>the cached
>content
>>that is returned is *always* the first page that gets accessed directly after the cache
>>period is expired.
>>So if they access 'addcomment.php' first, then every subsequent page, be it
>>viewcomment.php or index.php is
>>returned as 'addcomment.php' which makes the whole system not work. Everything
>>works perfectly when
caching
>>is set to false.
>>
>>I have tried all the hacks suggested so far in the list, but nothing seems to work. The
>>whole system is
>>getting confused for some reason. When I set caching to false then display the
>>'main.tpl', it seems to
>>generate the correct content. But there is a huge problem. Every time I reload, it looks
>>fine, it's
>>generating from cache. But then I access a separate page, set up the same way, that still
>>uses main.tpl,
>and
>>that page looks fine. But it generates it dynamically. So then I go back to the original
>>page, and it had
>>deleted the cached copy, or expired it somehow, because every time I leave the page, then
>>come back, it
>>regenerates the cache - even though it's still within the time limit (a day) and no
>>template or php page
has
>>changed. Plus, all the options to not check and all that are turned correctly, and
>>remember, it is
>returning
>>cached content if I reload over and over... just not if I visit a separate page and then
>>come back.
>>
>>I guess I don't see why this is such a hard problem to solve. Haven't other
>>people used smarty for coding
>>applications that require user logins and whatnot? Haven't they used it to cache
>>content that changed
>>depending on whether you're logged in? Hasn't this system been used in an
>>environment larger than the
>>examples in the documentation?
>>
>>It just seems like it would be MUCH easier if the thing had an option to just get cached
>>content...
>>
>>if($tpl->is_cached('index.tpl', $REQUEST_URI)) {
>> $tpl->assign('Content', $tpl->fetch_cached('index.tpl',
>> $REQUEST_URI));
>>}
>>else {
>> // do all my calls and assigns, then:
>> $tpl->assign('Content', $tpl->fetch('index.tpl', $REQUEST_URI));
>>}
>>
>>The problem is that the cache variable is 'global' in that it doesn't respond
>>properly when you have mixed
>>cached and non-cached content :( When I turn it back off to display, it drops all the
>>output. It also
>seems
>>to re-generate the cache each time you access a separate page that uses the same template
>>for some reason,
a
>>behaviour that I just can't figure out for the life of me.
>>
>>It's getting to the point that I'm spending so much time trying to figure out how
>>to do it in smarty, that
I
>>could just have written a template/caching system that works correctly in my case. I'm
>>getting so
>>frustrated, I can't even get an answer on how the cache is handled. You say 'per
>>page' but that makes no
>>sense because smarty has no idea what page I'm calling it from! So in that case, it
>>must be per-template,
>>which breaks my whole system because I use the same single template throughout the site.
>>Which is the root
>>of all the problems, apparently. It just doesn't make sense to have to split your page
>>design into two
>files
>>(header, footer) in a templated system.
>>
>>And all my questions about why it's storing two copies of the cache, which one it
>>actually uses, how to
tell
>>smarty to only cache specific content, etc., just go unanswered or replied to with smug,
>>"go look at the
>>compile some time." I already did, it helps nothing. It doesn't show me how the
>>actual content is
>inserted,
>>it only shows the template itself with the {if} replaced with if().
>>
>>Brian
>>------------------------------------
>>"How should I know if it works? That's what beta testers are for. I only coded
>>it." - Linus Torvalds
>>
>>----- Original Message -----
>>From: Monte Ohrt <monte@ispi.net>
>>To: smarty@lists.ispi.net
>>Date: 12/28/01 3:17:12 PM
>>
>>>
>>>Maybe there is a better way to approach your problem.
>>>
>>>If I understand correctly, you should be able to do something like this:
>>>
>>>// $page is our identifier for our content
>>>
>>>$smarty->assign("page",$page);
>>>
>>>// possibly use $page or $REQUEST_URI for the cache_id, depeding on if
>>>you need separate caches per $page or not
>>>$smarty->display('main.tpl',$REQUEST_URI);
>>>
>>>
>>>main.tpl
>>>---------
>>>
>>><html>
>>><body>
>>> {if $page eq "article"}
>>> {include file="article.tpl"}
>>> {elseif $page eq "category"}
>>> {include file="category.tpl"}
>>> {else}
>>> {include file="default.tpl"}
>>> {/if}
>>></body>
>>></html>
>>>
>>>
>>>This way, the page fetching happens all in one go, under one cache file
>>>(for each cache_id).
>>>
>>>Does this help at all?
>>>
>>>Monte
>>>
>>>"Brian E. Lozier" wrote:
>>>>
>>>> >If you don't want a particular template cached, then you can turn off
>>>> >caching before displaying it.
>>>> >
>>>> >$smarty->caching = false;
>>>> >$smarty->display('login.tpl');
>>>>
>>>> See, this is why I don't think you guys are understanding my problem. *ALL*
>>>> my $tpl->display() calls
>look
>>>> like:
>>>>
>>>> $tpl->display('main.tpl');
>>>>
>>>> At the end of EVERY page in my application. Because 'main.tpl' contains
>>>> my entire design.
>>>>
>>>> [html][body]
>>>> {Content}
>>>> [/body][/html]
>>>>
>>>> Then I populate {Content} with a $tpl->fetch() command, such as:
>>>>
>>>> $tpl->assign('Content', $tpl->fetch('userregister.tpl'));
>>>>
>>>> The problem is that it's always trying to cache main.tpl exactly the same no
>>>> matter what. If I pass a
>>>> $REQUEST_URI, it still does the same thing. If I pass a $REQUEST_URI to the
>>>> ->fetch(), it caches my
>>content
>>>> correctly, but there's no way to tell ->display('main.tpl') to
>>>> use *that* cached content. See, since I
>>never
>>>> call $tpl->display('userregister.tpl'), there's no way to
>>>> 'turn off caching' before I do that, because
>>that
>>>> step never takes place.
>>>>
>>>> And I tried turning off (and turning on) caching just before the fetch, but it
>>>> still doesn't work. It
>>>> returns nothing at that point.
>>>>
>>>> >No, you shouldn't have to do this. You just need to use the caching
>>>> >appropriately for your application. Are you using Zend Cache or APC with
>>>> >your application? Even without using Smarty's built-in caching, it
>>>> >should still be quite fast, and most likely faster than other templating
>>>> >solutions because there is no template parsing needed, it just executes
>>>> >PHP scripts. The only noticable overhead would be the compilation of the
>>>> >PHP scripts themselves, which is where APC or Zend Cache will make a
>>>> >_huge_ difference in performance, and Smarty can take advantage of that
>>>> >because all the templates _are_ PHP scripts. If your page loading times
>>>> >are slow because of database calls, then the problem does not lie in
>>>> >your templating solution.
>>>>
>>>> I'm not using any of those. Smarty is taking half a second 0.576877 or
>>>> whatever, and the rest are
taking
>>>> 0.08587575 or something. When I turn on caching, smarty is returning the page in
>>>> comparable amounts of
>>time.
>>>>
>>>> >> There needs to be some way to tell smarty to get the cached content for
>>>> >> certain templates within a
>page
>>>> but
>>>> >> not others.
>>>> >
>>>> >Caching happens at a "page" level. If you want certain components of
>>>> >your page dynamic, use the {insert} function to create that content.
>>>> >That is exactly what it is intended for.
>>>>
>>>> This is the very root of the problem. Since Smarty thinks *all* my pages are
>>>> 'main.tpl' it can't
>>correctly
>>>> cache the parts I want. I can't use insert, because I populate the {$Content}
>>>> portion of my main.tpl
>>>> depending on what page they're accessing. And the {$Content} is what I want
>>>> cached, not the main.tpl.
>>>> main.tpl doesn't require any database calls, just some basic logic such as {if
>>>> $Logged} {/if}, etc. But
>>>> since that gets cached, I have to have a way not to cache main.tpl.
>>>>
>>>> >You can obtain all of this by making non-cached components of your page
>>>> >with the {insert} tag. that is exactly how we handle feedback, polls,
>>>> >banners, or any other content that needs to remain dynamic on the page.
>>>>
>>>> That doesn't make much sense, because then you're placing logic inside
>>>> the design templates. You're
>>forcing
>>>> me, ahead of time, to write a function (of which I have *2* in my entire project,
>>>> the rest of all my
>>>> functionality is in objects and methods) that corrosponds with whatever content
>>>> isn't cached. So this
>>means
>>>> I have to write a PHP function to do some very simple HTML display/presentation
>>>> stuff like outputting a
>>>> simple form. Also, since everything is in main.tpl, I can't just pick some
>>>> specific point and add an
>>>> {insert} tag.
>>>>
>>>> Take for instance my 'right hand sidebar' - this is where I would put the
>>>> poll, or the featured articles
>>>> list, or whatever I want. But my application can replace the 'standard'
>>>> sidebar with virtually any
>>content
>>>> it wants (for instance, in the admin section, the admin options are there... in the
>>>> files section,
>>>> thumbnailed screenshots appear there). There is no way to make one canned {insert}
>>>> function that can
>>handle
>>>> all of these options. And if the content isn't dynamic for that page, IE: if
>>>> it uses the stock one, it
>>>> should be able to cache it.
>>>>
>>>> I think the reason this is so hard to explain is because nobody else (except that
>>>> one guy) has ever use
>>the
>>>> templates like this. You guys are using two {include} statements, one at the top
>>>> and one at the bottom,
>>to
>>>> hold your design. From the beginning, this was what I wanted to get away from,
>>>> because it forces me to
>>make
>>>> an artificial split (or 5 or 6 depending on the complexity of the design) in my
>>>> design. It confuses the
>>>> designers to have to view their page in multiple files split up in the middle of xx
>>>> number of nested
>>tables.
>>>>
>>>> I've tried what seems like a dozen different ways of explaining this, but most
>>>> people aren't
>understanding
>>>> for some reason. I don't know what else to say... Thanks again for all the
>>>> help, though.
>>>>
>>>> >>
>>>> >> ----- Original Message -----
>>>> >> From: Peter Bowyer <reywob@f2s.com>
>>>> >> To: smarty@lists.ispi.net
>>>> >> Date: 12/27/01 3:34:09 AM
>>>> >>
>>>> >> >
>>>> >> >At 04:06 PM 12/26/01 -0800, you wrote:
>>>> >> >>I have queried this list quite a few times but nothing is helping.
>>>> >> >> I
>>>> >> >>upgraded to the latest version. I have
>>>> >> >>tried all suggestions. Is this a bug in Smarty? Does anyone else
>>>> >> >>have
>>>> >> >>this "dynamic" content caching
>>>> >> >>correctly using the cache_id? Because Smarty is just ignoring my
>>>> >> >>cache_id
>>>> >> >>I pass, no matter what it is.
>>>> >> >>Please, I really need to get this working, it's important.
>>>> >> >
>>>> >> >I have it working with Smarty 1.4.5 - see it in action at
>>>> >>
>>>> >> >><http://casualties.commemoratewtc.com> where each page is
>>>> >> >cached for 3 days.
>>>> >> >
>>>> >> >>Here is my 'main.tpl' which is the main layout of the
>>>> >> >>site (note I
>>>> >> >>replaced > and < with ] and [ because my
>>>> >> >>email program strips the HTML for some reason):
>>>> >> >>
>>>> >> >>main.tpl:
>>>> >> >>[html]
>>>> >> >>[head][title]{$PageTitle}[/title][/head]
>>>> >> >>[body]
>>>> >> >>{$Content}
>>>> >> >>[/body]
>>>> >> >>[/html]
>>>> >> >
>>>> >> >Great - another coder who hates header/footer files and prefers the
>>>> >> >design
>>>> >> >to be in one file! That now makes two of us for whom the
>>>> >> >"normal" smarty
>>>> >> >method doesn't suit :-)
>>>> >> >
>>>> >> >>I have tried passing both $REQUEST_URI (which *is* passing
>>>> >> >>correctly, I
>>>> >> >>verified!) and $catID (since it's
>>>> >> >>unique for each of the cache results I'm looking for) to
>>>> >> >>main.tpl like:
>>>> >> >>$tpl->display('main.tpl', $REQUEST_URI); // and:
>>>> >> >>$tpl->display('main.tpl', $catID);
>>>> >> >>
>>>> >> >>I've also tried passing it to the $tpl->fetch() where I
>>>> >> >>get the actual
>>>> >> >>files.tpl template like:
>>>> >> >>$tpl->assign("Content",
>>>> >> >>$tpl->fetch('files.tpl', $REQUEST_URI));
>>>> >> >
>>>> >> >This is the right one. After this, just call
>>>> >> >$tpl->display('main.tpl'); and
>>>> >> >it should work. You could always try
>>>> >> >$tpl->display('main.tpl',
>>>> >> >$REQUEST_URI); as well.
>>>> >> >
>>>> >> >Did you clear the cache and the compiled templates before you tried
>>>> >> >this?
>>>> >> >
>>>> >> >Sorry I can't be more help.
>>>> >> >Peter.
>>>> >> >
>>>> >> >--
>>>> >> >Maple Design -
>>>> >> >http://www.mapledesign.co.uk
>>>> >> >
>>>> >> >
>>>> >> >---
>>>> >> >You are currently subscribed to smarty as: brian@massassi.net
>>>> >> >To unsubscribe send a blank email to $subst('Email.Unsub')
>>>> >> >Visit Smarty's homepage at
>>>> >> >http://www.phpinsider.com/php/code/Smarty/
>>>> >>
>>>> >> ---
>>>> >> You are currently subscribed to smarty as: monte@ispi.net
>>>> >> To unsubscribe send a blank email to $subst('Email.Unsub')
>>>> >> Visit Smarty's homepage at
>>>> >> http://www.phpinsider.com/php/code/Smarty/
>>>> >
>>>> >--
>>>> >Monte Ohrt <monte@ispi.net>
>>>> >http://www.ispi.net/
>>>> >
>>>> >---
>>>> >You are currently subscribed to smarty as: brian@massassi.net
>>>> >To unsubscribe send a blank email to $subst('Email.Unsub')
>>>> >Visit Smarty's homepage at
>>>> >http://www.phpinsider.com/php/code/Smarty/
>>>
>>>--
>>>Monte Ohrt <monte@ispi.net>
>>>http://www.ispi.net/
>>>
>>>---
>>>You are currently subscribed to smarty as: brian@massassi.net
>>>To unsubscribe send a blank email to $subst('Email.Unsub')
>>>Visit Smarty's homepage at
>>>http://www.phpinsider.com/php/code/Smarty/
>>
>>
>>---
>>You are currently subscribed to smarty as: brian@massassi.net
>>To unsubscribe send a blank email to $subst('Email.Unsub')
>>Visit Smarty's homepage at
>>http://www.phpinsider.com/php/code/Smarty/