美文网首页
SpringBoot集成redis(2)

SpringBoot集成redis(2)

作者: 杨健kimyeung | 来源:发表于2020-08-20 08:57 被阅读0次

    一、 添加依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
      <!-- 1.5的版本默认采用的连接池技术是jedis  2.0以上版本默认连接池是lettuce, 在这里采用jedis,所以需要排除lettuce的jar -->
    </dependency>
    
    <!-- 将作为Redis对象序列化器 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>
    

    二、配置信息

    spring:
      cache:
        # 如果由底层的缓存管理器支持创建,以逗号分隔的列表来缓存名称
        cache-names: redisCache
        redis:
          # 是否允许redis缓存空值
          cache-null-values: true
          # redis的键前缀
          key-prefix: redisCache_
          # 缓存过期时间戳,设置为0不设置过期时间
          time-to-live: 600000ms
          # 是否启用redis的键前缀
          use-key-prefix: true
        # 缓存类型,在默认的情况下,redis会根据上下文自动探测
        type: redis
      redis:
        # Redis数据库索引(默认为0)
        database: 0
        # Redis服务器地址
        host: 127.0.0.1
        # Redis服务器连接端口
        port: 6379
        # Redis服务器连接密码(默认为空)
        password: ""
        # 连接超时时间(毫秒)
        timeout: 1000
        jedis:
          pool:
            # 连接池最大连接数(使用负值表示没有限制)
            max-active: 8
            # 连接池中的最大空闲连接
            max-idle: 8
            # 连接池最大阻塞等待时间(使用负值表示没有限制)
            max-wait: -1
            # 连接池中的最小空闲连接
            min-idle: 0
    
    

    三、配置类

    @Configuration
    public class RedisConfiguration {
        @Bean
        @Primary
        public FastJsonRedisSerializer<Object> fastJsonRedisSerializer() {
            return new FastJsonRedisSerializer<>(Object.class);
        }
    
        @Bean
        @Primary
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            // 开启事务支持(可选)
            redisTemplate.setEnableTransactionSupport(true);
            StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    
            redisTemplate.setKeySerializer(stringRedisSerializer);
            // 设置值的序列化方法
            redisTemplate.setValueSerializer(fastJsonRedisSerializer());
            // 注册到工厂中
            redisTemplate.setConnectionFactory(redisConnectionFactory);
    
            redisTemplate.setHashValueSerializer(fastJsonRedisSerializer());
            // 设置默认的序列化方法
            redisTemplate.setDefaultSerializer(stringRedisSerializer);
            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
    
        @Bean
        public RedisCacheManager customCacheManager(RedisConnectionFactory redisConnectionFactory) {
            //包装成SerializationPair类型
            RedisSerializationContext.SerializationPair serializationPair = RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer());
            //redis默认配置文件,并且设置过期时间
            RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(1));
            //设置序列化器
            redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(serializationPair);
            //RedisCacheManager 生成器创建
            RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(redisCacheConfiguration);
            return builder.build();
        }
    
        @Bean
        public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForValue();
        }
    
        @Bean
        public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForHash();
        }
    
        @Bean
        public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForList();
        }
    
        @Bean
        public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForSet();
        }
    
        @Bean
        public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
            return redisTemplate.opsForZSet();
        }
    }
    
    

    四、测试代码

    @SpringBootTest
    public class ValueOperationsTests {
        @Resource
        ValueOperations<String, Object> valueOperations;
    
        @Test
        void operationString() {
            valueOperations.set("hello", "world");
        }
    }    
    

    相关文章

      网友评论

          本文标题:SpringBoot集成redis(2)

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