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

include template with cache_lifetime expire at midnight

 
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 -> General
View previous topic :: View next topic  
Author Message
wern0561
Smarty Rookie


Joined: 05 Jan 2009
Posts: 13

PostPosted: Wed Dec 26, 2018 11:13 pm    Post subject: include template with cache_lifetime expire at midnight Reply with quote

Hello,

In my project I have a root template file that contains a lot of dynamic content that I don't want cached and so, by default, I turn caching off. I have this root template including other template files that I do want to cache. Some of these subtemplates deal with time specific information and I need them to update at midnight (based on the server's time). I'm trying to set the cache_lifetime of the include to a value that always evaluates to midnight (now + cache_lifetime = midnight). This isn't working though. What am I missing? Here is my basic setup:

Code:

$smarty= new Smarty();
$smarty->caching = Smarty::CACHING_OFF;
$smarty->assign('expires', strtotime("tomorrow")-time());
$smarty->display('index.tpl');


index.tpl
Code:

{include file='time_sensative_file.tpl' cache_lifetime=$expires cache_id='file_cache'}


I've spent a while looking over the documentation and its not clear to me what the cache_lifetime of the included subtemplate is based on, Smarty::CACHING_LIFETIME_CURRENT or Smarty::CACHING_LIFETIME_SAVED. Or, is this dependant on what the root template is set at? Is there a way to specify that I want the caching to be for the lifetime_saved?

How can I have my root template not get cached and my subtemple cache on it's own cache_lifetime? Thanks in advance.
Back to top
View user's profile Send private message
wern0561
Smarty Rookie


Joined: 05 Jan 2009
Posts: 13

PostPosted: Fri Dec 28, 2018 9:30 pm    Post subject: Reply with quote

After spending way too much time digging through the source code, I don't think there is any good way to do what I want. At least not that I am aware of.

Here are my observations.

When setting the cache_lifetime attribute of the include tag, Smarty does use Smarty::CACHING_LIFETIME_CURRENT and there is no good way to change that. This is what has been preventing me from doing what I'm trying to achieve. Smarty checks the time the cache file was created (the last modified date if your using the default file cache) against the currently passed cache_lifetime value. The last modified date is a fixed timestamp and for the cache_lifetime I've been passing in a number that is based on the current time. This just isn't compatible.

Since there is no way for me to easily know the time the file was cached at, and there is no way that I'm aware of to tell smarty to use Smarty::CACHING_LIFETIME_SAVED instead, my last option is to change the source code. This is about the last thing I want to do but it's pretty essential for the project I'm working on. To do this, in Smarty version 3.1.33, I'm just changing line 159 in the file smarty_internal_compile_include.php to:

Code:
$_new_caching = Smarty::CACHING_LIFETIME_SAVED;


This seems to be working perfectly for me. I haven't noticed problems after doing this. I hope it doesn't cause problems. It clearly isn't ideal. This is allowing me to cache certain parts of my page for up to 24 hours and have it always expire at midnight. This is the solution I'm going to use for now while I consider better alternatives. If anyone knows a better solution I'd be glad to hear it.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Jan 01, 2019 4:45 am    Post subject: Re: include template with cache_lifetime expire at midnight Reply with quote

wern0561 wrote:
In my project I have a root template file that contains a lot of dynamic content that I don't want cached and so, by default, I turn caching off.

Here you make a mistake.
All the following is a product of it.
Back to top
View user's profile Send private message
wern0561
Smarty Rookie


Joined: 05 Jan 2009
Posts: 13

PostPosted: Thu Jan 03, 2019 10:19 pm    Post subject: Reply with quote

Quote:
Here you make a mistake.
All the following is a product of it.


Thanks for the reply.

I suppose you are correct. I guess this isn't the way Smarty is intended to be used. I thought this was something that was possible using the include tag right out of the box. I guess i misunderstood.

For the project I'm working on, using includes the way that I want seems much simpler, but i guess i should look at caching from at the root level. Not sure what I'll do yet. Thanks again.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Jan 08, 2019 2:03 pm    Post subject: Reply with quote

You can still use includes, just add "nocache" flag to those you don't want to be cached.
Be aware that such includes impact the rendering time of the page.
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 -> General 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