分享

spring boot整合redis,实现shiro的CacheManager

 WindySky 2017-10-25 发布于广东

接着上一篇博客来讲:Spring Boot整合jpa,Shiro进行权限管理

Shiro默认整合了EhCache,来实现缓存,如果我们想用redis替换EhCache来实现缓存怎么做了?我们可以从Shiro的源码来找到一些端倪。我们可以模拟EhCacheManager的实现方式,EhCacheManager类定义如下:

  1. public class EhCacheManager implements CacheManager, Initializable, Destroyable {  
  2.   
  3. }  

我们从上面的代码可以看到,最终要的是实现了CacheManager接口,该接口很简单,只有一个方法:

  1. public interface CacheManager {  
  2.   
  3.     /** 
  4.      * Acquires the cache with the specified <code>name</code>.  If a cache does not yet exist with that name, a new one 
  5.      * will be created with that name and returned. 
  6.      * 
  7.      * @param name the name of the cache to acquire. 
  8.      * @return the Cache with the given name 
  9.      * @throws CacheException if there is an error acquiring the Cache instance. 
  10.      */  
  11.     public <K, V> Cache<K, V> getCache(String name) throws CacheException;  
  12. }  

从上面的注释中,我们可以发现,这个接口需要一个Cache,通过name来获取Cache,首先,我们来实现CacheManager这个接口

  1. @Service  
  2. public class RedisCacheManager implements CacheManager {  
  3.       
  4.     @Autowired  
  5.     private RedisTemplate redisTemplate; // RedisTemplate,如果不明白怎么使用的,请参考http://blog.csdn.net/liuchuanhong1/article/details/54601037  
  6.       
  7.     @Override  
  8.     public <K, V> Cache<K, V> getCache(String name) throws CacheException {  
  9.         System.out.println("name:"+name);  
  10.         return new RedisCache<K, V>(120, redisTemplate);// 为了简化代码的编写,此处直接new一个Cache  
  11.     }  
  12.   
  13. }  


下面,我们来看下这个Cache怎么写

  1. package com.chhliu.springboot.shiro.cache;  
  2.   
  3. import java.util.Collection;  
  4. import java.util.Set;  
  5. import java.util.concurrent.TimeUnit;  
  6.   
  7. import org.apache.shiro.cache.Cache;  
  8. import org.apache.shiro.cache.CacheException;  
  9. import org.springframework.data.redis.core.RedisTemplate;  
  10.   
  11. public class RedisCache<K, V> implements Cache<K, V> {  
  12.       
  13.     private long expireTime = 120;// 缓存的超时时间,单位为s  
  14.       
  15.     private RedisTemplate<K, V> redisTemplate;// 通过构造方法注入该对象  
  16.       
  17.     public RedisCache() {  
  18.         super();  
  19.     }  
  20.   
  21.     public RedisCache(long expireTime, RedisTemplate<K, V> redisTemplate) {  
  22.         super();  
  23.         this.expireTime = expireTime;  
  24.         this.redisTemplate = redisTemplate;  
  25.     }  
  26.   
  27.     /** 
  28.      * 通过key来获取对应的缓存对象 
  29.      * 通过源码我们可以发现,shiro需要的key的类型为Object,V的类型为AuthorizationInfo对象 
  30.      */  
  31.     @Override  
  32.     public V get(K key) throws CacheException {  
  33.         return redisTemplate.opsForValue().get(key);  
  34.     }  
  35.   
  36.     /** 
  37.      * 将权限信息加入缓存中 
  38.      */  
  39.     @Override  
  40.     public V put(K key, V value) throws CacheException {  
  41.         redisTemplate.opsForValue().set(key, value, this.expireTime, TimeUnit.SECONDS);  
  42.         return value;  
  43.     }  
  44.   
  45.     /** 
  46.      * 将权限信息从缓存中删除 
  47.      */  
  48.     @Override  
  49.     public V remove(K key) throws CacheException {  
  50.         V v = redisTemplate.opsForValue().get(key);  
  51.         redisTemplate.opsForValue().getOperations().delete(key);  
  52.         return v;  
  53.     }  
  54.   
  55.     @Override  
  56.     public void clear() throws CacheException {  
  57.           
  58.     }  
  59.   
  60.     @Override  
  61.     public int size() {  
  62.         return 0;  
  63.     }  
  64.   
  65.     @Override  
  66.     public Set<K> keys() {  
  67.         return null;  
  68.     }  
  69.   
  70.     @Override  
  71.     public Collection<V> values() {  
  72.         return null;  
  73.     }  
  74.   
  75. }  

这两步完成之后,就是需要将原来的EhCacheManager的配置换成RedisCacheManager了

  1. @Bean  
  2.     public DefaultWebSessionManager configWebSessionManager(){  
  3.         DefaultWebSessionManager manager = new DefaultWebSessionManager();  
  4.         manager.setCacheManager(cacheManager);// 换成Redis的缓存管理器  
  5.         manager.setSessionDAO(sessionDao);  
  6.         manager.setDeleteInvalidSessions(true);  
  7.         manager.setGlobalSessionTimeout(sessionDao.getExpireTime());  
  8.         manager.setSessionValidationSchedulerEnabled(true);  
  9.           
  10.         return manager;  
  11.     }  

通过上面的几步,我们就实现了用Redis来缓存Shiro的权限等相关信息





    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多