Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

compile and cache confusion.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
kk
Smarty Rookie


Joined: 10 Aug 2010
Posts: 22

PostPosted: Thu Sep 29, 2011 7:08 pm    Post subject: compile and cache confusion. Reply with quote

I might be slow in the intake but I try to understand the compiling – caching behaviour.

When should Smarty compile the templates? Every time or after a certain amount of time?
When would I use caching if Smarty caches the compiled template anyway?
Against what does smarty check the compiled status and the cached status? The timestamp written in my resource handler seem to be without any effect.

Where does Smarty save the cached files? In my case I see the compiled and cache in the same directory even if set to different locations. It only appends ‘.cache’ after the name and before ‘.php’.
$ smarty->setCacheDir(‘../somewhere/cache’);
$ smarty->setTemplateDir(‘../somewhere/template_c’);

What is the difference between a compiled file and a cached file apart the name particular when own resource handler is used? The files are identical on my system. Only the native resource handler writes files into the cache directory.

As an example:
Initialize smarty:
$smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;
$smarty->setCacheLifetime(60);

I use my own resource handler:
class Smarty_Resource_Module extends Smarty_Resource_Custom {…}

which pulls out the correct templates and produces the correct output APART from the correct timestamp as it seems.

My own debug show following output:
DB stamp used mcbModifiedTS - 1317080020 called for: homepagemaincontent compare to: 1317317487 difference: 237467

