在SpringBoot中Shiro缓存使用Redis、Ehcache实现的两种方式实例
SpringBoot 中配置redis作为session 缓存器。 让shiro引用
本文是建立在你是使用这shiro基础之上的补充内容
第一种:Redis缓存,将数据存储到redis 并且开启session存入redis中。 引入pom <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> 配置redisConfig @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } @Bean //在这里配置缓存reids配置 public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofHours(1)); // 设置缓存有效期一小时 System.out.println("《========【开启redis】 ======== 》 "); return RedisCacheManager .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)) .cacheDefaults(redisCacheConfiguration).build(); } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } } 配置自定义缓存管理器,引入redis缓存管理器定义自己的CacheManager
/** * <p> 自定义cacheManage 扩张shiro里面的缓存 使用reids作缓存 </p> * <description> * 引入自己定义的CacheManager * 关于CacheManager的配置文件在spring-redis-cache.xml中 * </description> */ @Component public class ShiroSpringCacheManager implements CacheManager ,Destroyable{ /** * 将之上的RedisCacheManager的Bean拿出来 注入于此 */ @Autowired private org.springframework.cache.CacheManager cacheManager; public org.springframework.cache.CacheManager getCacheManager() { return cacheManager; } public void setCacheManager(org.springframework.cache.CacheManager cacheManager) { this.cacheManager = cacheManager; } @Override public void destroy() throws Exception { cacheManager = null; } @Override public <K, V> Cache<K, V> getCache(String name) { if (name == null ){ return null; } // 新建一个ShiroSpringCache 将Bean放入并实例化 return new ShiroSpringCache<K,V>(name,getCacheManager()); } }定义自己实现的Shiro的Cache,实现了Shiro包里的Cache
/** * <p> 自定义缓存 将数据存入到redis中 </p> */ @SuppressWarnings("unchecked") public class ShiroSpringCache<K,V> implements org.apache.shiro.cache.Cache<K, V>{ private static final Logger log = LoggerFactory.getLogger(ShiroSpringCache.class); private CacheManager cacheManager; private Cache cache; public ShiroSpringCache(String name, CacheManager cacheManager) { if(name==null || cacheManager==null){ throw new IllegalArgumentException("cacheManager or CacheName cannot be null."); } this.cacheManager = cacheManager; //这里首先是从父类中获取这个cache,如果没有会创建一个redisCache,初始化这个redisCache的时候 //会设置它的过期时间如果没有配置过这个缓存的,那么默认的缓存时间是为0的,如果配置了,就会把配置的时间赋予给这个RedisCache //如果从缓存的过期时间为0,就表示这个RedisCache不存在了,这个redisCache实现了spring中的cache this.cache= cacheManager.getCache(name); } @Override public V get(K key) throws CacheException { log.info("从缓存中获取key为{}的缓存信息",key); if(key == null){ return null; } ValueWrapper valueWrapper = cache.get(key); if(valueWrapper==null){ return null; } return (V) valueWrapper.get(); } @Override public V put(K key, V value) throws CacheException { log.info("创建新的缓存,信息为:{}={}",key,value); cache.put(key, value); return get(key); } @Override public V remove(K key) throws CacheException { log.info("干掉key为{}的缓存",key); V v = get(key); cache.evict(key);//干掉这个名字为key的缓存 return v; } @Override public void clear() throws CacheException { log.info("清空所有的缓存"); cache.clear(); } @Override public int size() { return cacheManager.getCacheNames().size(); } /** * 获取缓存中所的key值 */ @Override public Set<K> keys() { return (Set<K>) cacheManager.getCacheNames(); } /** * 获取缓存中所有的values值 */ @Override public Collection<V> values() { return (Collection<V>) cache.get(cacheManager.getCacheNames()).get(); } @Override public String toString() { return "ShiroSpringCache [cache=" + cache + "]"; } }到此为止,使用redis做缓存,和spring的集成就完成了。