加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

springboot基础五:集成redis

发布时间:2020-12-16 04:42:57 所属栏目:安全 来源:网络整理
导读:《springboot基础五:集成redis》要点: 本文介绍了springboot基础五:集成redis,希望对您有用。如果有疑问,可以联系我们。 前言 在项目里redis作为缓存已经是国际惯例了,那springboot项目里如何能与redis集成进行使用呢?照着教程做吧 配置 引入pom depend

《springboot基础五:集成redis》要点:
本文介绍了springboot基础五:集成redis,希望对您有用。如果有疑问,可以联系我们。

springboot基础五:集成redis

前言

在项目里redis作为缓存已经是国际惯例了,那springboot项目里如何能与redis集成进行使用呢?照着教程做吧

配置

  1. 引入pom

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-redis</artifactId>

<version>1.4.7.RELEASE</version>

</dependency>

  1. 创建RedisUtil类

@Configuration

public class RedisUtil {

@Bean

public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory) {

RedisTemplate<Object,Object> template = new RedisTemplate<>();

template.setConnectionFactory(connectionFactory);

//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值

Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper mapper = new ObjectMapper();

mapper.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);

mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

serializer.setObjectMapper(mapper);

template.setValueSerializer(serializer);

//使用StringRedisSerializer来序列化和反序列化redis的key值

template.setKeySerializer(new StringRedisSerializer());

template.afterPropertiesSet();

return template;

}

}

  1. application.properties加入配置信息

## Redis 配置

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

spring.redis.database=0

## Redis服务器地址

spring.redis.host=127.0.0.1

## Redis服务器连接端口

spring.redis.port=6379

## Redis服务器连接暗码(默认为空)

spring.redis.password=

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

spring.redis.pool.max-active=8

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

spring.redis.pool.max-wait=-1

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

spring.redis.pool.max-idle=8

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

spring.redis.pool.min-idle=0

## 连接超时时间(毫秒)

spring.redis.timeout=0

  1. 使用:

    在需要的类里注入RedisTemplate即可

@Autowired

private RedisTemplate redisTemplate;

springboot配置redis真的是简单至极,赶紧本身试试吧!

欢迎参与《springboot基础五:集成redis》讨论,分享您的想法,编程之家PHP学院为您提供专业教程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读