ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

shiro使用redis作为缓存

2022-08-06 15:31:10  阅读:169  来源: 互联网

标签:缓存 redis shiro org import public redisTemplate


shiro使用redis作为缓存

应用场景:Shiro为每个用户的角色和权限信息提供缓存支持,通过Shiro自己定义的CacheManager实现,默认实现有Ehcache和内存(就是一个Map结构),在应用中通常使用redis作为缓存服务器,因此使用redis来作为shiro的缓存。

优缺点:一般来说缓存放在本地,通过本地内存进行缓存速度更快,但是在分布式环境下,修改了用户权限需要进行同步。将缓存和Session放在统一的地方进行管理可以解决,但是网络请求延迟较高,且容易造成瞬间高并发。 代码分析:

1、实现AbstractCacheManager:

 import org.apache.shiro.cache.AbstractCacheManager;
 import org.apache.shiro.cache.Cache;
 import org.apache.shiro.cache.CacheException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 ​
 /**
  * @author: yykk
  **/
 @Component
 public class MyCacheManager extends AbstractCacheManager {
     @Autowired
     private MCahce mCahce;
     @Override
     protected Cache createCache(String name) throws CacheException {
         return mCahce;
     }
 }

2、实现Shiro定义的Cache接口

 import lombok.extern.slf4j.Slf4j;
 import org.apache.shiro.cache.Cache;
 import org.apache.shiro.cache.CacheException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 ​
 import java.util.Collection;
 import java.util.Set;
 ​
 /**
  * @author: yykk
  **/
 @Slf4j
 @Component
 public class MCahce<K,V> implements Cache<K,V> {
     //redis key
     private final K prefix = (K)"shiro_cache";
 ​
     @Autowired
     private RedisTemplate redisTemplate;
 ​
     public RedisTemplate getRedisTemplate() {
         return redisTemplate;
     }
 ​
     public void setRedisTemplate(RedisTemplate redisTemplate) {
         this.redisTemplate = redisTemplate;
     }
 ​
     //------------------------
     @Override
     public V get(K key) throws CacheException {
         Object o = redisTemplate.opsForHash().get(prefix,key);
         return o == null ? null : (V) o;
     }
 ​
     @Override
     public V put(K key, V value) throws CacheException {
         V old = this.get(key);
         redisTemplate.opsForHash().put(prefix,key,value);
         return old;
     }
 ​
     @Override
     public V remove(K key) throws CacheException {
         V old = this.get(key);
         redisTemplate.opsForHash().delete(prefix,key);
         return old;
     }
 ​
     @Override
     public void clear() throws CacheException {
         redisTemplate.delete(prefix);
     }
 ​
     @Override
     public int size() {
         return  redisTemplate.opsForHash().size(prefix).intValue();
     }
 ​
     @Override
     public Set<K> keys() {
         return redisTemplate.opsForHash().keys(prefix);
     }
 ​
     @Override
     public Collection<V> values() {
         return redisTemplate.opsForHash().values(prefix);
     }
 }

3、配置shiro注入缓存管理器:

 @bean
 public AuthorizingRealm myrealm(CacheManager cacheManager)
     {
         log.info("myrealm()");
         AuthorizingRealm realm = new MyRealm();
         realm.setCachingEnabled(true);  //开启缓存
         realm.setCacheManager(cacheManager); //注入缓存管理器
         return realm;
     }
 ​

4、Redis序列化

  • jdk序列化

    •  new  JdkSerializationRedisSerializer()
  • json序列化

    •  GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();

       

标签:缓存,redis,shiro,org,import,public,redisTemplate
来源: https://www.cnblogs.com/zhaostudy/p/16557158.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有