diff --git a/core/config.php b/core/config.php index 85c3a2a1eb..341ed09a04 100644 --- a/core/config.php +++ b/core/config.php @@ -278,27 +278,27 @@ public function __construct( $this->table_name = $table_name; $this->sub_value = $sub_value; $this->sub_column = $sub_column; - $this->cache_name = empty($sub_value) ? "config" : "config_{$sub_value}"; + $this->cache_name = empty($sub_value) ? "config" : "config_{$sub_column}={$sub_value}"; + $this->values = cache_get_or_set($this->cache_name, fn () => $this->get_values()); + } - $cached = $cache->get($this->cache_name); - if (!is_null($cached)) { - $this->values = $cached; - } else { - $this->values = []; + private function get_values(): mixed + { + $values = []; - $query = "SELECT name, value FROM {$this->table_name}"; - $args = []; + $query = "SELECT name, value FROM {$this->table_name}"; + $args = []; - if (!empty($sub_column) && !empty($sub_value)) { - $query .= " WHERE $sub_column = :sub_value"; - $args["sub_value"] = $sub_value; - } + if (!empty($this->sub_column) && !empty($this->sub_value)) { + $query .= " WHERE {$this->sub_column} = :sub_value"; + $args["sub_value"] = $this->sub_value; + } - foreach ($this->database->get_all($query, $args) as $row) { - $this->values[$row["name"]] = $row["value"]; - } - $cache->set($this->cache_name, $this->values); + foreach ($this->database->get_all($query, $args) as $row) { + $values[$row["name"]] = $row["value"]; } + + return $values; } public function save(string $name = null): void diff --git a/core/imageboard/search.php b/core/imageboard/search.php index d6af5815eb..2b9dfff577 100644 --- a/core/imageboard/search.php +++ b/core/imageboard/search.php @@ -119,13 +119,8 @@ public static function count_tag(string $tag): int private static function count_total_images(): int { - global $cache, $database; - $total = $cache->get("image-count"); - if (is_null($total)) { - $total = (int)$database->get_one("SELECT COUNT(*) FROM images"); - $cache->set("image-count", $total, 600); - } - return $total; + global $database; + return cache_get_or_set("image-count", fn () => (int)$database->get_one("SELECT COUNT(*) FROM images"), 600); } /** diff --git a/core/imageboard/tag.php b/core/imageboard/tag.php index 547ed0f040..46bfc07071 100644 --- a/core/imageboard/tag.php +++ b/core/imageboard/tag.php @@ -55,9 +55,9 @@ public static function tags(string $search, int $limit = 10): array $cache_key .= "-" . $limit; } - $res = $cache->get($cache_key); - if (is_null($res)) { - $res = $database->get_pairs( + $res = cache_get_or_set( + $cache_key, + fn () => $database->get_pairs( " SELECT tag, count FROM tags @@ -68,9 +68,9 @@ public static function tags(string $search, int $limit = 10): array $limitSQL ", $SQLarr - ); - $cache->set($cache_key, $res, 600); - } + ), + 600 + ); $counts = []; foreach ($res as $k => $v) { diff --git a/core/polyfills.php b/core/polyfills.php index bcecda02b8..6609945d8b 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -814,3 +814,18 @@ function stringer($s): string } return ""; } + +/** + * If a value is in the cache, return it; otherwise, call the callback + * to generate it and store it in the cache. + */ +function cache_get_or_set(string $key, callable $callback, int $ttl = 0) +{ + global $cache; + $value = $cache->get($key); + if ($value === null) { + $value = $callback(); + $cache->set($key, $value, $ttl); + } + return $value; +} diff --git a/ext/autocomplete/main.php b/ext/autocomplete/main.php index a350c00d96..147260e3d0 100644 --- a/ext/autocomplete/main.php +++ b/ext/autocomplete/main.php @@ -62,10 +62,8 @@ private function complete(string $search, int $limit): array $cache_key .= "-" . $limit; } - $res = $cache->get($cache_key); - if (is_null($res)) { - $res = $database->get_pairs( - " + return cache_get_or_set($cache_key, fn () => $database->get_pairs( + " SELECT tag, count FROM tags WHERE LOWER(tag) LIKE LOWER(:search) @@ -74,11 +72,7 @@ private function complete(string $search, int $limit): array ORDER BY count DESC $limitSQL ", - $SQLarr - ); - $cache->set($cache_key, $res, 600); - } - - return $res; + $SQLarr + ), 600); } } diff --git a/ext/blocks/main.php b/ext/blocks/main.php index c7bcb2b969..2711fd9aba 100644 --- a/ext/blocks/main.php +++ b/ext/blocks/main.php @@ -53,11 +53,7 @@ public function onPageRequest(PageRequestEvent $event) { global $cache, $database, $page, $user; - $blocks = $cache->get("blocks"); - if (is_null($blocks)) { - $blocks = $database->get_all("SELECT * FROM blocks"); - $cache->set("blocks", $blocks, 600); - } + $blocks = cache_get_or_set("blocks", fn () => $database->get_all("SELECT * FROM blocks"), 600); foreach ($blocks as $block) { $path = implode("/", $event->args); if (strlen($path) < 4000 && fnmatch($block['pages'], $path)) { diff --git a/ext/comment/main.php b/ext/comment/main.php index a16d8d6435..8b5c4874ed 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -283,20 +283,17 @@ private function onPageRequest_list(PageRequestEvent $event) { global $cache, $config, $database, $user; + $threads_per_page = 10; + $where = SPEED_HAX ? "WHERE posted > now() - interval '24 hours'" : ""; - $total_pages = $cache->get("comment_pages"); - if (is_null($total_pages)) { - $total_pages = (int)ceil($database->get_one(" - SELECT COUNT(c1) - FROM (SELECT COUNT(image_id) AS c1 FROM comments $where GROUP BY image_id) AS s1 - ") / 10); - $cache->set("comment_pages", $total_pages, 600); - } + $total_pages = cache_get_or_set("comment_pages", fn () => (int)ceil($database->get_one(" + SELECT COUNT(c1) + FROM (SELECT COUNT(image_id) AS c1 FROM comments $where GROUP BY image_id) AS s1 + ") / $threads_per_page), 600); $total_pages = max($total_pages, 1); $current_page = $event->try_page_num(1, $total_pages); - $threads_per_page = 10; $start = $threads_per_page * $current_page; $result = $database->execute(" @@ -357,11 +354,7 @@ public function onPostListBuilding(PostListBuildingEvent $event) global $cache, $config; $cc = $config->get_int("comment_count"); if ($cc > 0) { - $recent = $cache->get("recent_comments"); - if (is_null($recent)) { - $recent = $this->get_recent_comments($cc); - $cache->set("recent_comments", $recent, 60); - } + $recent = cache_get_or_set("recent_comments", fn () => $this->get_recent_comments($cc), 60); if (count($recent) > 0) { $this->theme->display_recent_comments($recent); } diff --git a/ext/featured/main.php b/ext/featured/main.php index 11cba6ecb0..de82bde3f4 100644 --- a/ext/featured/main.php +++ b/ext/featured/main.php @@ -52,14 +52,17 @@ public function onPostListBuilding(PostListBuildingEvent $event) global $cache, $config, $page, $user; $fid = $config->get_int("featured_id"); if ($fid > 0) { - $image = $cache->get("featured_image_object:$fid"); - if (is_null($image)) { - $image = Image::by_id($fid); - if ($image) { // make sure the object is fully populated before saving - $image->get_tag_array(); - } - $cache->set("featured_image_object:$fid", $image, 600); - } + $image = cache_get_or_set( + "featured_image_object:$fid", + function () use ($fid) { + $image = Image::by_id($fid); + if ($image) { // make sure the object is fully populated before saving + $image->get_tag_array(); + } + return $image; + }, + 600 + ); if (!is_null($image)) { if (Extension::is_enabled(RatingsInfo::KEY)) { if (!in_array($image->rating, Ratings::get_user_class_privs($user))) { diff --git a/ext/index/main.php b/ext/index/main.php index ea776f227c..9bfd6bfe9e 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -77,11 +77,11 @@ public function onPageRequest(PageRequestEvent $event) if (SPEED_HAX) { if ($count_search_terms === 0 && ($page_number < 10)) { // extra caching for the first few post/list pages - $images = $cache->get("post-list:$page_number"); - if (is_null($images)) { - $images = Search::find_images(($page_number - 1) * $page_size, $page_size, $search_terms); - $cache->set("post-list:$page_number", $images, 60); - } + $images = cache_get_or_set( + "post-list:$page_number", + fn () => Search::find_images(($page_number - 1) * $page_size, $page_size, $search_terms), + 60 + ); } } diff --git a/ext/pm/main.php b/ext/pm/main.php index 5600a2d94b..6f56761bcd 100644 --- a/ext/pm/main.php +++ b/ext/pm/main.php @@ -297,18 +297,17 @@ public function onSendPM(SendPMEvent $event) private function count_pms(User $user) { - global $cache, $database; + global $database; - $count = $cache->get("pm-count:{$user->id}"); - if (is_null($count)) { - $count = $database->get_one(" - SELECT count(*) - FROM private_message - WHERE to_id = :to_id - AND is_read = :is_read - ", ["to_id" => $user->id, "is_read" => false]); - $cache->set("pm-count:{$user->id}", $count, 600); - } - return $count; + return cache_get_or_set( + "pm-count:{$user->id}", + fn () => $database->get_one(" + SELECT count(*) + FROM private_message + WHERE to_id = :to_id + AND is_read = :is_read + ", ["to_id" => $user->id, "is_read" => false]), + 600 + ); } } diff --git a/ext/report_image/main.php b/ext/report_image/main.php index 2cc5b4be45..4a348a7f13 100644 --- a/ext/report_image/main.php +++ b/ext/report_image/main.php @@ -240,14 +240,12 @@ public function get_reported_images(): array public function count_reported_images(): int { - global $cache, $database; - - $count = $cache->get("image-report-count"); - if (is_null($count)) { - $count = $database->get_one("SELECT count(*) FROM image_reports"); - $cache->set("image-report-count", $count, 600); - } + global $database; - return (int)$count; + return (int)cache_get_or_set( + "image-report-count", + fn () => $database->get_one("SELECT count(*) FROM image_reports"), + 600 + ); } } diff --git a/ext/tag_list/main.php b/ext/tag_list/main.php index 3d13c7ae93..89d451389b 100644 --- a/ext/tag_list/main.php +++ b/ext/tag_list/main.php @@ -553,7 +553,6 @@ public static function get_related_tags(array $search, int $limit): array { global $cache, $database; - $wild_tags = $search; $cache_key = "related_tags:" . md5(Tag::implode($search)); $related_tags = $cache->get($cache_key);