From ad6d88443746b2a0604578822354967dc465da2d Mon Sep 17 00:00:00 2001 From: Ming Tsay Date: Sun, 27 Aug 2023 22:55:49 +0800 Subject: [PATCH] clear cache of Me when logout --- app/App.php | 5 +++-- app/Redis.php | 10 ++++++++++ webroot/logout.php | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/App.php b/app/App.php index f05f3ff..61ce698 100644 --- a/app/App.php +++ b/app/App.php @@ -64,6 +64,7 @@ class App public static function unsetU(): void { + Redis::unsetMe($_COOKIE['u'] ?? ''); try { setcookie('u', null, [ 'expires' => time() - 3600, @@ -128,8 +129,8 @@ HTML, try { $u = static::getU(); if ($u === null) return null; - $cachedMe = Redis::getCachedMe($_COOKIE['u']); - if ($cachedMe === null) return Redis::cacheMe($_COOKIE['u'], Auth::getMe($u)); + $cachedMe = Redis::getCachedMe($_COOKIE['u'] ?? ''); + if ($cachedMe === null) return Redis::cacheMe($_COOKIE['u'] ?? '', Auth::getMe($u)); return $cachedMe; } catch (Exception) { error_log('Failed to getMe.'); diff --git a/app/Redis.php b/app/Redis.php index 5981b3c..f2c4f0b 100644 --- a/app/Redis.php +++ b/app/Redis.php @@ -51,4 +51,14 @@ class Redis return null; } } + + public static function unsetMe(string $u): void + { + try { + $redis = static::connect(); + $redis->unlink(static::getKey($u)); + } catch (\RedisException $e) { + trigger_error($e->getMessage()); + } + } } diff --git a/webroot/logout.php b/webroot/logout.php index 1b58474..b546da5 100644 --- a/webroot/logout.php +++ b/webroot/logout.php @@ -1,6 +1,7 @@