cache_get_or_set function
This commit is contained in:
parent
672ef4ac53
commit
7f98412d8b
12 changed files with 85 additions and 93 deletions
|
@ -278,27 +278,27 @@ class DatabaseConfig extends BaseConfig
|
|||
$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;
|
||||
}
|
||||
|
||||
foreach ($this->database->get_all($query, $args) as $row) {
|
||||
$this->values[$row["name"]] = $row["value"];
|
||||
}
|
||||
$cache->set($this->cache_name, $this->values);
|
||||
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) {
|
||||
$values[$row["name"]] = $row["value"];
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
public function save(string $name = null): void
|
||||
|
|
|
@ -119,13 +119,8 @@ class Search
|
|||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -55,9 +55,9 @@ class TagUsage
|
|||
$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 @@ class TagUsage
|
|||
$limitSQL
|
||||
",
|
||||
$SQLarr
|
||||
);
|
||||
$cache->set($cache_key, $res, 600);
|
||||
}
|
||||
),
|
||||
600
|
||||
);
|
||||
|
||||
$counts = [];
|
||||
foreach ($res as $k => $v) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -62,10 +62,8 @@ class AutoComplete extends Extension
|
|||
$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 @@ class AutoComplete extends Extension
|
|||
ORDER BY count DESC
|
||||
$limitSQL
|
||||
",
|
||||
$SQLarr
|
||||
);
|
||||
$cache->set($cache_key, $res, 600);
|
||||
}
|
||||
|
||||
return $res;
|
||||
$SQLarr
|
||||
), 600);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,11 +53,7 @@ class Blocks extends Extension
|
|||
{
|
||||
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)) {
|
||||
|
|
|
@ -283,20 +283,17 @@ class CommentList extends Extension
|
|||
{
|
||||
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 @@ class CommentList extends Extension
|
|||
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);
|
||||
}
|
||||
|
|
|
@ -52,14 +52,17 @@ class Featured extends Extension
|
|||
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))) {
|
||||
|
|
|
@ -77,11 +77,11 @@ class Index extends Extension
|
|||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -297,18 +297,17 @@ class PrivMsg extends Extension
|
|||
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -240,14 +240,12 @@ class ReportImage extends Extension
|
|||
|
||||
public function count_reported_images(): int
|
||||
{
|
||||
global $cache, $database;
|
||||
global $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);
|
||||
}
|
||||
|
||||
return (int)$count;
|
||||
return (int)cache_get_or_set(
|
||||
"image-report-count",
|
||||
fn () => $database->get_one("SELECT count(*) FROM image_reports"),
|
||||
600
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -553,7 +553,6 @@ class TagList extends Extension
|
|||
{
|
||||
global $cache, $database;
|
||||
|
||||
|
||||
$wild_tags = $search;
|
||||
$cache_key = "related_tags:" . md5(Tag::implode($search));
|
||||
$related_tags = $cache->get($cache_key);
|
||||
|
|
Reference in a new issue