Skip to content

Commit

Permalink
cache_get_or_set function
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Dec 14, 2023
1 parent a5c6f13 commit e70a668
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 92 deletions.
32 changes: 16 additions & 16 deletions core/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions core/imageboard/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions core/imageboard/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions core/polyfills.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,3 +814,18 @@ function stringer($s): string
}
return "<Unstringable>";
}

/**
* 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;
}
14 changes: 4 additions & 10 deletions ext/autocomplete/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}
6 changes: 1 addition & 5 deletions ext/blocks/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
21 changes: 7 additions & 14 deletions ext/comment/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("
Expand Down Expand Up @@ -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);
}
Expand Down
19 changes: 11 additions & 8 deletions ext/featured/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
10 changes: 5 additions & 5 deletions ext/index/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

Expand Down
23 changes: 11 additions & 12 deletions ext/pm/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
14 changes: 6 additions & 8 deletions ext/report_image/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
1 change: 0 additions & 1 deletion ext/tag_list/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e70a668

Please sign in to comment.