spring boot整合redis,实现shiro的CacheManager
接着上一篇博客来讲:Spring Boot整合jpa,Shiro进行权限管理
Shiro默认整合了EhCache,来实现缓存,如果我们想用redis替换EhCache来实现缓存怎么做了?我们可以从Shiro的源码来找到一些端倪。我们可以模拟EhCacheManager的实现方式,EhCacheManager类定义如下:
public class EhCacheManager implements CacheManager, Initializable, Destroyable {}
我们从上面的代码可以看到,最终要的是实现了CacheManager接口,该接口很简单,只有一个方法:
public interface CacheManager {/*** Acquires the cache with the specified name. If a cache does not yet exist with that name, a new one* will be created with that name and returned.** @param name the name of the cache to acquire.* @return the Cache with the given name* @throws CacheException if there is an error acquiring the Cache instance.*/public Cache getCache(String name) throws CacheException;
}
从上面的注释中,我们可以发现,这个接口需要一个Cache,通过name来获取Cache,首先,我们来实现CacheManager这个接口
@Service
public class RedisCacheManager implements CacheManager {@Autowiredprivate RedisTemplate redisTemplate; // RedisTemplate,如果不明白怎么使用的,请参考http://blog.csdn.net/liuchuanhong1/article/details/54601037@Overridepublic Cache getCache(String name) throws CacheException {System.out.println("name:"+name);return new RedisCache(120, redisTemplate);// 为了简化代码的编写,此处直接new一个Cache}}
package com.chhliu.springboot.shiro.cache;import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.data.redis.core.RedisTemplate;public class RedisCache implements Cache {private long expireTime = 120;// 缓存的超时时间,单位为sprivate RedisTemplate redisTemplate;// 通过构造方法注入该对象public RedisCache() {super();}public RedisCache(long expireTime, RedisTemplate redisTemplate) {super();this.expireTime = expireTime;this.redisTemplate = redisTemplate;}/*** 通过key来获取对应的缓存对象* 通过源码我们可以发现,shiro需要的key的类型为Object,V的类型为AuthorizationInfo对象*/@Overridepublic V get(K key) throws CacheException {return redisTemplate.opsForValue().get(key);}/*** 将权限信息加入缓存中*/@Overridepublic V put(K key, V value) throws CacheException {redisTemplate.opsForValue().set(key, value, this.expireTime, TimeUnit.SECONDS);return value;}/*** 将权限信息从缓存中删除*/@Overridepublic V remove(K key) throws CacheException {V v = redisTemplate.opsForValue().get(key);redisTemplate.opsForValue().getOperations().delete(key);return v;}@Overridepublic void clear() throws CacheException {}@Overridepublic int size() {return 0;}@Overridepublic Set keys() {return null;}@Overridepublic Collection values() {return null;}}
这两步完成之后,就是需要将原来的EhCacheManager的配置换成RedisCacheManager了
@Beanpublic DefaultWebSessionManager configWebSessionManager(){DefaultWebSessionManager manager = new DefaultWebSessionManager();manager.setCacheManager(cacheManager);// 换成Redis的缓存管理器manager.setSessionDAO(sessionDao);manager.setDeleteInvalidSessions(true);manager.setGlobalSessionTimeout(sessionDao.getExpireTime());manager.setSessionValidationSchedulerEnabled(true);return manager;}
通过上面的几步,我们就实现了用Redis来缓存Shiro的权限等相关信息
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
