make Cache its own thing, separate from Database

This commit is contained in:
Shish 2019-10-02 10:49:32 +01:00
parent 842df41951
commit e08cdb1638
20 changed files with 105 additions and 117 deletions

View file

@ -4,7 +4,7 @@
* actually do anything as far as the app is concerned * actually do anything as far as the app is concerned
*/ */
global $config, $database, $user, $page, $_tracer; global $cache, $config, $database, $user, $page, $_tracer;
require_once "core/sys_config.php"; require_once "core/sys_config.php";
require_once "core/polyfills.php"; require_once "core/polyfills.php";
@ -37,7 +37,10 @@ unset($_shm_files);
unset($_shm_filename); unset($_shm_filename);
$_tracer->end(); $_tracer->end();
// connect to the database $_tracer->begin("Connecting to Cache");
$cache = new Cache(CACHE_DSN);
$_tracer->end();
$_tracer->begin("Connecting to DB"); $_tracer->begin("Connecting to DB");
$database = new Database(); $database = new Database();
$config = new DatabaseConfig($database); $config = new DatabaseConfig($database);

View file

@ -319,6 +319,8 @@ class DatabaseConfig extends BaseConfig
string $sub_column = null, string $sub_column = null,
string $sub_value = null string $sub_value = null
) { ) {
global $cache;
$this->database = $database; $this->database = $database;
$this->table_name = $table_name; $this->table_name = $table_name;
$this->sub_value = $sub_value; $this->sub_value = $sub_value;
@ -329,7 +331,7 @@ class DatabaseConfig extends BaseConfig
$cache_name .= "_".$sub_value; $cache_name .= "_".$sub_value;
} }
$cached = $this->database->cache->get($cache_name); $cached = $cache->get($cache_name);
if ($cached) { if ($cached) {
$this->values = $cached; $this->values = $cached;
} else { } else {
@ -346,12 +348,14 @@ class DatabaseConfig extends BaseConfig
foreach ($this->database->get_all($query, $args) as $row) { foreach ($this->database->get_all($query, $args) as $row) {
$this->values[$row["name"]] = $row["value"]; $this->values[$row["name"]] = $row["value"];
} }
$this->database->cache->set($cache_name, $this->values); $cache->set($cache_name, $this->values);
} }
} }
public function save(string $name=null): void public function save(string $name=null): void
{ {
global $cache;
if (is_null($name)) { if (is_null($name)) {
reset($this->values); // rewind the array to the first element reset($this->values); // rewind the array to the first element
foreach ($this->values as $name => $value) { foreach ($this->values as $name => $value) {
@ -379,7 +383,7 @@ class DatabaseConfig extends BaseConfig
} }
// rather than deleting and having some other request(s) do a thundering // rather than deleting and having some other request(s) do a thundering
// herd of race-conditioned updates, just save the updated version once here // herd of race-conditioned updates, just save the updated version once here
$this->database->cache->set("config", $this->values); $cache->set("config", $this->values);
} }
} }

View file

