| | |
| | | |
| | | import cn.dev33.satoken.dao.SaTokenDao; |
| | | import cn.dev33.satoken.util.SaFoxUtil; |
| | | import com.github.benmanes.caffeine.cache.Cache; |
| | | import com.github.benmanes.caffeine.cache.Caffeine; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * Sa-Token持久层接口(使用框架自带RedisUtils实现 协议统一) |
| | | * <p> |
| | | * 采用 caffeine + redis 多级缓存 优化并发查询效率 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class PlusSaTokenDao implements SaTokenDao { |
| | | |
| | | private static final Cache<String, Object> CAFFEINE = Caffeine.newBuilder() |
| | | // 设置最后一次写入或访问后经过固定时间过期 |
| | | .expireAfterWrite(5, TimeUnit.SECONDS) |
| | | // 初始的缓存空间大小 |
| | | .initialCapacity(100) |
| | | // 缓存的最大条数 |
| | | .maximumSize(1000) |
| | | .build(); |
| | | |
| | | /** |
| | | * 获取Value,如无返空 |
| | | */ |
| | | @Override |
| | | public String get(String key) { |
| | | return RedisUtils.getCacheObject(key); |
| | | Object o = CAFFEINE.get(key, k -> RedisUtils.getCacheObject(key)); |
| | | return (String) o; |
| | | } |
| | | |
| | | /** |
| | |
| | | if (timeout == NEVER_EXPIRE) { |
| | | RedisUtils.setCacheObject(key, value); |
| | | } else { |
| | | RedisUtils.setCacheObject(key, value, Duration.ofSeconds(timeout)); |
| | | if (RedisUtils.hasKey(key)) { |
| | | RedisUtils.setCacheObject(key, value, true); |
| | | } else { |
| | | RedisUtils.setCacheObject(key, value, Duration.ofSeconds(timeout)); |
| | | } |
| | | } |
| | | CAFFEINE.invalidate(key); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public void update(String key, String value) { |
| | | long expire = getTimeout(key); |
| | | // -2 = 无此键 |
| | | if (expire == NOT_VALUE_EXPIRE) { |
| | | return; |
| | | if (RedisUtils.hasKey(key)) { |
| | | RedisUtils.setCacheObject(key, value, true); |
| | | CAFFEINE.invalidate(key); |
| | | } |
| | | this.set(key, value, expire); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public void updateTimeout(String key, long timeout) { |
| | | // 判断是否想要设置为永久 |
| | | if (timeout == NEVER_EXPIRE) { |
| | | long expire = getTimeout(key); |
| | | if (expire == NEVER_EXPIRE) { |
| | | // 如果其已经被设置为永久,则不作任何处理 |
| | | } else { |
| | | // 如果尚未被设置为永久,那么再次set一次 |
| | | this.set(key, this.get(key), timeout); |
| | | } |
| | | return; |
| | | } |
| | | RedisUtils.expire(key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public Object getObject(String key) { |
| | | return RedisUtils.getCacheObject(key); |
| | | Object o = CAFFEINE.get(key, k -> RedisUtils.getCacheObject(key)); |
| | | return o; |
| | | } |
| | | |
| | | /** |
| | |
| | | if (timeout == NEVER_EXPIRE) { |
| | | RedisUtils.setCacheObject(key, object); |
| | | } else { |
| | | RedisUtils.setCacheObject(key, object, Duration.ofSeconds(timeout)); |
| | | if (RedisUtils.hasKey(key)) { |
| | | RedisUtils.setCacheObject(key, object, true); |
| | | } else { |
| | | RedisUtils.setCacheObject(key, object, Duration.ofSeconds(timeout)); |
| | | } |
| | | } |
| | | CAFFEINE.invalidate(key); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public void updateObject(String key, Object object) { |
| | | long expire = getObjectTimeout(key); |
| | | // -2 = 无此键 |
| | | if (expire == NOT_VALUE_EXPIRE) { |
| | | return; |
| | | if (RedisUtils.hasKey(key)) { |
| | | RedisUtils.setCacheObject(key, object, true); |
| | | CAFFEINE.invalidate(key); |
| | | } |
| | | this.setObject(key, object, expire); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public void updateObjectTimeout(String key, long timeout) { |
| | | // 判断是否想要设置为永久 |
| | | if (timeout == NEVER_EXPIRE) { |
| | | long expire = getObjectTimeout(key); |
| | | if (expire == NEVER_EXPIRE) { |
| | | // 如果其已经被设置为永久,则不作任何处理 |
| | | } else { |
| | | // 如果尚未被设置为永久,那么再次set一次 |
| | | this.setObject(key, this.getObject(key), timeout); |
| | | } |
| | | return; |
| | | } |
| | | RedisUtils.expire(key, Duration.ofSeconds(timeout)); |
| | | } |
| | | |
| | |
| | | /** |
| | | * 搜索数据 |
| | | */ |
| | | @SuppressWarnings("unchecked") |
| | | @Override |
| | | public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) { |
| | | Collection<String> keys = RedisUtils.keys(prefix + "*" + keyword + "*"); |
| | | List<String> list = new ArrayList<>(keys); |
| | | return SaFoxUtil.searchList(list, start, size, sortType); |
| | | String keyStr = prefix + "*" + keyword + "*"; |
| | | return (List<String>) CAFFEINE.get(keyStr, k -> { |
| | | Collection<String> keys = RedisUtils.keys(keyStr); |
| | | List<String> list = new ArrayList<>(keys); |
| | | return SaFoxUtil.searchList(list, start, size, sortType); |
| | | }); |
| | | } |
| | | } |