expire cache at given time
| From: | Josh Johnson | Date: | Wed, 04 Sep 2002 15:00:47 +0000 |
| Subject: | expire cache at given time | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-5925@lists.php.net to get a copy of this message | ||
Hi all,
I just sort of hacked together a solution to a problem, and I want to
pose it to the group and see if anyone else has had a similar problem
they might have solved a bit more elegantly, or if there's something in
smarty I missed that would do the same thing.
I've got a page that displays a schedule of events that only has to
change once a day (it's like a snapshot of what's in the database for
today). Since this page might get over 5000 hits in a day, I wanted to
make the strain on the server as small as possible, so I decided to use
caching.
The problem is, I don't want the cache to expire in a given amount of
time, I want it to expire at midnight, when the day ends and the
schedule would display the next day. I also only really want caching to
be used on this one page (lots of dynamic stuff going on elsewhere).
This is the code I came up with, feel free to critique!
# $page is a variable set before this include is called
# $smarty is my smarty instance
if($page == "public_schedule"){
$smarty->caching = 2;
$now = mktime();
$this_day = date("j");
$this_month = date("n");
$expire = mktime(24,00,0,$this_month,$this_day);
$expire_in = $expire - $now;
$smarty->cache_lifetime = $expire_in;
}
$smarty->display("$page.tpl");
This way it only caches the one page, and will set the expiration date
every time the page is loaded, effectively making the cache lifetime
dwindle down to nothing and causing a recache of the content at
midnight.
-- Josh