View previous topic :: View next topic |
Author |
Message |
Killua Smarty n00b
Joined: 29 Dec 2005 Posts: 3
|
Posted: Thu Dec 29, 2005 10:53 am Post subject: Force cache |
|
|
Hello everyone,
I wanted to figure out a way to force the use of the cache even when it expires. The aim is to display the cached pages when the database is down.
The only related feature I found was this one :
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=1399&highlight=force+cache
Still it doesn't solve the expiration problem.
So I simply added a little mod on smarty, and here it is :
In Smarty.class.php, right after the cache variables definition :
Code: |
var $cache_modified_check = false;
+
+ /**
+ * This forces the cache to be used even if it has expired.
+ *
+ * @var boolean
+ */
+ var $force_cache = false;
|
In internals/core.read_cache_file.php, right before the expiration check :
Code: |
$params['results'] = substr($_contents, $_info_start + $_info_len);
+ if ($smarty->force_cache) {
+ return true;
+ }
if ($smarty->caching == 2 && isset ($_cache_info['expires'])){
// caching by expiration time
|
It's CVS style quotation, only the lines beginning with a '+' have to be added (not including the +, of course)
Guess it will help some people.
Though, since it's a patch to Smarty, could this feature be added to Smarty ? |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
|
Back to top |
|
Killua Smarty n00b
Joined: 29 Dec 2005 Posts: 3
|
Posted: Fri Dec 30, 2005 12:48 pm Post subject: |
|
|
Well I may check that but the problem is that I want my templates to expire. But if the DB is down, I want Smarty to use my template, even if expired. And as soon as the DB is up again then the next visits compiles and updates the template, since it is expired.
I'm not sure your solution would help, as far as I see, it only helps having different templates from an other ressource if the DB is down. But I want the same ones, as much up to date as possible. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Dec 30, 2005 4:47 pm Post subject: |
|
|
Killua wrote: | Well I may check that but the problem is that I want my templates to expire. But if the DB is down, I want Smarty to use my template, even if expired. And as soon as the DB is up again then the next visits compiles and updates the template, since it is expired.
I'm not sure your solution would help, as far as I see, it only helps having different templates from an other ressource if the DB is down. But I want the same ones, as much up to date as possible. |
I think I see where you are going with this and I agree that the way Smarty is structured, it is unlikely a resource would help much (although it seems to me that that is where such functionality ought be envisioned). I want to chime in and make a somewhat anal technical comment: templates don't expire per-se. Either their source was changed or not and this is deduced by a timestamp. You can set compile_check to false to make Smarty skip such checking. Surely that is fairly clear but that is all very different than a cached display call which caches the output produced when you run data through a template. Cache images can expire and this is what all of the caching functionality in Smarty deals with. Of course, you seem well versed in this but your post was not precise
As to your solution, it seems okay on the surface. I wonder what messju and monte think about this... |
|
Back to top |
|
oZZ Smarty n00b
Joined: 15 Mar 2006 Posts: 3
|
Posted: Wed Mar 15, 2006 8:54 pm Post subject: |
|
|
I'm quite new to Smarty and up to now like it very much. Unfortunatly I need the kind of functionality descriped by Killua an therefore would appreciate if this feature could be added in future versions of Smarty. Thanks in advance |
|
Back to top |
|
oZZ Smarty n00b
Joined: 15 Mar 2006 Posts: 3
|
Posted: Thu Dec 07, 2006 8:57 pm Post subject: |
|
|
Well perhaps anybody hasn't understand the usage and functinality of Killuas improvement. So I try to explain it with some lines of code.
Code: |
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->caching = true;
if(!$smarty->is_cached('index.tpl')) {
$contents = get_database_contents();
if ( $contents == false ) { // Assume that the get_database_contents() function returns 'false' in case of an unreachable db.
$smarty->force_cache = true; // Force Smarty to use the cached file even when it's already expired
} else {
$smarty->assign($contents);
}
}
$smarty->display('index.tpl');
|
Why is this useful?
Even if the source (database, webservice, rss feed etc.) of the data which you try to display is unavailable you can show a cached version of it to your customers or websites visitors.
Imagine a news page whichs uses different rss feeds as source to aggregate news of different topics. One of the providing servers could be down and instead of showing no news of this rss feed at all you can show an older version of the news (possibly nevertheless the latest) to your websites visitors.
There are many other different cases in which the feature could be useful.
Please consider to put the force_cache feature in one of the future versions of Smarty. |
|
Back to top |
|
Killua Smarty n00b
Joined: 29 Dec 2005 Posts: 3
|
Posted: Thu May 15, 2008 12:40 pm Post subject: |
|
|
I figured I should bump this little trick, as there has never been any feedback after boots comment wondering what the others think about it.
Was it discussed internally, was it analyzed, rejected, whatever ? or just forgotten ?
I still consider it as a nice addition, so I'm curious at how you guys consider it. Since I posted it, I always added and used this trick to every release to work with it :p |
|
Back to top |
|
|