美文网首页
SpringBoot Redis

SpringBoot Redis

作者: 茶树丶ccha | 来源:发表于2018-07-04 15:30 被阅读0次

    SpringCache

    https://www.cnblogs.com/niechen/p/8760139.html

    • Cache:该接口定义了操作缓存的最基本行为
    package org.springframework.cache;  
    public interface Cache {  
        String getName();  //缓存的名字  
        Object getNativeCache(); //得到底层使用的缓存,如Ehcache  
        ValueWrapper get(Object key); //根据key得到一个ValueWrapper,然后调用其get方法获取值  
        <T> T get(Object key, Class<T> type);//根据key,和value的类型直接获取value  
        void put(Object key, Object value);//往缓存放数据  
        void evict(Object key);//从缓存中移除key对应的缓存  
        void clear(); //清空缓存  
        interface ValueWrapper { //缓存值的Wrapper  
            Object get(); //得到真实的value  
        }  
    }
    
    • CacheManager:该接口主要作用是获取缓存和获取缓存名称(因为我们在应用中并不是使用一个Cache,而是多个,因此Spring提供了CacheManager抽象,用于缓存的管理)
    /**
      *默认提供的实现: 
      *ConcurrentMapCacheManager/ConcurrentMapCacheFactoryBean:管理ConcurrentMapCache;
      *GuavaCacheManager;
      *EhCacheCacheManager/EhCacheManagerFactoryBean;
      *JCacheCacheManager/JCacheManagerFactoryBean;
      *另外还提供了CompositeCacheManager用于组合CacheManager,即可以从多个CacheManager中轮询得到相应的Cache
      *
      *当我们调用cacheManager.getCache(cacheName) 时,会先从第一个cacheManager中查找有没有cacheName的cache,如果没有接着查找第二个,如果最后找不到,因为fallbackToNoOpCache=true,那么将返回一个NOP的Cache否则返回null。
      *除了GuavaCacheManager之外,其他Cache都支持Spring事务的,即如果事务回滚了,Cache的数据也会移除掉。 
      *Spring不进行Cache的缓存策略的维护,这些都是由底层Cache自己实现,Spring只是提供了一个Wrapper,提供一套对外一致的API。 
      */
    package org.springframework.cache;  
    import java.util.Collection;  
    public interface CacheManager {  
        Cache getCache(String name); //根据Cache名字获取Cache   
        Collection<String> getCacheNames(); //得到所有Cache的名字  
    }
    
    • @EnableCaching:用于开启缓存注解,通常需要和@Configuration使用
    • @CacheEvict 触发Cache接口中的evict方法,相当于移除key对应的值

    SpringBoot-2.0之前

    https://www.jianshu.com/p/5a70b13a4fa7
    要启用缓存支持,我们需要创建一个新的 CacheManager bean。CacheManager 接口有很多实现,本文演示的是和 Redis 的集成,自然就是用 RedisCacheManager 了。Redis 不是应用的共享内存,它只是一个内存服务器,就像 MySql 似的,我们需要将应用连接到它并使用某种“语言”进行交互,因此我们还需要一个连接工厂以及一个 Spring 和 Redis 对话要用的 RedisTemplate,这些都是 Redis 缓存所必需的配置,把它们都放在自定义的 CachingConfigurerSupport 中:

    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
     
    @Configuration
    @EnableCaching
    public class RedisCacheConfig extends CachingConfigurerSupport {
     
        @Bean
        public JedisConnectionFactory redisConnectionFactory() {
            JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
     
            // Defaults
            redisConnectionFactory.setHostName("192.168.1.166");
            redisConnectionFactory.setPort(6379);
            return redisConnectionFactory;
        }
     
        @Bean
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
            RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
            redisTemplate.setConnectionFactory(cf);
            return redisTemplate;
        }
     
        @Bean
        public CacheManager cacheManager(RedisTemplate redisTemplate) {
            RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
     
            // Number of seconds before expiration. Defaults to unlimited (0)
            cacheManager.setDefaultExpiration(3000); // Sets the default expire time (in seconds)
            return cacheManager;
        }
        
    }
    

    我们使用@EnableCaching注解来开启我们的项目支持缓存,并在配置类内添加了方法cacheManager(),方法的返回值则是使用了我们的Redis缓存的管理器,SpringBoot项目启动时就会去找自定义配置的CacheManager对象并且自动应用到项目中。

    SpringBoot-2.0

    https://www.jianshu.com/p/4ccd4512700f

    添加redis依赖并在application.properties中添加redis配置后,Spring Boot会在侦测到存在Redis的依赖并且Redis的配置是可用的情况下,使用RedisCacheManager初始化CacheManager。也就是说要使用缓存的话,SpringBoot就会选择Redis来作为缓存的容器。Spring会执行RedisAutoConfiguration,初始化RedisTemplate和StringRedisTemplate。然后RedisCacheConfiguration会将RedisAutoConfiguration生成的RedisTemplate注入方法生成RedisCacheManager 并进行了相关配置。(我们还可以对这个RedisCacheManager进行二次配置,这里只列出配置key的有效期

         /**
         * 重新配置RedisCacheManager
         * @param rd
         */
        @Autowired
        public void configRedisCacheManger(RedisCacheManager rd){
            rd.setDefaultExpiration(100L);
        }
    

    注意:

    请不要在applicaion.properties中配置: spring.cache.cache-names=book1,book2,否则会导致我们新的配置无法作用到这些配置的cache上。这是因为RedisCacheConfiguration 初始化RedisCacheManager后,会立即调用RedisCacheConfiguration 的初始化cache,而此时configRedisCacheManger还没有执行此方法,使得我们的配置无法启作用。反之,如果不配置,则后创建cache,会使用我们的配置。

    相关文章

      网友评论

          本文标题:SpringBoot Redis

          本文链接:https://www.haomeiwen.com/subject/wwdjuftx.html