From aa252f831c9619ca0acb5a9ef32eb661ab5d2f9a Mon Sep 17 00:00:00 2001 From: noear Date: Wed, 30 Mar 2022 22:59:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20sa-token-dao-redisx=20?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=EF=BC=8C=E7=94=A8=E4=BA=8E=E9=80=82=E9=85=8D?= =?UTF-8?q?redisx=E6=A1=86=E6=9E=B6=EF=BC=88=E6=97=A0=20spring=20=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sa-token-plugin/sa-token-dao-redisx/pom.xml | 41 ++++ .../dev33/satoken/dao/SaTokenDaoOfRedis.java | 185 ++++++++++++++++++ .../src/test/java/demo/App.java | 12 ++ .../src/test/java/demo/Config.java | 19 ++ .../src/test/resources/app.yml | 10 + 5 files changed, 267 insertions(+) create mode 100644 sa-token-plugin/sa-token-dao-redisx/pom.xml create mode 100644 sa-token-plugin/sa-token-dao-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedis.java create mode 100644 sa-token-plugin/sa-token-dao-redisx/src/test/java/demo/App.java create mode 100644 sa-token-plugin/sa-token-dao-redisx/src/test/java/demo/Config.java create mode 100644 sa-token-plugin/sa-token-dao-redisx/src/test/resources/app.yml diff --git a/sa-token-plugin/sa-token-dao-redisx/pom.xml b/sa-token-plugin/sa-token-dao-redisx/pom.xml new file mode 100644 index 00000000..9dfffe5d --- /dev/null +++ b/sa-token-plugin/sa-token-dao-redisx/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + + cn.dev33 + sa-token-plugin + 1.29.1.trial + + jar + + sa-token-dao-redisx + sa-token-dao-redisx + sa-token integrate redis + + + + + cn.dev33 + sa-token-core + ${sa-token-version} + + + + org.noear + redisx + 1.3.4 + + + + org.noear + solon-test + 1.6.34 + + + + + + diff --git a/sa-token-plugin/sa-token-dao-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedis.java b/sa-token-plugin/sa-token-dao-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedis.java new file mode 100644 index 00000000..34572a56 --- /dev/null +++ b/sa-token-plugin/sa-token-dao-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedis.java @@ -0,0 +1,185 @@ +package cn.dev33.satoken.dao; + +import cn.dev33.satoken.util.SaFoxUtil; +import org.noear.redisx.RedisClient; +import org.noear.redisx.plus.RedisBucket; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +/** + * SaTokenDao 的 redis 适配(相对于之前的 redis 适配;主要去除去 Spring 的依赖) + * + * @author noear + * @since 2022-03-30 + */ +public class SaTokenDaoOfRedis implements SaTokenDao { + private final RedisBucket redisBucket; + + public SaTokenDaoOfRedis(Properties props) { + this(new RedisClient(props)); + } + + public SaTokenDaoOfRedis(RedisClient redisClient) { + redisBucket = redisClient.getBucket(); + } + + + /** + * 获取Value,如无返空 + */ + @Override + public String get(String key) { + return redisBucket.get(key); + } + + /** + * 写入Value,并设定存活时间 (单位: 秒) + */ + @Override + public void set(String key, String value, long timeout) { + if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) { + return; + } + // 判断是否为永不过期 + if (timeout == SaTokenDao.NEVER_EXPIRE) { + redisBucket.store(key, value, (int) SaTokenDao.NEVER_EXPIRE); + } else { + redisBucket.store(key, value, (int) timeout); + } + } + + /** + * 修改指定key-value键值对 (过期时间不变) + */ + @Override + public void update(String key, String value) { + long expire = getTimeout(key); + // -2 = 无此键 + if (expire == SaTokenDao.NOT_VALUE_EXPIRE) { + return; + } + this.set(key, value, expire); + } + + /** + * 删除Value + */ + @Override + public void delete(String key) { + redisBucket.remove(key); + } + + /** + * 获取Value的剩余存活时间 (单位: 秒) + */ + @Override + public long getTimeout(String key) { + return redisBucket.ttl(key); + } + + /** + * 修改Value的剩余存活时间 (单位: 秒) + */ + @Override + public void updateTimeout(String key, long timeout) { + // 判断是否想要设置为永久 + if (timeout == SaTokenDao.NEVER_EXPIRE) { + long expire = getTimeout(key); + if (expire == SaTokenDao.NEVER_EXPIRE) { + // 如果其已经被设置为永久,则不作任何处理 + } else { + // 如果尚未被设置为永久,那么再次set一次 + this.set(key, this.get(key), timeout); + } + return; + } + redisBucket.delay(key, (int) timeout); + } + + + /** + * 获取Object,如无返空 + */ + @Override + public Object getObject(String key) { + return redisBucket.getAndDeserialize(key); + } + + /** + * 写入Object,并设定存活时间 (单位: 秒) + */ + @Override + public void setObject(String key, Object object, long timeout) { + if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) { + return; + } + // 判断是否为永不过期 + if (timeout == SaTokenDao.NEVER_EXPIRE) { + redisBucket.storeAndSerialize(key, object); + } else { + redisBucket.storeAndSerialize(key, object, (int) timeout); + } + } + + /** + * 更新Object (过期时间不变) + */ + @Override + public void updateObject(String key, Object object) { + long expire = getObjectTimeout(key); + // -2 = 无此键 + if (expire == SaTokenDao.NOT_VALUE_EXPIRE) { + return; + } + this.setObject(key, object, expire); + } + + /** + * 删除Object + */ + @Override + public void deleteObject(String key) { + redisBucket.remove(key); + } + + /** + * 获取Object的剩余存活时间 (单位: 秒) + */ + @Override + public long getObjectTimeout(String key) { + return redisBucket.ttl(key); + } + + /** + * 修改Object的剩余存活时间 (单位: 秒) + */ + @Override + public void updateObjectTimeout(String key, long timeout) { + // 判断是否想要设置为永久 + if (timeout == SaTokenDao.NEVER_EXPIRE) { + long expire = getObjectTimeout(key); + if (expire == SaTokenDao.NEVER_EXPIRE) { + // 如果其已经被设置为永久,则不作任何处理 + } else { + // 如果尚未被设置为永久,那么再次set一次 + this.setObject(key, this.getObject(key), timeout); + } + return; + } + redisBucket.delay(key, (int) timeout); + } + + + /** + * 搜索数据 + */ + @Override + public List searchData(String prefix, String keyword, int start, int size) { + Set keys = redisBucket.keys(prefix + "*" + keyword + "*"); + List list = new ArrayList(keys); + return SaFoxUtil.searchList(list, start, size); + } +} \ No newline at end of file diff --git a/sa-token-plugin/sa-token-dao-redisx/src/test/java/demo/App.java b/sa-token-plugin/sa-token-dao-redisx/src/test/java/demo/App.java new file mode 100644 index 00000000..ac68f579 --- /dev/null +++ b/sa-token-plugin/sa-token-dao-redisx/src/test/java/demo/App.java @@ -0,0 +1,12 @@ +package demo; + +import org.noear.solon.Solon; + +/** + * @author noear 2022/3/30 created + */ +public class App { + public static void main(String[] args) { + Solon.start(App.class, args); + } +} diff --git a/sa-token-plugin/sa-token-dao-redisx/src/test/java/demo/Config.java b/sa-token-plugin/sa-token-dao-redisx/src/test/java/demo/Config.java new file mode 100644 index 00000000..71535445 --- /dev/null +++ b/sa-token-plugin/sa-token-dao-redisx/src/test/java/demo/Config.java @@ -0,0 +1,19 @@ +package demo; + +import cn.dev33.satoken.SaManager; +import cn.dev33.satoken.dao.SaTokenDaoOfRedis; +import org.noear.solon.annotation.Bean; +import org.noear.solon.annotation.Configuration; +import org.noear.solon.annotation.Inject; + +/** + * @author noear 2022/3/30 created + */ +@Configuration +public class Config { + @Bean + public void saTokenDaoInit(@Inject("${sa-token-dao.redis}") SaTokenDaoOfRedis saTokenDao) { + //手动操作,可适用于任何框架 + SaManager.setSaTokenDao(saTokenDao); + } +} diff --git a/sa-token-plugin/sa-token-dao-redisx/src/test/resources/app.yml b/sa-token-plugin/sa-token-dao-redisx/src/test/resources/app.yml new file mode 100644 index 00000000..3e45af02 --- /dev/null +++ b/sa-token-plugin/sa-token-dao-redisx/src/test/resources/app.yml @@ -0,0 +1,10 @@ + + + + +sa-token-dao: #名字可以随意取 + redis: + server: "localhost:6379" + password: 123456 + db: 1 + maxTotal: 200