Explained -> A database module(homepagemaincontent) is delivered with mcbModifiedTS (timestamp of last change in format of MySQL timedate field) of 1317080020. This is compared to the current time (1317317487) ((time()) which is a difference of 237467 which is more than 60 seconds BUT a recompile won’t happening.
Same when compared against a file system date and time.

And why does Smarty call function fetchTimestamp twice?

This is the same behaviour when used without caching at all.

This seems all odd to me.
As far as I understand the compiled template should be recompiled if the source has changed, hence the timestamp check.
And the cached file will never be replaced apart if explicitly requested and the replacement is a recompiled template.

I know this are many questions but is there someone who could explain all this in a easy way?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Sep 29, 2011 8:14 pm    Post subject: Reply with quote

1. I think this is just a typo, should be
Code:
$smarty->setCacheDir(‘../somewhere/cache’);
$smarty->setCompileDir(‘../somewhere/template_c’);


2. compileDir does hold the compiled PHP code of your template

3. cacheDir does cache the rendered HMTL image of your page if caching is enabled. This is also a PHP file as the page contend is capsulated in a function and it may include PHP code from nocache sections.

4. compileDir can hold different 2 versions for one template. The compiled code for cached pages could look different from a nocache page because of nocache sections and other. The xxx.cache.php file is the compiled code when caching is enabled not to be confused with the cache file in cacheDir.

5. Note that the MySql timestamps are not identical with the time() timestamps. You must convert the MySql timestamp into Unix format. Look in the MySql forum how this could be done.

6. I'm not sure why Mysql gets called twice. This may need some investigation.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Sep 29, 2011 8:29 pm    Post subject: Re: compile and cache confusion. Reply with quote

kk wrote:
When should Smarty compile the templates? Every time or after a certain amount of time?


Smarty checks if a template needs (re)compiled on every iteration, unless you disable compile_check. It compares the timestamp of the template to the timestamp of the compiled file.

kk wrote:
When would I use caching if Smarty caches the compiled template anyway?


A cache file is a text file of the output of the call to display(). The compiled templates are PHP files created from each template file.

kk wrote:
Against what does smarty check the compiled status and the cached status? The timestamp written in my resource handler seem to be without any effect.


Timestamps. You may want to use the lastest SVN, there may have been some bug fixes regarding this. Otherwise it should use timestamps from your resource handler just fine. Be sure your resource handler hands back unix timestamps, not mysql date/times.

kk wrote:
Where does Smarty save the cached files?


Cache files are stored in the $cache_dir, compiled files are stored in the $compile_dir. With the way Smarty works internally you may see a mix of php/text files in those directories, but that is the separation.

kk wrote:
What is the difference between a compiled file and a cached file apart the name particular when own resource handler is used? The files are identical on my system. Only the native resource handler writes files into the cache directory.

As an example:
Initialize smarty:
$smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;
$smarty->setCacheLifetime(60);

I use my own resource handler:
class Smarty_Resource_Module extends Smarty_Resource_Custom {…}

which pulls out the correct templates and produces the correct output APART from the correct timestamp as it seems.

My own debug show following output:
DB stamp used mcbModifiedTS - 1317080020 called for: homepagemaincontent compare to: 1317317487 difference: 237467

Explained -> A database module(homepagemaincontent) is delivered with mcbModifiedTS (timestamp of last change in format of MySQL timedate field) of 1317080020. This is compared to the current time (1317317487) ((time()) which is a difference of 237467 which is more than 60 seconds BUT a recompile won’t happening.
Same when compared against a file system date and time.


The cache should refresh, but the template should not recompile if it has not changed since the last compile.

kk wrote:
And why does Smarty call function fetchTimestamp twice?


Uwe will be better to answer that.

kk wrote:
This is the same behaviour when used without caching at all.

This seems all odd to me.
As far as I understand the compiled template should be recompiled if the source has changed, hence the timestamp check.
And the cached file will never be replaced apart if explicitly requested and the replacement is a recompiled template.


That is exactly how it should work, correct. Turn off caching and get your resource working with just the compiling part. Once that works, enable caching. A template may not recompile with caching enabled, depending on how you have it setup. There is a new feature in 3.1 to allow the check of a template recompile only upon a cache miss. That is handy if you update a template but don't care to clear all the cache files out, just let it update when the cache expires.
Back to top
View user's profile Send private message Visit poster's website
kk
Smarty Rookie


Joined: 10 Aug 2010
Posts: 22

PostPosted: Thu Sep 29, 2011 8:45 pm    Post subject: Reply with quote

Thanks for quick reply.

1. Yes it was a typo.
2. That’s make sense
3. In my case cacheDir contains only the ‘root’ template and none of the additional modules which are requested by {include file=”module:…’}. Shouldn’t the other resources which are not excluded by {nocache} or {include … nocache} be in cachDir as well? But this is not my problem.
4. In compileDir are cached files i.e. xxx.cache.php. Are they not cached files?
5. They are converted by strtotime() function. That couldn’t be the problem.

I still don’t know how I can trigger a recompile within the class Smarty_Resource_Module extends Smarty_Resource_Custom with the timestamp?

And why do I have an additional function fetchTimestamp() when I can and do manipulate the timestamp in fetch(), 3rd parameter, already?
Is this not a potential overhead to run through some code I already used earlier or has to send additional requests to a database or similar?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Sep 29, 2011 11:17 pm    Post subject: Reply with quote

To 3.
In cacheDir you have only a file with the name of the root template name. It does contain the HTML output of the complete page. Any included subtemplates content is merged into this file. So what you see is correct.

To 4.
compileDir does hold the compiled not yet rendered template files. Afterr you have called your page once with caching enable and once with caching disabled it's correct that you see two files.

To 5.
What is your problem with the timestamps? Your values show that the mysql timestamp is lower/older so no recompile should occur.

I have to investigate why fetchTimestamp gets called twice. I have not yet any answer for this.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Sep 29, 2011 11:25 pm    Post subject: Reply with quote

Smarty 3.1.1 does have a bug on compile check of custom resources.
This could have caused that fetchTimestamp is called twice.

Please use the SVN trunk version and check again.
Back to top
View user's profile Send private message
kk
Smarty Rookie


Joined: 10 Aug 2010
Posts: 22

PostPosted: Fri Sep 30, 2011 7:07 am    Post subject: Reply with quote

I solved the timestamp problem. Thanks for the help on that.
And thanks for explaining the cache - compile relationship.

But, the double call still happens.

Situation: Clear all cached files and compiled files. First call a single execution of fetchTimestamp()

Refresh with out deleting anything: fetchTimestamp() is called twice.

I'm using revision 4341.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Sep 30, 2011 5:09 pm    Post subject: Reply with quote

Karl fetchTimestamp() isn't called twice.

I think you did test it with some debug output with echo or var_dump.

What happens is when you compile the first time the debug output is displayed. But any output is also stored in the cache file.

When you call the page a second time you get the new debug output from the compile cecking, but after that the cached page gets displayed and you see the old output generated at compile time again.
Back to top
View user's profile Send private message
kk
Smarty Rookie


Joined: 10 Aug 2010
Posts: 22

PostPosted: Fri Sep 30, 2011 5:58 pm    Post subject: Reply with quote

That makes sense. Indeed I used echo to get the values displayed.

One more:
What purpose does the 3rd parameter of the method fetch() have? Does this need to be filled with a real timestamp value? The timestamp value will be provided by method fetchTimestamp() during runtime I assume.

Thanks again.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Sep 30, 2011 7:00 pm    Post subject: Reply with quote

You may have custom resources where the storage delivers always content and time stamp together or where obtaining both values from storage will not take longer as obtaining just one value.

In this case you can remove fetchTimestamp() from your resource and fill also the third parameter of fetch with the time stamp. This will save one access of storage in case the template must be recompiled.

if fetchTimestamp() is implemented the 3rd parameter of fetch is not used.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP