From 6977d3d136fcdbbb420bdf207a3addf0d93d06a7 Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Tue, 17 Oct 2023 12:14:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20SaTokenDao=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E6=9B=B4=E6=8D=A2=E6=97=B6=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/dev33/satoken/SaManager.java | 5 ++++- .../java/cn/dev33/satoken/dao/SaTokenDao.java | 13 +++++++++--- .../satoken/dao/SaTokenDaoDefaultImpl.java | 21 ++++++++++--------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/SaManager.java b/sa-token-core/src/main/java/cn/dev33/satoken/SaManager.java index a2dea0297..1fb7c22e3 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/SaManager.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/SaManager.java @@ -103,9 +103,12 @@ public static void setSaTokenDao(SaTokenDao saTokenDao) { } private static void setSaTokenDaoMethod(SaTokenDao saTokenDao) { if (SaManager.saTokenDao != null) { - SaManager.saTokenDao.onChange(); + SaManager.saTokenDao.destroy(); } SaManager.saTokenDao = saTokenDao; + if (SaManager.saTokenDao != null) { + SaManager.saTokenDao.init(); + } } public static SaTokenDao getSaTokenDao() { if (saTokenDao == null) { diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDao.java b/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDao.java index 75a46cd42..571b98746 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDao.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDao.java @@ -201,11 +201,18 @@ default void updateSessionTimeout(String sessionId, long timeout) { List searchData(String prefix, String keyword, int start, int size, boolean sortType); - // --------------------- 实例相关 --------------------- + // --------------------- 生命周期 --------------------- /** - * 当 SaManager.saTokenDao 变更时,调用该方法 + * 当此 SaTokenDao 实例被装载时触发 */ - default void onChange() { + default void init() { } + + /** + * 当此 SaTokenDao 实例被卸载时触发 + */ + default void destroy() { + } + } diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDaoDefaultImpl.java b/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDaoDefaultImpl.java index eae8d6e9e..c4be19984 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDaoDefaultImpl.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDaoDefaultImpl.java @@ -40,14 +40,6 @@ public class SaTokenDaoDefaultImpl implements SaTokenDao { * 存储数据过期时间的集合(单位: 毫秒), 记录所有 key 的到期时间 (注意存储的是到期时间,不是剩余存活时间) */ public Map expireMap = new ConcurrentHashMap<>(); - - /** - * 构造函数 - */ - public SaTokenDaoDefaultImpl() { - initRefreshThread(); - } - // ------------------------ String 读写操作 @@ -261,11 +253,20 @@ public void initRefreshThread() { this.refreshThread.start(); } + + /** + * 组件被安装时,开始刷新数据线程 + */ + @Override + public void init() { + initRefreshThread(); + } + /** - * 结束定时任务,不再定时清理过期数据 + * 组件被卸载时,结束定时任务,不再定时清理过期数据 */ @Override - public void onChange() { + public void destroy() { this.refreshFlag = false; } }