RE: [SMARTY] using expired cached files
| From: | Marshall Sorenson | Date: | Tue, 16 Sep 2003 03:02:39 +0000 |
| Subject: | RE: [SMARTY] using expired cached files | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-10218@lists.php.net to get a copy of this message | ||
Messju,
Thanks for the idea, it pointed me well in the right direction. I have
implemented this with some small changes to make it easier to work with. I
can't help but thinking there should be built in support for something like
this in Smarty?
Here is the class extension... I would appreciate you taking a peek and
letting me know what you think.
Basically instead of waiting for an error and then trying to put the brakes
on the caching, I implemented my own expire checking. So what I do is check
if everything is OK to cache, then use $force_compile to overwrite the old
cache file.
// the class
class SmartyX extends Smarty {
/**
* better cache control
*/
function cache_control ($tpl, $params='') {
// $params =
// array ( 'cache_id' => [string],
// 'compile_id'=> [string],
// 'lifetime' => [integer],
// 'write' => [boolean] );
// cache files probably won't expire, unless we delete them.
// tried setting this to -1, but got strange results.
$this->cache_lifetime = 9999999;
// check if cache file exists
if (! $this->is_cached($tpl, $params['cache_id'])) {
// cache file does not exist at all
return false;
} elseif ($params['write']) {
// force overwrite cache file
$smarty->force_compile = true;
} elseif ($params['lifetime']) {
// is cache file older than our custom lifetime?
return ($this->_cache_info['timestamp'] < time() - $params['lifetime'])
?
false : true;
} else {
// cache file exists
return true;
}
}
}
////////////
basic example:
if(! $smarty->cache_control('home.tpl', array('lifetime' => 7200)) ) {
do database query
if no database error {
if (! $smarty->cache_control('home.tpl', array('write' => true)) ) {
die('a friendly fatal error');
} else {
// display() will compile new template
}
} else {
// old cache file still in tact
}
} else {
// old cache file still in tact
}
I have tested this a bit, and seems to work well so far.
TIA
-Marshall
____________________________________
"If at first the idea is not absurd,
then there is no hope for it."
- Albert Einstein
-----Original Message-----
From: messju mohr [mailto:messju@lammfellpuschen.de]
Sent: Friday, September 12, 2003 10:38 AM
To: Marshall Sorenson
Cc: smarty-general@lists.php.net
Subject: Re: [SMARTY] using expired cached files
On Thu, Sep 11, 2003 at 02:57:21PM -0400, Marshall Sorenson wrote:
> Hello,
>
> Does anyone know of a way to tell Smarty to use an expired cache file if
an
> error occured while trying to generate an updated cache file?
>
> For instance:
>
> if template not cached {
>
> do database query here
>
> if database query failed {
>
> smarty should use the recently expired cache file
>
> } else {
>
> process query results and assign to template
>
> }
>
> }
>
> $smarty->display('index.tpl');
you can try to modify $smarty->cache_lifetime before your call to
is_cached(), maybe sth like:
$smarty->caching = 1;
$smarty->cache_lifetime = 3600;
if (!smarty->is_cached()) {
if database query failed {
$cache_lifetime = -1; /* never expire */
# --- optional test
if (!smarty->is_cached()) {
// no cached version fatal error
} else {
// we use the expired cached template below
}
# --- end of optional test
} else {
// okay
}
}
$smarty->display('index.tpl');
HTH
messju
> The way things are now I have to either die() on an error, or check for an
> error and switch caching off. Both are ugly.
>
> -Marshall
> ____________________________________
>
> "If at first the idea is not absurd,
> then there is no hope for it."
> - Albert Einstein
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php