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

Falling back to cache on error

 
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
jtm
Smarty Rookie


Joined: 13 Nov 2003
Posts: 9

PostPosted: Thu Nov 13, 2003 1:32 pm    Post subject: Falling back to cache on error Reply with quote

I'm getting some data from a database which I then insert to a template.
I'm also using $smarty->caching = 1; so the page is cached and everything works fine.

But, when the cache expires, the php connects to the database but what if the database is down or there is some other error which prevents getting the critical data which needs to be inserted to the template? Is there any way to force smarty to use the old cache even if it's expired?

Think about the following pseudo code:

Code:

$smarty->caching = 1;

if (! $smarty->is_cached(...)) {
  // The cache is expired, get some data from a database, assing
  // template variables etc so that the page is regenerated

  ...
  if ( database is down) {
    force smarty to use the old cache file
  }

}
$smarty->display("template.tpl");


Thanks in advice! =)

- jtm
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Nov 13, 2003 3:09 pm    Post subject: Re: Falling back to cache on error Reply with quote

Code:

$smarty->caching = 1;

if (! $smarty->is_cached(...)) {
  // The cache is expired, get some data from a database, assing
  // template variables etc so that the page is regenerated

  ...
  if ( database is down) {
    $smarty->cache_lifetime = -1;
    if ($smarty->is_cached(...)) {
       // there is an old cached version
    } else {
       // fatal error
    }
  }

}
$smarty->display("template.tpl");
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jtm
Smarty Rookie


Joined: 13 Nov 2003
Posts: 9

PostPosted: Thu Nov 13, 2003 5:41 pm    Post subject: Reply with quote

That looked great but I've come cross to another problem.

As seen in thread http://www.phpinsider.com/smarty-forum/viewtopic.php?t=1343&start=0, there's a block function which did a bit the same.

I figured that I'd need something like a {cache} ... {/cache} block function which would also check if code inside would set some error template variable to true which would force the cache to use the old file.

The code which boots posted there did nearly that. The only drawback is that I don't need subtemplates here, I just need to cache the data inside those {cache} tags. It would look dumb if I just create a template which have nothing but one $insert_data_here variable. How can I create cache files by myself in a block function so that I can also use is_cached etc functions to check if they still are valid etc?
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Nov 13, 2003 5:52 pm    Post subject: Reply with quote

hi jtm.

First off, messju's solution above is very nice Smile

On to your last comments: I'm not sure if the plugin I proposed does exactly what you say. Just to be clear, though, it does not require you to create subtemplates--the plugin literally creates the subtemplate based on the block.

So if your template was index.tpl and you had the following block in that template:
Code:
{sec template=$smarty.template id=test group="abc" ttl=300}
blah
{/sec}

the plugin will automagically write a new template into your template directory called index.tpl.test. It then includes the subtemplate which "compiles" it (which is superfluous in this case, IMO) and caches it.

Note that once the subtemplate is created, nothing prevents you from using it elsewhere using a normal inlclude. Of course, the outcomes will be less predictible so I'm not sure that it is a wonderful idea, but it has its interesting qualities and if managed well, can be quite useful, I think.

You may be able to use that plugin as a starting point for your needs. If you do such a thing, please post your results Smile

At this point, I have no real intention to support it and there are several caveats (some mentioned in the thread you linked). For example, the resource api does not support writing templates so don't bother using this plugin for resource based templates.
Back to top
View user's profile Send private message
jtm
Smarty Rookie


Joined: 13 Nov 2003
Posts: 9

PostPosted: Mon Nov 17, 2003 9:51 am    Post subject: Reply with quote

I tried the code but I'm not quite satisfacted with the results.

It seems that smarty can't search template files from multiple source directories. I have one directory where my templates resists (which I have manually wrote) and I don't want templates, which are created by this {sec} block directove, to be created in the same directory.

I can easily modify the sec block handler to create the template files to a different directory but I can't make smarty look them from there. Any suggestions?

I also think that it could work better if the sec block directive could just store the data inside the block to a cache file, without using templates in the middle. Is this possible? can a plugin create cache files on it's own so that they can be used with is_cached etc?

EDIT: After a short discussion with messju in the IRC, I think that I got the right way to do this. I'll post my results later =)

- jtm
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon Nov 17, 2003 3:16 pm    Post subject: Reply with quote

jtm-- heh, yeah, it is definately not a fully developed solution. If you have improvements, I'd appreciate you posting them Smile You may also consider posting them into the original thread if you think they are appropriate for others trying to solve atu's original query.

Cheers!
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