From 2e20324fe60cce4cd68a13e39c22f95fb98c2eb2 Mon Sep 17 00:00:00 2001 From: liugang Date: Fri, 8 Nov 2024 10:56:56 +0800 Subject: [PATCH] + [refac] close cache connection when program is shutdown. --- framework/base/router.class.php | 3 +++ lib/cache/cache.class.php | 12 ++++++++++++ lib/cache/driver/RedisDriver.php | 12 ++++++++++++ 3 files changed, 27 insertions(+) diff --git a/framework/base/router.class.php b/framework/base/router.class.php index e3e8df8759..c7ed4ca26c 100644 --- a/framework/base/router.class.php +++ b/framework/base/router.class.php @@ -3226,6 +3226,9 @@ class baseRouter */ public function shutdown() { + /* 如果开启了缓存则关闭缓存连接,主要用于 Redis 等缓存服务。Close the cache connection if it's open. */ + if(!empty($this->cache)) $this->cache->close(); + /* 如果debug模式开启,保存sql语句(If debug on, save sql queries) */ if(!empty($this->config->debug)) $this->saveSQL(); diff --git a/lib/cache/cache.class.php b/lib/cache/cache.class.php index b35fdaf0a0..1ea276bd7f 100644 --- a/lib/cache/cache.class.php +++ b/lib/cache/cache.class.php @@ -893,6 +893,18 @@ class cache return $this->cache->clear(); } + /** + * 关闭缓存连接。 + * Close cache connection. + * + * @access public + * @return void + */ + public function close() + { + if(method_exists($this->cache, 'close')) $this->cache->close(); + } + /** * 获取内存使用情况。 * Get memory usage. diff --git a/lib/cache/driver/RedisDriver.php b/lib/cache/driver/RedisDriver.php index d2402f88d8..3622485e5f 100644 --- a/lib/cache/driver/RedisDriver.php +++ b/lib/cache/driver/RedisDriver.php @@ -215,6 +215,18 @@ class RedisDriver implements CacheInterface return (bool) $this->redis->exists($key); } + /** + * 关闭 Redis 连接。 + * Close the Redis connection. + * + * @access public + * @return bool + */ + public function close() + { + return $this->redis->close(); + } + /** * 获取内存使用情况。 * Get memory usage.