码迷,mamicode.com
首页 > 编程语言 > 详细

springboot集成redis

时间:2019-11-24 15:46:46      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:red   connect   nal   nfa   public   hash   void   gre   超时   

1.配置文件如下

spring:
# 热部署
devtools:
restart:
enabled: true #设置开启热部署
additional-paths: src/main/java #重启目录
exclude: WEB-INF/**
freemarker:
cache: false #页面不加载缓存,修改即时生效
# redis
redis:
host: 192.168.0.6
port: 6479
timeout: 5000 # 连接超时时间(毫秒)
# password:
pool:
minIdle: 0 # 连接池中的最小空闲连接
maxIdle: 8 # 连接池中的最大空闲连接
maxWait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
maxActive: 8 # 连接池最大连接数(使用负值表示没有限制)

 2.引入data-redisjar包

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

3.写redis配置类

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {

@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
//使用fastjson序列化
FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
// value值的序列化采用fastJsonRedisSerializer
template.setValueSerializer(fastJsonRedisSerializer);
template.setHashValueSerializer(fastJsonRedisSerializer);
// key的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}

@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}

}

@GetMapping("/getdata")
public void getdata(){
//测试redis

redisUtil.set("name","小猫");
redisUtil.set("age", "11", 15);
System.out.println(redisUtil.getExpire("name"));
System.out.println( redisUtil.get("name"));
System.out.println(redisUtil.getExpire("age"));
System.out.println( redisUtil.get("age"));

}

4.配置好了写简单测试类

 

springboot集成redis

标签:red   connect   nal   nfa   public   hash   void   gre   超时   

原文地址:https://www.cnblogs.com/zhangzhiqin/p/11922500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!