@ -29,12 +29,6 @@ class Database
*/ */
private $engine = null; private $engine = null;
/**
* The currently active cache engine.
* @var Cache|null
*/
public $cache = null;
/** /**
* A boolean flag to track if we already have an active transaction. * A boolean flag to track if we already have an active transaction.
* (ie: True if beginTransaction() already called) * (ie: True if beginTransaction() already called)
@ -48,16 +42,6 @@ class Database
*/ */
public $query_count = 0; public $query_count = 0;
/**
* For now, only connect to the cache, as we will pretty much certainly
* need it. There are some pages where all the data is in cache, so the
* DB connection is on-demand.
*/
public function __construct()
{
$this->cache = new Cache(CACHE_DSN);
}
private function connect_db(): void private function connect_db(): void
{ {
# FIXME: detect ADODB URI, automatically translate PDO DSN # FIXME: detect ADODB URI, automatically translate PDO DSN
@ -364,12 +348,9 @@ class MockDatabase extends Database
private $query_id = 0; private $query_id = 0;
/** @var array */ /** @var array */
private $responses = []; private $responses = [];
/** @var ?NoCache */
public $cache = null;
public function __construct(array $responses = []) public function __construct(array $responses = [])
{ {
$this->cache = new NoCache();
$this->responses = $responses; $this->responses = $responses;
} }

View file

@ -41,16 +41,16 @@ class Image
/** @var int */ /** @var int */
public $owner_id; public $owner_id;
/** @var string */ /** @var string */
public $owner_ip; public $owner_ip;
/** @var string */ /** @var string */
public $posted; public $posted;
/** @var string */ /** @var string */
public $source; public $source;
/** @var boolean */ /** @var boolean */
public $locked = false; public $locked = false;
@ -291,14 +291,14 @@ class Image
*/ */
public static function count_images(array $tags=[]): int public static function count_images(array $tags=[]): int
{ {
global $database; global $cache, $database;
$tag_count = count($tags); $tag_count = count($tags);
if ($tag_count === 0) { if ($tag_count === 0) {
$total = $database->cache->get("image-count"); $total = $cache->get("image-count");
if (!$total) { if (!$total) {
$total = $database->get_one("SELECT COUNT(*) FROM images"); $total = $database->get_one("SELECT COUNT(*) FROM images");
$database->cache->set("image-count", $total, 600); $cache->set("image-count", $total, 600);
} }
} elseif ($tag_count === 1 && !preg_match("/[:=><\*\?]/", $tags[0])) { } elseif ($tag_count === 1 && !preg_match("/[:=><\*\?]/", $tags[0])) {
$total = $database->get_one( $total = $database->get_one(
@ -682,7 +682,7 @@ class Image
*/ */
public function set_tags(array $unfiltered_tags): void public function set_tags(array $unfiltered_tags): void
{ {
global $database; global $cache, $database;
$unfiltered_tags = array_unique($unfiltered_tags); $unfiltered_tags = array_unique($unfiltered_tags);
@ -755,7 +755,7 @@ class Image
} }
log_info("core_image", "Tags for Image #{$this->id} set to: ".Tag::implode($tags), null, ["image_id" => $this->id]); log_info("core_image", "Tags for Image #{$this->id} set to: ".Tag::implode($tags), null, ["image_id" => $this->id]);
$database->cache->delete("image-{$this->id}-tags"); $cache->delete("image-{$this->id}-tags");
} }
} }

View file

@ -66,8 +66,8 @@ class User
public static function by_session(string $name, string $session): ?User public static function by_session(string $name, string $session): ?User
{ {
global $config, $database; global $cache, $config, $database;
$row = $database->cache->get("user-session:$name-$session"); $row = $cache->get("user-session:$name-$session");
if (!$row) { if (!$row) {
if ($database->get_driver_name() === DatabaseDriver::MYSQL) { if ($database->get_driver_name() === DatabaseDriver::MYSQL) {
$query = "SELECT * FROM users WHERE name = :name AND md5(concat(pass, :ip)) = :sess"; $query = "SELECT * FROM users WHERE name = :name AND md5(concat(pass, :ip)) = :sess";
@ -75,23 +75,23 @@ class User
$query = "SELECT * FROM users WHERE name = :name AND md5(pass || :ip) = :sess"; $query = "SELECT * FROM users WHERE name = :name AND md5(pass || :ip) = :sess";
} }
$row = $database->get_row($query, ["name"=>$name, "ip"=>get_session_ip($config), "sess"=>$session]); $row = $database->get_row($query, ["name"=>$name, "ip"=>get_session_ip($config), "sess"=>$session]);
$database->cache->set("user-session:$name-$session", $row, 600); $cache->set("user-session:$name-$session", $row, 600);
} }
return is_null($row) ? null : new User($row); return is_null($row) ? null : new User($row);
} }
public static function by_id(int $id): ?User public static function by_id(int $id): ?User
{ {
global $database; global $cache, $database;
if ($id === 1) { if ($id === 1) {
$cached = $database->cache->get('user-id:'.$id); $cached = $cache->get('user-id:'.$id);
if ($cached) { if ($cached) {
return new User($cached); return new User($cached);
} }
} }
$row = $database->get_row("SELECT * FROM users WHERE id = :id", ["id"=>$id]); $row = $database->get_row("SELECT * FROM users WHERE id = :id", ["id"=>$id]);
if ($id === 1) { if ($id === 1) {
$database->cache->set('user-id:'.$id, $row, 600); $cache->set('user-id:'.$id, $row, 600);
} }
return is_null($row) ? null : new User($row); return is_null($row) ? null : new User($row);
} }

View file

@ -305,7 +305,7 @@ function path_to_tags(string $path): string
$path = dirname($path); $path = dirname($path);
$path = str_replace(";", ":", $path); $path = str_replace(";", ":", $path);
$path = str_replace("__", " ", $path); $path = str_replace("__", " ", $path);
$category = ""; $category = "";
foreach (explode("/", $path) as $dir) { foreach (explode("/", $path) as $dir) {
@ -366,7 +366,7 @@ $_shm_load_start = microtime(true);
*/ */
function get_debug_info(): string function get_debug_info(): string
{ {
global $config, $_shm_event_count, $database, $_shm_load_start; global $cache, $config, $_shm_event_count, $database, $_shm_load_start;
$i_mem = sprintf("%5.2f", ((memory_get_peak_usage(true)+512)/1024)/1024); $i_mem = sprintf("%5.2f", ((memory_get_peak_usage(true)+512)/1024)/1024);
@ -378,8 +378,8 @@ function get_debug_info(): string
$time = sprintf("%.2f", microtime(true) - $_shm_load_start); $time = sprintf("%.2f", microtime(true) - $_shm_load_start);
$dbtime = sprintf("%.2f", $database->dbtime); $dbtime = sprintf("%.2f", $database->dbtime);
$i_files = count(get_included_files()); $i_files = count(get_included_files());
$hits = $database->cache->get_hits(); $hits = $cache->get_hits();
$miss = $database->cache->get_misses(); $miss = $cache->get_misses();
$debug = "<br>Took $time seconds (db:$dbtime) and {$i_mem}MB of RAM"; $debug = "<br>Took $time seconds (db:$dbtime) and {$i_mem}MB of RAM";
$debug .= "; Used $i_files files and {$database->query_count} queries"; $debug .= "; Used $i_files files and {$database->query_count} queries";

View file

@ -9,7 +9,7 @@ class AutoComplete extends Extension
public function onPageRequest(PageRequestEvent $event) public function onPageRequest(PageRequestEvent $event)
{ {
global $page, $database; global $cache, $page, $database;
if ($event->page_matches("api/internal/autocomplete")) { if ($event->page_matches("api/internal/autocomplete")) {
if (!isset($_GET["s"])) { if (!isset($_GET["s"])) {
@ -42,7 +42,7 @@ class AutoComplete extends Extension
$cache_key .= "-" . $_GET["limit"]; $cache_key .= "-" . $_GET["limit"];
} }
$res = $database->cache->get($cache_key); $res = $cache->get($cache_key);
if (!$res) { if (!$res) {
$res = $database->get_pairs( $res = $database->get_pairs(
$database->scoreql_to_sql(" $database->scoreql_to_sql("
@ -55,7 +55,7 @@ class AutoComplete extends Extension
$limitSQL"), $limitSQL"),
$SQLarr $SQLarr
); );
$database->cache->set($cache_key, $res, 600); $cache->set($cache_key, $res, 600);
} }
$page->set_data(json_encode($res)); $page->set_data(json_encode($res));

View file

@ -39,12 +39,12 @@ class Blocks extends Extension
public function onPageRequest(PageRequestEvent $event) public function onPageRequest(PageRequestEvent $event)
{ {
global $database, $page, $user; global $cache, $database, $page, $user;
$blocks = $database->cache->get("blocks"); $blocks = $cache->get("blocks");
if ($blocks === false) { if ($blocks === false) {
$blocks = $database->get_all("SELECT * FROM blocks"); $blocks = $database->get_all("SELECT * FROM blocks");
$database->cache->set("blocks", $blocks, 600); $cache->set("blocks", $blocks, 600);
} }
foreach ($blocks as $block) { foreach ($blocks as $block) {
$path = implode("/", $event->args); $path = implode("/", $event->args);
@ -63,7 +63,7 @@ class Blocks extends Extension
VALUES (?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?)
", [$_POST['pages'], $_POST['title'], $_POST['area'], (int)$_POST['priority'], $_POST['content']]); ", [$_POST['pages'], $_POST['title'], $_POST['area'], (int)$_POST['priority'], $_POST['content']]);
log_info("blocks", "Added Block #".($database->get_last_insert_id('blocks_id_seq'))." (".$_POST['title'].")"); log_info("blocks", "Added Block #".($database->get_last_insert_id('blocks_id_seq'))." (".$_POST['title'].")");
$database->cache->delete("blocks"); $cache->delete("blocks");
$page->set_mode(PageMode::REDIRECT); $page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("blocks/list")); $page->set_redirect(make_link("blocks/list"));
} }
@ -83,7 +83,7 @@ class Blocks extends Extension
", [$_POST['pages'], $_POST['title'], $_POST['area'], (int)$_POST['priority'], $_POST['content'], $_POST['id']]); ", [$_POST['pages'], $_POST['title'], $_POST['area'], (int)$_POST['priority'], $_POST['content'], $_POST['id']]);
log_info("blocks", "Updated Block #".$_POST['id']." (".$_POST['title'].")"); log_info("blocks", "Updated Block #".$_POST['id']." (".$_POST['title'].")");
} }
$database->cache->delete("blocks"); $cache->delete("blocks");
$page->set_mode(PageMode::REDIRECT); $page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("blocks/list")); $page->set_redirect(make_link("blocks/list"));
} }

View file

@ -263,13 +263,13 @@ class CommentList extends Extension
public function onPostListBuilding(PostListBuildingEvent $event) public function onPostListBuilding(PostListBuildingEvent $event)
{ {
global $config, $database; global $cache, $config, $database;
$cc = $config->get_int("comment_count"); $cc = $config->get_int("comment_count");
if ($cc > 0) { if ($cc > 0) {
$recent = $database->cache->get("recent_comments"); $recent = $cache->get("recent_comments");
if (empty($recent)) { if (empty($recent)) {
$recent = $this->get_recent_comments($cc); $recent = $this->get_recent_comments($cc);
$database->cache->set("recent_comments", $recent, 60); $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);
@ -370,17 +370,17 @@ class CommentList extends Extension
// page building {{{ // page building {{{
private function build_page(int $current_page) private function build_page(int $current_page)
{ {
global $database, $user; global $cache, $database, $user;
$where = SPEED_HAX ? "WHERE posted > now() - interval '24 hours'" : ""; $where = SPEED_HAX ? "WHERE posted > now() - interval '24 hours'" : "";
$total_pages = $database->cache->get("comment_pages"); $total_pages = $cache->get("comment_pages");
if (empty($total_pages)) { if (empty($total_pages)) {
$total_pages = (int)($database->get_one(" $total_pages = (int)($database->get_one("
SELECT COUNT(c1) SELECT COUNT(c1)
FROM (SELECT COUNT(image_id) AS c1 FROM comments $where GROUP BY image_id) AS s1 FROM (SELECT COUNT(image_id) AS c1 FROM comments $where GROUP BY image_id) AS s1
") / 10); ") / 10);
$database->cache->set("comment_pages", $total_pages, 600); $cache->set("comment_pages", $total_pages, 600);
} }
$total_pages = max($total_pages, 1); $total_pages = max($total_pages, 1);

View file

@ -42,16 +42,16 @@ class Featured extends Extension
public function onPostListBuilding(PostListBuildingEvent $event) public function onPostListBuilding(PostListBuildingEvent $event)
{ {
global $config, $database, $page, $user; global $cache, $config, $database, $page, $user;
$fid = $config->get_int("featured_id"); $fid = $config->get_int("featured_id");
if ($fid > 0) { if ($fid > 0) {
$image = $database->cache->get("featured_image_object:$fid"); $image = $cache->get("featured_image_object:$fid");
if ($image === false) { if ($image === false) {
$image = Image::by_id($fid); $image = Image::by_id($fid);
if ($image) { // make sure the object is fully populated before saving if ($image) { // make sure the object is fully populated before saving
$image->get_tag_array(); $image->get_tag_array();
} }
$database->cache->set("featured_image_object:$fid", $image, 600); $cache->set("featured_image_object:$fid", $image, 600);
} }
if (!is_null($image)) { if (!is_null($image)) {
if (Extension::is_enabled(RatingsInfo::KEY)) { if (Extension::is_enabled(RatingsInfo::KEY)) {

View file

@ -79,7 +79,7 @@ class Index extends Extension
public function onPageRequest(PageRequestEvent $event) public function onPageRequest(PageRequestEvent $event)
{ {
global $database, $page, $user; global $cache, $page, $user;
if ($event->page_matches("post/list")) { if ($event->page_matches("post/list")) {
if (isset($_GET['search'])) { if (isset($_GET['search'])) {
// implode(explode()) to resolve aliases and sanitise // implode(explode()) to resolve aliases and sanitise
@ -123,10 +123,10 @@ class Index extends Extension
} }
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 = $database->cache->get("post-list:$page_number"); $images = $cache->get("post-list:$page_number");
if (!$images) { if (!$images) {
$images = Image::find_images(($page_number-1)*$page_size, $page_size, $search_terms); $images = Image::find_images(($page_number-1)*$page_size, $page_size, $search_terms);
$database->cache->set("post-list:$page_number", $images, 60); $cache->set("post-list:$page_number", $images, 60);
} }
} }
} }
@ -175,9 +175,9 @@ class Index extends Extension
public function onImageInfoSet(ImageInfoSetEvent $event) public function onImageInfoSet(ImageInfoSetEvent $event)
{ {
global $database; global $cache;
if (SPEED_HAX) { if (SPEED_HAX) {
$database->cache->delete("thumb-block:{$event->image->id}"); $cache->delete("thumb-block:{$event->image->id}");
} }
} }

View file

@ -113,20 +113,20 @@ class IPBan extends Extension
public function onAddIPBan(AddIPBanEvent $event) public function onAddIPBan(AddIPBanEvent $event)
{ {
global $user, $database; global $cache, $user, $database;
$sql = "INSERT INTO bans (ip, reason, end_timestamp, banner_id) VALUES (:ip, :reason, :end, :admin_id)"; $sql = "INSERT INTO bans (ip, reason, end_timestamp, banner_id) VALUES (:ip, :reason, :end, :admin_id)";
$database->Execute($sql, ["ip"=>$event->ip, "reason"=>$event->reason, "end"=>strtotime($event->end), "admin_id"=>$user->id]); $database->Execute($sql, ["ip"=>$event->ip, "reason"=>$event->reason, "end"=>strtotime($event->end), "admin_id"=>$user->id]);
$database->cache->delete("ip_bans_sorted"); $cache->delete("ip_bans_sorted");
log_info("ipban", "Banned {$event->ip} because '{$event->reason}' until {$event->end}"); log_info("ipban", "Banned {$event->ip} because '{$event->reason}' until {$event->end}");
} }
public function onRemoveIPBan(RemoveIPBanEvent $event) public function onRemoveIPBan(RemoveIPBanEvent $event)
{ {
global $database; global $cache, $database;
$ban = $database->get_row("SELECT * FROM bans WHERE id = :id", ["id"=>$event->id]); $ban = $database->get_row("SELECT * FROM bans WHERE id = :id", ["id"=>$event->id]);
if ($ban) { if ($ban) {
$database->Execute("DELETE FROM bans WHERE id = :id", ["id"=>$event->id]); $database->Execute("DELETE FROM bans WHERE id = :id", ["id"=>$event->id]);
$database->cache->delete("ip_bans_sorted"); $cache->delete("ip_bans_sorted");
log_info("ipban", "Removed {$ban['ip']}'s ban"); log_info("ipban", "Removed {$ban['ip']}'s ban");
} }
} }
@ -306,9 +306,9 @@ class IPBan extends Extension
// returns [ips, nets] // returns [ips, nets]
private function get_active_bans_sorted() private function get_active_bans_sorted()
{ {
global $database; global $cache;
$cached = $database->cache->get("ip_bans_sorted"); $cached = $cache->get("ip_bans_sorted");
if ($cached) { if ($cached) {
return $cached; return $cached;
} }
@ -325,7 +325,7 @@ class IPBan extends Extension
} }
$sorted = [$ips, $nets]; $sorted = [$ips, $nets];
$database->cache->set("ip_bans_sorted", $sorted, 600); $cache->set("ip_bans_sorted", $sorted, 600);
return $sorted; return $sorted;
} }
// }}} // }}}

View file

@ -39,7 +39,7 @@ class LogDatabase extends Extension
public function onPageRequest(PageRequestEvent $event) public function onPageRequest(PageRequestEvent $event)
{ {
global $database, $user; global $cache, $database, $user;
if ($event->page_matches("log/view")) { if ($event->page_matches("log/view")) {
if ($user->can(Permissions::VIEW_EVENTLOG)) { if ($user->can(Permissions::VIEW_EVENTLOG)) {
$wheres = []; $wheres = [];
@ -95,12 +95,12 @@ class LogDatabase extends Extension
$limit = 50; $limit = 50;
$offset = ($page_num-1) * $limit; $offset = ($page_num-1) * $limit;
$page_total = $database->cache->get("event_log_length"); $page_total = $cache->get("event_log_length");
if (!$page_total) { if (!$page_total) {
$page_total = $database->get_one("SELECT count(*) FROM score_log $where", $args); $page_total = $database->get_one("SELECT count(*) FROM score_log $where", $args);
// don't cache a length of zero when the extension is first installed // don't cache a length of zero when the extension is first installed
if ($page_total > 10) { if ($page_total > 10) {
$database->cache->set("event_log_length", $page_total, 600); $cache->set("event_log_length", $page_total, 600);
} }
} }

View file

@ -122,7 +122,7 @@ class PrivMsg extends Extension
public function onPageRequest(PageRequestEvent $event) public function onPageRequest(PageRequestEvent $event)
{ {
global $database, $page, $user; global $cache, $database, $page, $user;
if ($event->page_matches("pm")) { if ($event->page_matches("pm")) {
if (!$user->is_anonymous()) { if (!$user->is_anonymous()) {
switch ($event->get_arg(0)) { switch ($event->get_arg(0)) {
@ -135,7 +135,7 @@ class PrivMsg extends Extension
$from_user = User::by_id(int_escape($pm["from_id"])); $from_user = User::by_id(int_escape($pm["from_id"]));
if ($pm["to_id"] == $user->id) { if ($pm["to_id"] == $user->id) {
$database->execute("UPDATE private_message SET is_read='Y' WHERE id = :id", ["id" => $pm_id]); $database->execute("UPDATE private_message SET is_read='Y' WHERE id = :id", ["id" => $pm_id]);
$database->cache->delete("pm-count-{$user->id}"); $cache->delete("pm-count-{$user->id}");
} }
$this->theme->display_message($page, $from_user, $user, new PM($pm)); $this->theme->display_message($page, $from_user, $user, new PM($pm));
} else { } else {
@ -150,7 +150,7 @@ class PrivMsg extends Extension
$this->theme->display_error(404, "No such PM", "There is no PM #$pm_id"); $this->theme->display_error(404, "No such PM", "There is no PM #$pm_id");
} elseif (($pm["to_id"] == $user->id) || $user->can(Permissions::VIEW_OTHER_PMS)) { } elseif (($pm["to_id"] == $user->id) || $user->can(Permissions::VIEW_OTHER_PMS)) {
$database->execute("DELETE FROM private_message WHERE id = :id", ["id" => $pm_id]); $database->execute("DELETE FROM private_message WHERE id = :id", ["id" => $pm_id]);
$database->cache->delete("pm-count-{$user->id}"); $cache->delete("pm-count-{$user->id}");
log_info("pm", "Deleted PM #$pm_id", "PM deleted"); log_info("pm", "Deleted PM #$pm_id", "PM deleted");
$page->set_mode(PageMode::REDIRECT); $page->set_mode(PageMode::REDIRECT);
$page->set_redirect($_SERVER["HTTP_REFERER"]); $page->set_redirect($_SERVER["HTTP_REFERER"]);
@ -179,7 +179,7 @@ class PrivMsg extends Extension
public function onSendPM(SendPMEvent $event) public function onSendPM(SendPMEvent $event)
{ {
global $database; global $cache, $database;
$database->execute( $database->execute(
" "
INSERT INTO private_message( INSERT INTO private_message(
@ -189,7 +189,7 @@ class PrivMsg extends Extension
["fromid" => $event->pm->from_id, "fromip" => $event->pm->from_ip, ["fromid" => $event->pm->from_id, "fromip" => $event->pm->from_ip,
"toid" => $event->pm->to_id, "subject" => $event->pm->subject, "message" => $event->pm->message] "toid" => $event->pm->to_id, "subject" => $event->pm->subject, "message" => $event->pm->message]
); );
$database->cache->delete("pm-count-{$event->pm->to_id}"); $cache->delete("pm-count-{$event->pm->to_id}");
log_info("pm", "Sent PM to User #{$event->pm->to_id}"); log_info("pm", "Sent PM to User #{$event->pm->to_id}");
} }
@ -216,9 +216,9 @@ class PrivMsg extends Extension
private function count_pms(User $user) private function count_pms(User $user)
{ {
global $database; global $cache, $database;
$count = $database->cache->get("pm-count:{$user->id}"); $count = $cache->get("pm-count:{$user->id}");
if (is_null($count) || $count === false) { if (is_null($count) || $count === false) {
$count = $database->get_one(" $count = $database->get_one("
SELECT count(*) SELECT count(*)
@ -226,7 +226,7 @@ class PrivMsg extends Extension
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" => "N"]); ", ["to_id" => $user->id, "is_read" => "N"]);
$database->cache->set("pm-count:{$user->id}", $count, 600); $cache->set("pm-count:{$user->id}", $count, 600);
} }
return $count; return $count;
} }

View file

@ -5,10 +5,10 @@ class RegenThumb extends Extension
{ {
public function regenerate_thumbnail($image, $force = true): string public function regenerate_thumbnail($image, $force = true): string
{ {
global $database; global $cache;
$event = new ThumbnailGenerationEvent($image->hash, $image->ext, $force); $event = new ThumbnailGenerationEvent($image->hash, $image->ext, $force);
send_event($event); send_event($event);
$database->cache->delete("thumb-block:{$image->id}"); $cache->delete("thumb-block:{$image->id}");
return $event->generated; return $event->generated;
} }

View file

@ -89,21 +89,21 @@ class ReportImage extends Extension
public function onAddReportedImage(AddReportedImageEvent $event) public function onAddReportedImage(AddReportedImageEvent $event)
{ {
global $database; global $cache, $database;
log_info("report_image", "Adding report of Image #{$event->report->image_id} with reason '{$event->report->reason}'", null, ["image_id" => $event->report->image_id]); log_info("report_image", "Adding report of Image #{$event->report->image_id} with reason '{$event->report->reason}'", null, ["image_id" => $event->report->image_id]);
$database->Execute( $database->Execute(
"INSERT INTO image_reports(image_id, reporter_id, reason) "INSERT INTO image_reports(image_id, reporter_id, reason)
VALUES (?, ?, ?)", VALUES (?, ?, ?)",
[$event->report->image_id, $event->report->user_id, $event->report->reason] [$event->report->image_id, $event->report->user_id, $event->report->reason]
); );
$database->cache->delete("image-report-count"); $cache->delete("image-report-count");
} }
public function onRemoveReportedImage(RemoveReportedImageEvent $event) public function onRemoveReportedImage(RemoveReportedImageEvent $event)
{ {
global $database; global $cache, $database;
$database->Execute("DELETE FROM image_reports WHERE id = ?", [$event->id]); $database->Execute("DELETE FROM image_reports WHERE id = ?", [$event->id]);
$database->cache->delete("image-report-count"); $cache->delete("image-report-count");
} }
public function onUserPageBuilding(UserPageBuildingEvent $event) public function onUserPageBuilding(UserPageBuildingEvent $event)
@ -149,9 +149,9 @@ class ReportImage extends Extension
public function onImageDeletion(ImageDeletionEvent $event) public function onImageDeletion(ImageDeletionEvent $event)
{ {
global $database; global $cache, $database;
$database->Execute("DELETE FROM image_reports WHERE image_id = ?", [$event->image->id]); $database->Execute("DELETE FROM image_reports WHERE image_id = ?", [$event->image->id]);
$database->cache->delete("image-report-count"); $cache->delete("image-report-count");
} }
public function onUserDeletion(UserDeletionEvent $event) public function onUserDeletion(UserDeletionEvent $event)
@ -176,9 +176,9 @@ class ReportImage extends Extension
public function delete_reports_by(int $user_id) public function delete_reports_by(int $user_id)
{ {
global $database; global $cache, $database;
$database->execute("DELETE FROM image_reports WHERE reporter_id=?", [$user_id]); $database->execute("DELETE FROM image_reports WHERE reporter_id=?", [$user_id]);
$database->cache->delete("image-report-count"); $cache->delete("image-report-count");
} }
protected function install() protected function install()
@ -247,12 +247,12 @@ class ReportImage extends Extension
public function count_reported_images(): int public function count_reported_images(): int
{ {
global $database; global $cache, $database;
$count = $database->cache->get("image-report-count"); $count = $cache->get("image-report-count");
if (is_null($count) || $count === false) { if (is_null($count) || $count === false) {
$count = $database->get_one("SELECT count(*) FROM image_reports"); $count = $database->get_one("SELECT count(*) FROM image_reports");
$database->cache->set("image-report-count", $count, 600); $cache->set("image-report-count", $count, 600);
} }
return $count; return $count;

View file

@ -76,9 +76,9 @@ class RSSImages extends Extension
private function thumb(Image $image): string private function thumb(Image $image): string
{ {
global $database; global $cache, $database;
$cached = $database->cache->get("rss-thumb:{$image->id}"); $cached = $cache->get("rss-thumb:{$image->id}");
if ($cached) { if ($cached) {
return $cached; return $cached;
} }
@ -106,7 +106,7 @@ class RSSImages extends Extension
</item> </item>
"; ";
$database->cache->set("rss-thumb:{$image->id}", $data, 3600); $cache->set("rss-thumb:{$image->id}", $data, 3600);
return $data; return $data;
} }

View file

@ -13,7 +13,7 @@ class StatsDInterface extends Extension
private function _stats(string $type) private function _stats(string $type)
{ {
global $_shm_event_count, $database, $_shm_load_start; global $_shm_event_count, $cache, $database, $_shm_load_start;
$time = microtime(true) - $_shm_load_start; $time = microtime(true) - $_shm_load_start;
StatsDInterface::$stats["shimmie.$type.hits"] = "1|c"; StatsDInterface::$stats["shimmie.$type.hits"] = "1|c";
StatsDInterface::$stats["shimmie.$type.time"] = "$time|ms"; StatsDInterface::$stats["shimmie.$type.time"] = "$time|ms";
@ -22,8 +22,8 @@ class StatsDInterface extends Extension
StatsDInterface::$stats["shimmie.$type.files"] = count(get_included_files())."|c"; StatsDInterface::$stats["shimmie.$type.files"] = count(get_included_files())."|c";
StatsDInterface::$stats["shimmie.$type.queries"] = $database->query_count."|c"; StatsDInterface::$stats["shimmie.$type.queries"] = $database->query_count."|c";
StatsDInterface::$stats["shimmie.$type.events"] = $_shm_event_count."|c"; StatsDInterface::$stats["shimmie.$type.events"] = $_shm_event_count."|c";
StatsDInterface::$stats["shimmie.$type.cache-hits"] = $database->cache->get_hits()."|c"; StatsDInterface::$stats["shimmie.$type.cache-hits"] = $cache->get_hits()."|c";
StatsDInterface::$stats["shimmie.$type.cache-misses"] = $database->cache->get_misses()."|c"; StatsDInterface::$stats["shimmie.$type.cache-misses"] = $cache->get_misses()."|c";
} }
public function onPageRequest(PageRequestEvent $event) public function onPageRequest(PageRequestEvent $event)

View file

@ -20,7 +20,7 @@ class TagList extends Extension
public function onPageRequest(PageRequestEvent $event) public function onPageRequest(PageRequestEvent $event)
{ {
global $page, $database; global $cache, $page, $database;
if ($event->page_matches("tags")) { if ($event->page_matches("tags")) {
$this->theme->set_navigation($this->build_navigation()); $this->theme->set_navigation($this->build_navigation());
@ -60,7 +60,7 @@ class TagList extends Extension
} }
$res = null; $res = null;
$database->cache->get($cache_key); $cache->get($cache_key);
if (!$res) { if (!$res) {
$res = $database->get_col($database->scoreql_to_sql(" $res = $database->get_col($database->scoreql_to_sql("
SELECT tag SELECT tag
@ -69,7 +69,7 @@ class TagList extends Extension
AND count > 0 AND count > 0
$limitSQL $limitSQL
"), $SQLarr); "), $SQLarr);
$database->cache->set($cache_key, $res, 600); $cache->set($cache_key, $res, 600);
} }
$page->set_mode(PageMode::DATA); $page->set_mode(PageMode::DATA);
@ -183,10 +183,10 @@ class TagList extends Extension
private static function get_omitted_tags(): array private static function get_omitted_tags(): array
{ {
global $config, $database; global $cache, $config, $database;
$tags_config = $config->get_string(TagListConfig::OMIT_TAGS); $tags_config = $config->get_string(TagListConfig::OMIT_TAGS);
$results = $database->cache->get("tag_list_omitted_tags:".$tags_config); $results = $cache->get("tag_list_omitted_tags:".$tags_config);
if ($results==null) { if ($results==null) {
$results = []; $results = [];
@ -213,7 +213,7 @@ class TagList extends Extension
$results = $database->get_col("SELECT id FROM tags WHERE " . implode(" OR ", $where), $args); $results = $database->get_col("SELECT id FROM tags WHERE " . implode(" OR ", $where), $args);
$database->cache->set("tag_list_omitted_tags:" . $tags_config, $results, 600); $cache->set("tag_list_omitted_tags:" . $tags_config, $results, 600);
} }
return $results; return $results;
} }
@ -531,9 +531,9 @@ class TagList extends Extension
private function add_popular_block(Page $page) private function add_popular_block(Page $page)
{ {
global $database, $config; global $cache, $database, $config;
$tags = $database->cache->get("popular_tags"); $tags = $cache->get("popular_tags");
if (empty($tags)) { if (empty($tags)) {
$omitted_tags = self::get_omitted_tags(); $omitted_tags = self::get_omitted_tags();
@ -560,7 +560,7 @@ class TagList extends Extension
$tags = $database->get_all($query, $args); $tags = $database->get_all($query, $args);
$database->cache->set("popular_tags", $tags, 600); $cache->set("popular_tags", $tags, 600);
} }
if (count($tags) > 0) { if (count($tags) > 0) {
$this->theme->display_popular_block($page, $tags); $this->theme->display_popular_block($page, $tags);
@ -589,12 +589,12 @@ class TagList extends Extension
public static function get_related_tags(array $search, int $limit): array public static function get_related_tags(array $search, int $limit): array
{ {
global $config, $database; global $cache, $database;
$wild_tags = $search; $wild_tags = $search;
$str_search = Tag::implode($search); $str_search = Tag::implode($search);
$related_tags = $database->cache->get("related_tags:$str_search"); $related_tags = $cache->get("related_tags:$str_search");
if (empty($related_tags)) { if (empty($related_tags)) {
// $search_tags = array(); // $search_tags = array();
@ -645,7 +645,7 @@ class TagList extends Extension
$args = ["limit" => $limit]; $args = ["limit" => $limit];
$related_tags = $database->get_all($query, $args); $related_tags = $database->get_all($query, $args);
$database->cache->set("related_tags:$str_search", $related_tags, 60 * 60); $cache->set("related_tags:$str_search", $related_tags, 60 * 60);
} }
} }
if ($related_tags === false) { if ($related_tags === false) {

View file

@ -165,7 +165,7 @@ class Upload extends Extension
public function onPageRequest(PageRequestEvent $event) public function onPageRequest(PageRequestEvent $event)
{ {
global $database, $page, $user; global $cache, $page, $user;
if ($user->can(Permissions::CREATE_IMAGE)) { if ($user->can(Permissions::CREATE_IMAGE)) {
if ($this->is_full) { if ($this->is_full) {
@ -219,14 +219,14 @@ class Upload extends Extension
} }
} }
} }
$database->cache->delete("thumb-block:{$image_id}"); $cache->delete("thumb-block:{$image_id}");
$this->theme->display_upload_status($page, $ok); $this->theme->display_upload_status($page, $ok);
} elseif (!empty($_GET['url'])) { } elseif (!empty($_GET['url'])) {
$url = $_GET['url']; $url = $_GET['url'];
$tags = isset($_GET['tags']) ? Tag::explode($_GET['tags']) : 'tagme'; $tags = isset($_GET['tags']) ? Tag::explode($_GET['tags']) : 'tagme';
$source = isset($_GET['source']) ? $_GET['source'] : $url; $source = isset($_GET['source']) ? $_GET['source'] : $url;
$ok = $this->try_transload($url, $tags, $source, $image_id); $ok = $this->try_transload($url, $tags, $source, $image_id);
$database->cache->delete("thumb-block:{$image_id}"); $cache->delete("thumb-block:{$image_id}");
$this->theme->display_upload_status($page, $ok); $this->theme->display_upload_status($page, $ok);
} else { } else {
$this->theme->display_replace_page($page, $image_id); $this->theme->display_replace_page($page, $image_id);