Spring Boot Redis 配置和初步试用

1 ,在pom文件中引入

    org.springframework.boot

    spring-boot-starter-data-redis

2,配置文件

spring:

redis:

  # Redis数据库索引(默认为0)

    database: 0

    host: localhost

password:

    port: 6379

    pool:

      # 连接池最大连接数(使用负值表示没有限制)

      max-active: 8

      # 连接池中的最大空闲连接

      max-idle: 20

      # 连接池最大阻塞等待时间(使用负值表示没有限制)

      max-wait: -1

      # 连接池中的最小空闲连接

      min-idle: 0

    # 连接超时时间(毫秒)

    timeout: 0

3 config java 类

@Configuration

public class RedisConfig {

@Autowired

    private RedisConnectionFactoryredisConnectionFactory;

    @Bean

    public RedisTemplatefunctionDomainRedisTemplate() {

RedisTemplate redisTemplate =new RedisTemplate<>();

        initDomainRedisTemplate(redisTemplate, redisConnectionFactory);

        return redisTemplate;

    }

private void initDomainRedisTemplate(RedisTemplate redisTemplate, RedisConnectionFactory factory) {

redisTemplate.setKeySerializer(new StringRedisSerializer());

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        redisTemplate.setHashValueSerializer(new EntityRedisSerializer());

        redisTemplate.setValueSerializer(new EntityRedisSerializer());

        redisTemplate.setConnectionFactory(factory);

    }

/**

* 实例化HashOperations对象

*

    * @param redisTemplate

    * @return

    */

    @Bean

    public HashOperationshashOperations(RedisTemplate redisTemplate) {

return redisTemplate.opsForHash();

    }

/**

* 实例化ListOperations对象

    * @param redisTemplate

    * @return

    */

    @Bean

    public ListOperationslistOperations(RedisTemplate redisTemplate){

return redisTemplate.opsForList();

    }

}

public class EntityRedisSerializerimplements RedisSerializer {

@Override

    public byte[]serialize(Object o)throws SerializationException {

if(o ==null){

return new byte[0];

        }

return SerializerUtil.serialize(o);

    }

@Override

    public Objectdeserialize(byte[] bytes)throws SerializationException {

if(bytes ==null || bytes.length ==0){

return null;

        }

return SerializerUtil.unserialize(bytes);

    }

}

public class SerializerUtil {

public static byte[]serialize(Object object){

ObjectOutputStream oos =null;

        ByteArrayOutputStream baos =null;

        try {

baos =new ByteArrayOutputStream();

            oos =new ObjectOutputStream(baos);

            oos.writeObject(object);

            byte[] bytes = baos.toByteArray();

            return  bytes;

        }catch (IOException e) {

e.printStackTrace();

        }

return null;

    }

public static Objectunserialize(byte[] bytes){

ByteArrayInputStream bais =null;

        try {

bais =new ByteArrayInputStream(bytes);

            ObjectInputStream ois =new ObjectInputStream(bais);

            return ois.readObject();

        }catch (Exception e) {

e.printStackTrace();

        }

return null;

    }

}

4 在业务代码中使用

@Autowired

private RedisTemplateredisTemplate;

@Resource(name ="redisTemplate")

ValueOperationsvalueOperations;

valueOperations.set(key,value);

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 小编费力收集:给你想要的面试集合 1.C++或Java中的异常处理机制的简单原理和应用。 当JAVA程序违反了JA...
    八爷君阅读 4,681评论 1 114
  • JAVA面试题 1、作用域public,private,protected,以及不写时的区别答:区别如下:作用域 ...
    JA尐白阅读 1,188评论 1 0
  • 一次偶然的机会,在书城看到一本关于视觉化管理的书籍,当看到作者把抽象的管理事物变成可观赏的图案时,我惊叹不已。 突...
    良大师阅读 1,062评论 11 20
  • 我的大表哥终于要完婚了。十年以来,表哥把棒打鸳鸯、父母逼婚、相亲未遂到父母喜欢的我不爱、我喜欢的父母瞧不上等各种电...
    思小妞无后缀阅读 2,068评论 5 19