首页 / REDIS / springboot集成redis
springboot集成redis
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了springboot集成redis,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2127字,纯文字阅读大概需要4分钟。
内容图文
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.配置好了写简单测试类
原文:https://www.cnblogs.com/zhangzhiqin/p/11922500.html
内容总结
以上是互联网集市为您收集整理的springboot集成redis全部内容,希望文章能够帮你解决springboot集成redis所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。