| | |
| | | |
| | | import cn.dev33.satoken.dao.SaTokenDao; |
| | | import cn.dev33.satoken.util.SaFoxUtil; |
| | | import cn.hutool.core.lang.Console; |
| | | import com.github.benmanes.caffeine.cache.Cache; |
| | | import com.github.benmanes.caffeine.cache.Caffeine; |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | |
| | | |
| | | /** |
| | | * Sa-Token持久层接口(使用框架自带RedisUtils实现 协议统一) |
| | | * <p> |
| | | * 采用 caffeine + redis 多级缓存 优化并发查询效率 |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | |
| | | |
| | | private static final Cache<String, Object> CAFFEINE = Caffeine.newBuilder() |
| | | // 设置最后一次写入或访问后经过固定时间过期 |
| | | .expireAfterWrite(10, TimeUnit.SECONDS) |
| | | .expireAfterWrite(5, TimeUnit.SECONDS) |
| | | // 初始的缓存空间大小 |
| | | .initialCapacity(100) |
| | | // 缓存的最大条数 |
| | |
| | | @Override |
| | | public String get(String key) { |
| | | Object o = CAFFEINE.get(key, k -> RedisUtils.getCacheObject(key)); |
| | | Console.log("caffeine -> key:" + key + ",value:" + o); |
| | | 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.put(key, value); |
| | | CAFFEINE.invalidate(key); |
| | | } |
| | | |
| | | /** |
| | |
| | | public void update(String key, String value) { |
| | | if (RedisUtils.hasKey(key)) { |
| | | RedisUtils.setCacheObject(key, value, true); |
| | | CAFFEINE.put(key, value); |
| | | CAFFEINE.invalidate(key); |
| | | } |
| | | } |
| | | |
| | |
| | | @Override |
| | | public Object getObject(String key) { |
| | | Object o = CAFFEINE.get(key, k -> RedisUtils.getCacheObject(key)); |
| | | Console.log("caffeine -> key:" + key + ",value:" + o); |
| | | 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.put(key, object); |
| | | CAFFEINE.invalidate(key); |
| | | } |
| | | |
| | | /** |
| | |
| | | public void updateObject(String key, Object object) { |
| | | if (RedisUtils.hasKey(key)) { |
| | | RedisUtils.setCacheObject(key, object, true); |
| | | CAFFEINE.put(key, object); |
| | | CAFFEINE.invalidate(key); |
| | | } |
| | | } |
| | | |