Skip to content

Commit

Permalink
优化 SaTokenDao 组件更换时的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
click33 committed Oct 17, 2023
1 parent 0f7e1e9 commit 6977d3d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
5 changes: 4 additions & 1 deletion sa-token-core/src/main/java/cn/dev33/satoken/SaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 10 additions & 3 deletions sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,18 @@ default void updateSessionTimeout(String sessionId, long timeout) {
List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType);


// --------------------- 实例相关 ---------------------
// --------------------- 生命周期 ---------------------

/**
* 当 SaManager.saTokenDao 变更时,调用该方法
* 当此 SaTokenDao 实例被装载时触发
*/
default void onChange() {
default void init() {
}

/**
* 当此 SaTokenDao 实例被卸载时触发
*/
default void destroy() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ public class SaTokenDaoDefaultImpl implements SaTokenDao {
* 存储数据过期时间的集合(单位: 毫秒), 记录所有 key 的到期时间 (注意存储的是到期时间,不是剩余存活时间)
*/
public Map<String, Long> expireMap = new ConcurrentHashMap<>();

/**
* 构造函数
*/
public SaTokenDaoDefaultImpl() {
initRefreshThread();
}


// ------------------------ String 读写操作

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 6977d3d

Please sign in to comment.