From 158f697c88a96758e95fc4d007a0dc79beb9cdb0 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Wed, 11 Dec 2024 17:31:57 +0100 Subject: [PATCH] pages.php: use context cache on mod_ip and mod_ip_remove_note --- inc/mod/pages.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index bcb324b0..45f6db8b 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -4,7 +4,7 @@ */ use Vichan\Context; use Vichan\Data\ReportQueries; -use Vichan\Functions\Format; +use Vichan\Data\Driver\CacheDriver; use Vichan\Functions\Net; use function Vichan\Functions\Net\decode_cursor; @@ -860,11 +860,15 @@ function mod_ip_remove_note(Context $ctx, $ip, $id) { if (filter_var($ip, FILTER_VALIDATE_IP) === false) error("Invalid IP address."); + $cache = $ctx->get(CacheDriver::class); + $query = prepare('DELETE FROM ``ip_notes`` WHERE `ip` = :ip AND `id` = :id'); $query->bindValue(':ip', $ip); $query->bindValue(':id', $id); $query->execute() or error(db_error($query)); + $cache->delete("mod_page_ip_view_notes_$ip"); + modLog("Removed a note for {$ip}"); header('Location: ?/IP/' . $ip . '#notes', true, $config['redirect_http']); @@ -903,7 +907,8 @@ function mod_ip(Context $ctx, $ip, string $encoded_cursor = '') { $query->bindValue(':body', $_POST['note']); $query->execute() or error(db_error($query)); - Cache::delete("mod_page_ip_view_notes_$ip"); + $cache = $ctx->get(CacheDriver::class); + $cache->delete("mod_page_ip_view_notes_$ip"); modLog("Added a note for {$ip}"); @@ -1025,25 +1030,27 @@ function mod_ip(Context $ctx, $ip, string $encoded_cursor = '') { } if (hasPermission($config['mod']['view_notes'])) { - $ret = Cache::get("mod_page_ip_view_notes_$ip"); - if (!$ret) { + $cache = $ctx->get(CacheDriver::class); + $ret = $cache->get("mod_page_ip_view_notes_$ip"); + if ($ret === null) { $query = prepare("SELECT ``ip_notes``.*, `username` FROM ``ip_notes`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE `ip` = :ip ORDER BY `time` DESC"); $query->bindValue(':ip', $ip); $query->execute() or error(db_error($query)); $ret = $query->fetchAll(PDO::FETCH_ASSOC); - Cache::set("mod_page_ip_view_notes_$ip", $ret, 900); + $cache->set("mod_page_ip_view_notes_$ip", $ret, 900); } $args['notes'] = $ret; } if (hasPermission($config['mod']['modlog_ip'])) { - $ret = Cache::get("mod_page_ip_modlog_ip_$ip"); - if (!$ret) { + $cache = $ctx->get(CacheDriver::class); + $ret = $cache->get("mod_page_ip_modlog_ip_$ip"); + if ($ret === null) { $query = prepare("SELECT `username`, `mod`, `ip`, `board`, `time`, `text` FROM ``modlogs`` LEFT JOIN ``mods`` ON `mod` = ``mods``.`id` WHERE `text` LIKE :search ORDER BY `time` DESC LIMIT 50"); $query->bindValue(':search', '%' . $ip . '%'); $query->execute() or error(db_error($query)); $ret = $query->fetchAll(PDO::FETCH_ASSOC); - Cache::set("mod_page_ip_modlog_ip_$ip", $ret, 900); + $cache->set("mod_page_ip_modlog_ip_$ip", $ret, 900); } $args['logs'] = $ret; } else {