From b693779d0d5abba5caeabaddfc124540382a09d1 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Wed, 23 Jan 2013 12:43:46 +1100 Subject: [PATCH 1/3] Hopefully a final fix for caching thread previews. This feature was previous disabled because of a bug/confliction. --- inc/display.php | 9 ------- inc/functions.php | 70 +++++++++++++++++++++++++++++++------------------------ 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/inc/display.php b/inc/display.php index 39cb86c7..3dd57726 100644 --- a/inc/display.php +++ b/inc/display.php @@ -435,16 +435,7 @@ class Thread { $built = Element('post_thread.html', array('config' => $config, 'board' => $board, 'post' => &$this, 'index' => $index)); - if (!$this->mod && $index && $config['cache']['enabled']) { - cache::set($this->cache_key($index), $built); - } - return $built; } - function cache_key($index) { - global $board; - - return 'thread_' . ($index ? 'index_' : '') . $board['uri'] . '_' . $this->id; - } }; diff --git a/inc/functions.php b/inc/functions.php index f818f7b7..05ddc998 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -974,44 +974,51 @@ function index($page, $mod=false) { $th['sticky'], $th['locked'], $th['sage'], $th['embed'], $mod ? '?/' : $config['root'], $mod ); - // if (!$mod && $config['cache']['enabled'] && $cached_stuff = cache::get("thread_index_{$board['uri']}_{$th['id']}")) { - // $post_count = $cached_stuff[0]; - // $thread->posts = json_decode($cached_stuff[1]); - //} else { + if ($config['cache']['enabled'] && $replies = cache::get("thread_index_{$board['uri']}_{$th['id']}")) { + $replies = json_decode($replies, true); + } else { $posts = prepare(sprintf("SELECT * FROM `posts_%s` WHERE `thread` = :id ORDER BY `id` DESC LIMIT :limit", $board['uri'])); $posts->bindValue(':id', $th['id']); $posts->bindValue(':limit', ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']), PDO::PARAM_INT); $posts->execute() or error(db_error($posts)); + + $replies = $posts->fetchAll(PDO::FETCH_ASSOC); + + if ($config['cache']['enabled']) + cache::set("thread_index_{$board['uri']}_{$th['id']}", json_encode($replies)); + } + + $num_images = 0; + foreach ($replies as $po) { + if ($po['file']) + $num_images++; - $num_images = 0; - while ($po = $posts->fetch()) { - if ($po['file']) - $num_images++; - - $thread->add(new Post( - $po['id'], $th['id'], $po['subject'], $po['email'], $po['name'], $po['trip'], $po['capcode'], $po['body'], $po['time'], - $po['thumb'], $po['thumbwidth'], $po['thumbheight'], $po['file'], $po['filewidth'], $po['fileheight'], $po['filesize'], - $po['filename'], $po['ip'], $po['embed'], $mod ? '?/' : $config['root'], $mod) - ); - } - - $post_count = $posts->rowCount(); - //} - - if ($post_count == ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview'])) { - $count = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM `posts_%s` WHERE `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM `posts_%s` WHERE `file` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri'])); - $count->bindValue(':thread', $th['id'], PDO::PARAM_INT); - $count->execute() or error(db_error($count)); - - $c = $count->fetch(); - $thread->omitted = $c['num'] - ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']); - - $c = $count->fetch(); - $thread->omitted_images = $c['num'] - $num_images; + $thread->add(new Post( + $po['id'], $th['id'], $po['subject'], $po['email'], $po['name'], $po['trip'], $po['capcode'], $po['body'], $po['time'], + $po['thumb'], $po['thumbwidth'], $po['thumbheight'], $po['file'], $po['filewidth'], $po['fileheight'], $po['filesize'], + $po['filename'], $po['ip'], $po['embed'], $mod ? '?/' : $config['root'], $mod) + ); } - // if ($config['cache']['enabled']) - // cache::set("thread_index_{$board['uri']}_{$th['id']}", json_encode(array($posts->rowCount(), $thread->posts))); + $post_count = count($replies); // $posts->rowCount() + + if ($post_count == ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview'])) { + // Yeah, using two cache objects for one thread seems a little inefficient. This code is messy and dumb; please clean it up if you can. + if ($config['cache']['enabled'] && $count = cache::get("thread_index_{$board['uri']}_{$th['id']}_omitted")) { + $count = explode(',', $count); + } else { + $count = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM `posts_%s` WHERE `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM `posts_%s` WHERE `file` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri'])); + $count->bindValue(':thread', $th['id'], PDO::PARAM_INT); + $count->execute() or error(db_error($count)); + $count = $count->fetchAll(PDO::FETCH_COLUMN); + + if ($config['cache']['enabled']) + cache::set("thread_index_{$board['uri']}_{$th['id']}_omitted", implode(',', $count)); + } + + $thread->omitted = $count[0] - ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']); + $thread->omitted_images = $count[1] - $num_images; + } $thread->posts = array_reverse($thread->posts); @@ -1503,6 +1510,7 @@ function buildThread($id, $return=false, $mod=false) { if ($config['cache']['enabled'] && !$mod) { // Clear cache cache::delete("thread_index_{$board['uri']}_{$id}"); + cache::delete("thread_index_{$board['uri']}_{$th['id']}_omitted"); cache::delete("thread_{$board['uri']}_{$id}"); } From edb9999a7d74582f989f0372e04678b4fc7cb8dd Mon Sep 17 00:00:00 2001 From: Michael Save Date: Wed, 23 Jan 2013 12:48:23 +1100 Subject: [PATCH 2/3] I'm dumb --- inc/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/functions.php b/inc/functions.php index 05ddc998..90e0bf16 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -1510,7 +1510,7 @@ function buildThread($id, $return=false, $mod=false) { if ($config['cache']['enabled'] && !$mod) { // Clear cache cache::delete("thread_index_{$board['uri']}_{$id}"); - cache::delete("thread_index_{$board['uri']}_{$th['id']}_omitted"); + cache::delete("thread_index_{$board['uri']}_{$id}_omitted"); cache::delete("thread_{$board['uri']}_{$id}"); } From cd81ae8ea586bd3a4a05b0070dcb496fa2c8bd71 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Wed, 23 Jan 2013 13:42:14 +1100 Subject: [PATCH 3/3] No point using JSON as Cache class already supports objects --- inc/functions.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 90e0bf16..2ceb8a5d 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -974,9 +974,7 @@ function index($page, $mod=false) { $th['sticky'], $th['locked'], $th['sage'], $th['embed'], $mod ? '?/' : $config['root'], $mod ); - if ($config['cache']['enabled'] && $replies = cache::get("thread_index_{$board['uri']}_{$th['id']}")) { - $replies = json_decode($replies, true); - } else { + if (!$config['cache']['enabled'] || !$replies = cache::get("thread_index_{$board['uri']}_{$th['id']}")) { $posts = prepare(sprintf("SELECT * FROM `posts_%s` WHERE `thread` = :id ORDER BY `id` DESC LIMIT :limit", $board['uri'])); $posts->bindValue(':id', $th['id']); $posts->bindValue(':limit', ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']), PDO::PARAM_INT); @@ -985,9 +983,9 @@ function index($page, $mod=false) { $replies = $posts->fetchAll(PDO::FETCH_ASSOC); if ($config['cache']['enabled']) - cache::set("thread_index_{$board['uri']}_{$th['id']}", json_encode($replies)); + cache::set("thread_index_{$board['uri']}_{$th['id']}", $replies); } - + $num_images = 0; foreach ($replies as $po) { if ($po['file']) @@ -1004,16 +1002,14 @@ function index($page, $mod=false) { if ($post_count == ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview'])) { // Yeah, using two cache objects for one thread seems a little inefficient. This code is messy and dumb; please clean it up if you can. - if ($config['cache']['enabled'] && $count = cache::get("thread_index_{$board['uri']}_{$th['id']}_omitted")) { - $count = explode(',', $count); - } else { + if (!$config['cache']['enabled'] || !$count = cache::get("thread_index_{$board['uri']}_{$th['id']}_omitted")) { $count = prepare(sprintf("SELECT COUNT(`id`) as `num` FROM `posts_%s` WHERE `thread` = :thread UNION ALL SELECT COUNT(`id`) FROM `posts_%s` WHERE `file` IS NOT NULL AND `thread` = :thread", $board['uri'], $board['uri'])); $count->bindValue(':thread', $th['id'], PDO::PARAM_INT); $count->execute() or error(db_error($count)); $count = $count->fetchAll(PDO::FETCH_COLUMN); if ($config['cache']['enabled']) - cache::set("thread_index_{$board['uri']}_{$th['id']}_omitted", implode(',', $count)); + cache::set("thread_index_{$board['uri']}_{$th['id']}_omitted", $count); } $thread->omitted = $count[0] - ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']);