Commit c3ed111c authored by Simon Welsh's avatar Simon Welsh

Use Redis to cache dates instead of recalculating each time

git-svn-id: svn+ssh://lsgn/var/logbot-www@56 5f99df0c-6b0a-442a-b40a-040688e84a9d
parent 5d485bbd
......@@ -51,13 +51,26 @@ final class DB {
function build_dates() {
static $dates = array();
if(!$dates) {
$res = DB::inst()->query('SELECT DISTINCT DATE("Time") FROM "Log" WHERE DATE("Time") > \'2000-01-01\' ORDER BY DATE("Time") DESC');
while($row = pg_fetch_array($res)) {
$time = strtotime($row[0]);
$year = date('Y', $time);
$month = date('F', $time);
$day = date('j', $time);
$dates[$year][$month][$day] = array($day, $row[0]);
$r = new Redis;
$r->connect('localhost');
$r->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
$dates = $r->get('log:dates');
if(!$dates) {
$res = DB::inst()->query('SELECT DISTINCT DATE("Time") FROM "Log" WHERE DATE("Time") > \'2000-01-01\' ORDER BY DATE("Time") DESC');
while($row = pg_fetch_array($res)) {
$time = strtotime($row[0]);
$year = date('Y', $time);
$month = date('F', $time);
$day = date('j', $time);
$dates[$year][$month][$day] = array($day, $row[0]);
}
$ty = date('Y');
$tm = date('F');
$td = date('j');
if(isset($dates[$ty][$tm][$td])) {
$r->set('log:dates', $dates);
$r->expireAt('log:dates', strtotime('midnight tomorrow'));
}
}
}
return $dates;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment