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->table_name = $table_name;
|
||||||
$this->sub_value = $sub_value;
|
$this->sub_value = $sub_value;
|
||||||
$this->sub_column = $sub_column;
|
$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);
|
private function get_values(): mixed
|
||||||
if (!is_null($cached)) {
|
{
|
||||||
$this->values = $cached;
|
$values = [];
|
||||||
} else {
|
|
||||||
$this->values = [];
|
|
||||||
|
|
||||||
$query = "SELECT name, value FROM {$this->table_name}";
|
$query = "SELECT name, value FROM {$this->table_name}";
|
||||||
$args = [];
|
$args = [];
|
||||||
|
|
||||||
if (!empty($sub_column) && !empty($sub_value)) {
|
if (!empty($this->sub_column) && !empty($this->sub_value)) {
|
||||||
$query .= " WHERE $sub_column = :sub_value";
|
$query .= " WHERE {$this->sub_column} = :sub_value";
|
||||||
$args["sub_value"] = $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
|
public function save(string $name = null): void
|
||||||
|
|
|
@ -119,13 +119,8 @@ class Search
|
||||||
|
|
||||||
private static function count_total_images(): int
|
private static function count_total_images(): int
|
||||||
{
|
{
|
||||||
global $cache, $database;
|
global $database;
|
||||||
$total = $cache->get("image-count");
|
return cache_get_or_set("image-count", fn () => (int)$database->get_one("SELECT COUNT(*) FROM images"), 600);
|
||||||
if (is_null($total)) {
|
|
||||||
$total = (int)$database->get_one("SELECT COUNT(*) FROM images");
|
|
||||||
$cache->set("image-count", $total, 600);
|
|
||||||
}
|
|
||||||
return $total;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -55,9 +55,9 @@ class TagUsage
|
||||||
$cache_key .= "-" . $limit;
|
$cache_key .= "-" . $limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$res = $cache->get($cache_key);
|
$res = cache_get_or_set(
|
||||||
if (is_null($res)) {
|
$cache_key,
|
||||||
$res = $database->get_pairs(
|
fn () => $database->get_pairs(
|
||||||
"
|
"
|
||||||
SELECT tag, count
|
SELECT tag, count
|
||||||
FROM tags
|
FROM tags
|
||||||
|
@ -68,9 +68,9 @@ class TagUsage
|
||||||
$limitSQL
|
$limitSQL
|
||||||
",
|
",
|
||||||
$SQLarr
|
$SQLarr
|
||||||
);
|
),
|
||||||
$cache->set($cache_key, $res, 600);
|
600
|
||||||
}
|
);
|
||||||
|
|
||||||
$counts = [];
|
$counts = [];
|
||||||
foreach ($res as $k => $v) {
|
foreach ($res as $k => $v) {
|
||||||
|
|
|
@ -814,3 +814,18 @@ function stringer($s): string
|
||||||
}
|
}
|
||||||
return "<Unstringable>";
|
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;
|
$cache_key .= "-" . $limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$res = $cache->get($cache_key);
|
return cache_get_or_set($cache_key, fn () => $database->get_pairs(
|
||||||
if (is_null($res)) {
|
"
|
||||||
$res = $database->get_pairs(
|
|
||||||
"
|
|
||||||
SELECT tag, count
|
SELECT tag, count
|
||||||
FROM tags
|
FROM tags
|
||||||
WHERE LOWER(tag) LIKE LOWER(:search)
|
WHERE LOWER(tag) LIKE LOWER(:search)
|
||||||
|
@ -74,11 +72,7 @@ class AutoComplete extends Extension
|
||||||
ORDER BY count DESC
|
ORDER BY count DESC
|
||||||
$limitSQL
|
$limitSQL
|
||||||
",
|
",
|
||||||
$SQLarr
|
$SQLarr
|
||||||
);
|
), 600);
|
||||||
$cache->set($cache_key, $res, 600);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $res;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,11 +53,7 @@ class Blocks extends Extension
|
||||||
{
|
{
|
||||||
global $cache, $database, $page, $user;
|
global $cache, $database, $page, $user;
|
||||||
|
|
||||||
$blocks = $cache->get("blocks");
|
$blocks = cache_get_or_set("blocks", fn () => $database->get_all("SELECT * FROM blocks"), 600);
|
||||||
if (is_null($blocks)) {
|
|
||||||
$blocks = $database->get_all("SELECT * FROM blocks");
|
|
||||||
$cache->set("blocks", $blocks, 600);
|
|
||||||
}
|
|
||||||
foreach ($blocks as $block) {
|
foreach ($blocks as $block) {
|
||||||
$path = implode("/", $event->args);
|
$path = implode("/", $event->args);
|
||||||
if (strlen($path) < 4000 && fnmatch($block['pages'], $path)) {
|
if (strlen($path) < 4000 && fnmatch($block['pages'], $path)) {
|
||||||
|
|
|
@ -283,20 +283,17 @@ class CommentList extends Extension
|
||||||
{
|
{
|
||||||
global $cache, $config, $database, $user;
|
global $cache, $config, $database, $user;
|
||||||
|
|
||||||
|
$threads_per_page = 10;
|
||||||
|
|
||||||
$where = SPEED_HAX ? "WHERE posted > now() - interval '24 hours'" : "";
|
$where = SPEED_HAX ? "WHERE posted > now() - interval '24 hours'" : "";
|
||||||
|
|
||||||
$total_pages = $cache->get("comment_pages");
|
$total_pages = cache_get_or_set("comment_pages", fn () => (int)ceil($database->get_one("
|
||||||
if (is_null($total_pages)) {
|
SELECT COUNT(c1)
|
||||||
$total_pages = (int)ceil($database->get_one("
|
FROM (SELECT COUNT(image_id) AS c1 FROM comments $where GROUP BY image_id) AS s1
|
||||||
SELECT COUNT(c1)
|
") / $threads_per_page), 600);
|
||||||
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 = max($total_pages, 1);
|
$total_pages = max($total_pages, 1);
|
||||||
|
|
||||||
$current_page = $event->try_page_num(1, $total_pages);
|
$current_page = $event->try_page_num(1, $total_pages);
|
||||||
$threads_per_page = 10;
|
|
||||||
$start = $threads_per_page * $current_page;
|
$start = $threads_per_page * $current_page;
|
||||||
|
|
||||||
$result = $database->execute("
|
$result = $database->execute("
|
||||||
|
@ -357,11 +354,7 @@ class CommentList extends Extension
|
||||||
global $cache, $config;
|
global $cache, $config;
|
||||||
$cc = $config->get_int("comment_count");
|
$cc = $config->get_int("comment_count");
|
||||||
if ($cc > 0) {
|
if ($cc > 0) {
|
||||||
$recent = $cache->get("recent_comments");
|
$recent = cache_get_or_set("recent_comments", fn () => $this->get_recent_comments($cc), 60);
|
||||||
if (is_null($recent)) {
|
|
||||||
$recent = $this->get_recent_comments($cc);
|
|
||||||
$cache->set("recent_comments", $recent, 60);
|
|
||||||
}
|
|
||||||
if (count($recent) > 0) {
|
if (count($recent) > 0) {
|
||||||
$this->theme->display_recent_comments($recent);
|
$this->theme->display_recent_comments($recent);
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,14 +52,17 @@ class Featured extends Extension
|
||||||
global $cache, $config, $page, $user;
|
global $cache, $config, $page, $user;
|
||||||
$fid = $config->get_int("featured_id");
|
$fid = $config->get_int("featured_id");
|
||||||
if ($fid > 0) {
|
if ($fid > 0) {
|
||||||
$image = $cache->get("featured_image_object:$fid");
|
$image = cache_get_or_set(
|
||||||
if (is_null($image)) {
|
"featured_image_object:$fid",
|
||||||
$image = Image::by_id($fid);
|
function () use ($fid) {
|
||||||
if ($image) { // make sure the object is fully populated before saving
|
$image = Image::by_id($fid);
|
||||||
$image->get_tag_array();
|
if ($image) { // make sure the object is fully populated before saving
|
||||||
}
|
$image->get_tag_array();
|
||||||
$cache->set("featured_image_object:$fid", $image, 600);
|
}
|
||||||
}
|
return $image;
|
||||||
|
},
|
||||||
|
600
|
||||||
|
);
|
||||||
if (!is_null($image)) {
|
if (!is_null($image)) {
|
||||||
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
||||||
if (!in_array($image->rating, Ratings::get_user_class_privs($user))) {
|
if (!in_array($image->rating, Ratings::get_user_class_privs($user))) {
|
||||||
|
|
|
@ -77,11 +77,11 @@ class Index extends Extension
|
||||||
if (SPEED_HAX) {
|
if (SPEED_HAX) {
|
||||||
if ($count_search_terms === 0 && ($page_number < 10)) {
|
if ($count_search_terms === 0 && ($page_number < 10)) {
|
||||||
// extra caching for the first few post/list pages
|
// extra caching for the first few post/list pages
|
||||||
$images = $cache->get("post-list:$page_number");
|
$images = cache_get_or_set(
|
||||||
if (is_null($images)) {
|
"post-list:$page_number",
|
||||||
$images = Search::find_images(($page_number - 1) * $page_size, $page_size, $search_terms);
|
fn () => Search::find_images(($page_number - 1) * $page_size, $page_size, $search_terms),
|
||||||
$cache->set("post-list:$page_number", $images, 60);
|
60
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -297,18 +297,17 @@ class PrivMsg extends Extension
|
||||||
|
|
||||||
private function count_pms(User $user)
|
private function count_pms(User $user)
|
||||||
{
|
{
|
||||||
global $cache, $database;
|
global $database;
|
||||||
|
|
||||||
$count = $cache->get("pm-count:{$user->id}");
|
return cache_get_or_set(
|
||||||
if (is_null($count)) {
|
"pm-count:{$user->id}",
|
||||||
$count = $database->get_one("
|
fn () => $database->get_one("
|
||||||
SELECT count(*)
|
SELECT count(*)
|
||||||
FROM private_message
|
FROM private_message
|
||||||
WHERE to_id = :to_id
|
WHERE to_id = :to_id
|
||||||
AND is_read = :is_read
|
AND is_read = :is_read
|
||||||
", ["to_id" => $user->id, "is_read" => false]);
|
", ["to_id" => $user->id, "is_read" => false]),
|
||||||
$cache->set("pm-count:{$user->id}", $count, 600);
|
600
|
||||||
}
|
);
|
||||||
return $count;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,14 +240,12 @@ class ReportImage extends Extension
|
||||||
|
|
||||||
public function count_reported_images(): int
|
public function count_reported_images(): int
|
||||||
{
|
{
|
||||||
global $cache, $database;
|
global $database;
|
||||||
|
|
||||||
$count = $cache->get("image-report-count");
|
return (int)cache_get_or_set(
|
||||||
if (is_null($count)) {
|
"image-report-count",
|
||||||
$count = $database->get_one("SELECT count(*) FROM image_reports");
|
fn () => $database->get_one("SELECT count(*) FROM image_reports"),
|
||||||
$cache->set("image-report-count", $count, 600);
|
600
|
||||||
}
|
);
|
||||||
|
|
||||||
return (int)$count;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -553,7 +553,6 @@ class TagList extends Extension
|
||||||
{
|
{
|
||||||
global $cache, $database;
|
global $cache, $database;
|
||||||
|
|
||||||
|
|
||||||
$wild_tags = $search;
|
$wild_tags = $search;
|
||||||
$cache_key = "related_tags:" . md5(Tag::implode($search));
|
$cache_key = "related_tags:" . md5(Tag::implode($search));
|
||||||
$related_tags = $cache->get($cache_key);
|
$related_tags = $cache->get($cache_key);
|
||||||
|
|
Reference in a new issue