feat: 新增 sa-token-caffeine 插件

This commit is contained in:
click33
2025-03-08 20:25:37 +08:00
parent 469d387f8a
commit 9095ea3851
20 changed files with 581 additions and 14 deletions

View File

@@ -17,20 +17,24 @@ package cn.dev33.satoken.dao;
import cn.dev33.satoken.dao.auto.SaTokenDaoByStringFollowObject;
import cn.dev33.satoken.dao.map.SaMapPackageForConcurrentHashMap;
import cn.dev33.satoken.dao.timedcache.SaTimedCache;
import cn.dev33.satoken.util.SaFoxUtil;
import java.util.List;
/**
* Sa-Token 持久层接口,默认实现类,基于 SaTimedCache (内存 Map,系统重启后数据丢失)
* Sa-Token 持久层接口,默认实现类,基于 SaTimedCache - ConcurrentHashMap (内存缓存,系统重启后数据丢失)
*
* @author click33
* @since 1.10.0
*/
public class SaTokenDaoDefaultImpl implements SaTokenDaoByStringFollowObject {
public SaTimedCache timedCache = new SaTimedCache();
public SaTimedCache timedCache = new SaTimedCache(
new SaMapPackageForConcurrentHashMap<>(),
new SaMapPackageForConcurrentHashMap<>()
);
// ------------------------ Object 读写操作
@@ -75,7 +79,7 @@ public class SaTokenDaoDefaultImpl implements SaTokenDaoByStringFollowObject {
@Override
public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
return SaFoxUtil.searchList(timedCache.expireMap.keySet(), prefix, keyword, start, size, sortType);
return SaFoxUtil.searchList(timedCache.keySet(), prefix, keyword, start, size, sortType);
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.dao.map;
import java.util.Set;
/**
* Map 包装类
*
* @author click33
* @since 1.41.0
*/
public interface SaMapPackage<V> {
/**
* 获取底层被包装的源对象
*
* @return /
*/
Object getSource();
/**
* 读
*
* @param key /
* @return /
*/
V get(String key);
/**
* 写
*
* @param key /
* @param value /
*/
void put(String key, V value);
/**
* 删
* @param key /
*/
void remove(String key);
/**
* 所有 key
*/
Set<String> keySet();
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.dao.map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* Map 包装类 (ConcurrentHashMap 版)
*
* @author click33
* @since 1.41.0
*/
public class SaMapPackageForConcurrentHashMap<V> implements SaMapPackage<V> {
private final ConcurrentHashMap<String, V> map = new ConcurrentHashMap<String, V>();
@Override
public Object getSource() {
return map;
}
@Override
public V get(String key) {
return map.get(key);
}
@Override
public void put(String key, V value) {
map.put(key, value);
}
@Override
public void remove(String key) {
map.remove(key);
}
@Override
public Set<String> keySet() {
return map.keySet();
}
}

View File

@@ -18,9 +18,9 @@ package cn.dev33.satoken.dao.timedcache;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.map.SaMapPackage;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Set;
/**
* 一个定时缓存的简单实现,采用:惰性检查 + 异步循环扫描
@@ -33,12 +33,17 @@ public class SaTimedCache {
/**
* 存储数据的集合
*/
public Map<String, Object> dataMap = new ConcurrentHashMap<>();
public SaMapPackage<Object> dataMap;
/**
* 存储数据过期时间的集合(单位: 毫秒), 记录所有 key 的到期时间 (注意存储的是到期时间,不是剩余存活时间)
*/
public Map<String, Long> expireMap = new ConcurrentHashMap<>();
public SaMapPackage<Long> expireMap;
public SaTimedCache(SaMapPackage<Object> dataMap, SaMapPackage<Long> expireMap) {
this.dataMap = dataMap;
this.expireMap = expireMap;
}
// ------------------------ 基础 API 读写操作
@@ -76,6 +81,10 @@ public class SaTimedCache {
expireMap.put(key, (timeout == SaTokenDao.NEVER_EXPIRE) ? (SaTokenDao.NEVER_EXPIRE) : (System.currentTimeMillis() + timeout * 1000));
}
public Set<String> keySet() {
return dataMap.keySet();
}
// --------- 过期时间相关操作