美文网首页
【SpringBoot】配置Redis

【SpringBoot】配置Redis

作者: 扮鬼之梦 | 来源:发表于2019-06-11 15:37 被阅读0次

    引入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    

    yml配置文件

    spring:
      redis:
        database: 0 # Redis数据库索引(默认为0)
        host: 192.168.40.128
        port: 6379
        jedis:
          pool:
            max-active: 30 # 连接池最大连接数(使用负值表示没有限制)
            max-idle: 60 # 连接池中的最大空闲连接
            min-idle: 20 # 连接池中的最小空闲连接
    

    注入Bean配置

    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    @Configuration
    public class RedisConfig extends CachingConfigurerSupport {
    
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            // 配置连接工厂
            template.setConnectionFactory(factory);
    
            //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
            Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
    
            ObjectMapper om = new ObjectMapper();
            // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jacksonSeial.setObjectMapper(om);
    
            // 值采用json序列化
            template.setValueSerializer(jacksonSeial);
            //使用StringRedisSerializer来序列化和反序列化redis的key值
            template.setKeySerializer(new StringRedisSerializer());
    
            // 设置hash key 和value序列化模式
            template.setHashKeySerializer(new StringRedisSerializer());
            template.setHashValueSerializer(jacksonSeial);
            template.afterPropertiesSet();
    
            return template;
        }
    }
    

    Redis工具类

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;
    
    @Component
    public class RedisUtils {
    
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
        
        
        /**
         * 读取缓存
         */
        public Object get(final String key) {
            return redisTemplate.opsForValue().get(key);
        }
    
        /**
         * 写入缓存
         */
        public boolean set(final String key, Object value) {
            boolean result = false;
            try {
                redisTemplate.opsForValue().set(key, value);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * 更新缓存
         */
        public boolean getAndSet(final String key, Object value) {
            boolean result = false;
            try {
                redisTemplate.opsForValue().getAndSet(key, value);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * 删除缓存
         */
        public boolean delete(final String key) {
            boolean result = false;
            try {
                redisTemplate.delete(key);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:【SpringBoot】配置Redis

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