mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-10-21 02:57:23 +08:00
feat: 新增 sa-token-caffeine 插件
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
<module>sa-token-fastjson2</module>
|
||||
<module>sa-token-snack3</module>
|
||||
<module>sa-token-hutool-timed-cache</module>
|
||||
<module>sa-token-caffeine</module>
|
||||
<module>sa-token-thymeleaf</module>
|
||||
<module>sa-token-freemarker</module>
|
||||
<module>sa-token-dubbo</module>
|
||||
|
30
sa-token-plugin/sa-token-caffeine/pom.xml
Normal file
30
sa-token-plugin/sa-token-caffeine/pom.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-plugin</artifactId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sa-token-caffeine</name>
|
||||
<artifactId>sa-token-caffeine</artifactId>
|
||||
<description>sa-token integrate Caffeine</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import cn.dev33.satoken.dao.map.SaMapPackage;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Map 包装类 (Caffeine 版)
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.41.0
|
||||
*/
|
||||
public class SaMapPackageForCaffeine<V> implements SaMapPackage<V> {
|
||||
|
||||
public Cache<String, V> cache = Caffeine.newBuilder()
|
||||
.expireAfterWrite(Long.MAX_VALUE, TimeUnit.SECONDS)
|
||||
.maximumSize(Integer.MAX_VALUE)
|
||||
.build();
|
||||
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读
|
||||
*
|
||||
* @param key /
|
||||
* @return /
|
||||
*/
|
||||
@Override
|
||||
public V get(String key) {
|
||||
return cache.getIfPresent(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写
|
||||
*
|
||||
* @param key /
|
||||
* @param value /
|
||||
*/
|
||||
@Override
|
||||
public void put(String key, V value) {
|
||||
cache.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删
|
||||
* @param key /
|
||||
*/
|
||||
@Override
|
||||
public void remove(String key) {
|
||||
cache.invalidate(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 所有 key
|
||||
*/
|
||||
@Override
|
||||
public Set<String> keySet() {
|
||||
return cache.asMap().keySet();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
|
||||
import cn.dev33.satoken.dao.auto.SaTokenDaoByStringFollowObject;
|
||||
import cn.dev33.satoken.dao.timedcache.SaTimedCache;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Sa-Token 持久层实现,基于 SaTimedCache - Caffeine (内存缓存,系统重启后数据丢失)
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.41.0
|
||||
*/
|
||||
public class SaTokenDaoForCaffeine implements SaTokenDaoByStringFollowObject {
|
||||
|
||||
public SaTimedCache timedCache = new SaTimedCache(
|
||||
new SaMapPackageForCaffeine<>(),
|
||||
new SaMapPackageForCaffeine<>()
|
||||
);
|
||||
|
||||
// ------------------------ Object 读写操作
|
||||
|
||||
@Override
|
||||
public Object getObject(String key) {
|
||||
return timedCache.getObject(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getObject(String key, Class<T> classType){
|
||||
return (T) getObject(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setObject(String key, Object object, long timeout) {
|
||||
timedCache.setObject(key, object, timeout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateObject(String key, Object object) {
|
||||
timedCache.updateObject(key, object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteObject(String key) {
|
||||
timedCache.deleteObject(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getObjectTimeout(String key) {
|
||||
return timedCache.getObjectTimeout(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateObjectTimeout(String key, long timeout) {
|
||||
timedCache.updateObjectTimeout(key, timeout);
|
||||
}
|
||||
|
||||
|
||||
// --------- 会话管理
|
||||
|
||||
@Override
|
||||
public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
|
||||
return SaFoxUtil.searchList(timedCache.keySet(), prefix, keyword, start, size, sortType);
|
||||
}
|
||||
|
||||
|
||||
// --------- 组件生命周期
|
||||
|
||||
/**
|
||||
* 组件被安装时,开始刷新数据线程
|
||||
*/
|
||||
@Override
|
||||
public void init() {
|
||||
timedCache.initRefreshThread();
|
||||
}
|
||||
|
||||
/**
|
||||
* 组件被卸载时,结束定时任务,不再定时清理过期数据
|
||||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
timedCache.endRefreshThread();
|
||||
}
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.plugin;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.dao.SaTokenDaoForCaffeine;
|
||||
|
||||
/**
|
||||
* SaToken 插件安装:DAO 扩展 - Caffeine 版
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.41.0
|
||||
*/
|
||||
public class SaTokenPluginForCaffeine implements SaTokenPlugin {
|
||||
|
||||
@Override
|
||||
public void install() {
|
||||
|
||||
SaManager.setSaTokenDao(new SaTokenDaoForCaffeine());
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1 @@
|
||||
cn.dev33.satoken.plugin.SaTokenPluginForCaffeine
|
Reference in New Issue
Block a user