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

Force cache

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
Killua
Smarty n00b


Joined: 29 Dec 2005
Posts: 3

PostPosted: Thu Dec 29, 2005 10:53 am    Post subject: Force cache Reply with quote

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
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Thu Dec 29, 2005 12:30 pm    Post subject: Reply with quote

Hi,

why dont use a resource and never let a template expire?

http://smarty.php.net/manual/en/api.register.resource.php

Bye,
Markus
Back to top
View user's profile Send private message
Killua
Smarty n00b


Joined: 29 Dec 2005
Posts: 3

PostPosted: Fri Dec 30, 2005 12:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Dec 30, 2005 4:47 pm    Post subject: Reply with quote

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 Wink

As to your solution, it seems okay on the surface. I wonder what messju and monte think about this...
Back to top
View user's profile Send private message
oZZ
Smarty n00b


Joined: 15 Mar 2006
Posts: 3

PostPosted: Wed Mar 15, 2006 8:54 pm    Post subject: Reply with quote

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 Very Happy
Back to top
View user's profile Send private message
oZZ
Smarty n00b


Joined: 15 Mar 2006
Posts: 3

PostPosted: Thu Dec 07, 2006 8:57 pm    Post subject: Reply with quote

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
View user's profile Send private message
Killua
Smarty n00b


Joined: 29 Dec 2005
Posts: 3

PostPosted: Thu May 15, 2008 12:40 pm    Post subject: Reply with quote

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
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 -> Tips and Tricks 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