Springboot2.x 集成redis
pom.xml 添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
yml 配置
spring:
redis:
#数据库索引
database: 0
host: 118.24.11.111
port: 6379
password: 123456
jedis:
pool:
#最大连接数
max-active: 8
#最大阻塞等待时间(负数表示没限制)
max-wait: -1
#最大空闲
max-idle: 8
#最小空闲
min-idle: 0
#连接超时时间
timeout: 10000
RedisTemplate 配置,重写key和value的序列化
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
// 配置redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer); // key序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化
redisTemplate.setHashKeySerializer(stringSerializer); // Hash key序列化
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash value序列化
redisTemplate.afterPropertiesSet();
return redisTemplate; } }
public interface RedisService {
void setObj(String key, Object obj, long timeout);
Object getObj(String key);
}
@Service("redisService")
public class RedisServiceImpl implements RedisService {
@Resource
private RedisTemplate redisTemplate;
@Override
public void setObj(final String key, Object obj, long timeout) {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, obj, timeout, TimeUnit.SECONDS);
}
@Override
public Object getObj(final String key) {
Object o = redisTemplate.opsForValue().get(key);
return o;
}
}
调用示例
@Override
public User selectByPrimaryKey(Integer id) throws Exception {
User user1 = (User)redisService.getObj("user" + id);
if(user1 == null){
User user = userMapper.selectByPrimaryKey(id);
redisService.setObj("user" + id, user, 1000*60*2);
return user;
}
return user1;
}
Springboot2.x 集成redis的更多相关文章
- Springboot2.x集成Redis集群模式
Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...
- Springboot2.x集成Redis哨兵模式
Springboot2.x集成Redis哨兵模式 说明 Redis哨兵模式是Redis高可用方案的一种实现方式,通过哨兵来自动实现故障转移,从而保证高可用. 准备条件 pom.xml中引入相关jar ...
- springboot2.X 集成redis+消息发布订阅
需求场景:分布式项目中,每个子项目有各自的 user 数据库, 在综合管理系统中存放这所有用户信息, 为了保持综合管理系统用户的完整性, 子系统添加用户后将用户信息以json格式保存至redis,然后 ...
- SpringBoot2.x集成Redis (StringTemplate与redisTemplate的用法)
1. Redis介绍Redis数据库是一个完全开源免费的高性能Key-Value数据库.它支持存储的value类型有五种,包括string(字符串).list(链表).set(集合).zset(sor ...
- SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理
一.Redis简介 Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elastic ...
- Springboot2.x集成单节点Redis
Springboot2.x集成单节点Redis 说明 在Springboot 1.x版本中,默认使用Jedis客户端来操作Redis,而在Springboot 2.x 版本中,默认使用Lettuce客 ...
- Springboot2.x+shiro+redis(Lettuce)整合填坑
主要记录关键和有坑的地方 前提: 1.SpringBoot+shiro已经集成完毕,如果没有集成,先查阅之前的Springboot2.0 集成shiro权限管理 2.redis已经安装完成 3.red ...
- SpringBoot- springboot集成Redis出现报错:No qualifying bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory'
Springboot将accessToke写入Redisk 缓存,springboot集成Redis出现报错 No qualifying bean of type 'org.springframewo ...
- springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices
springboot 集成redis使用redis作为缓存,会报错的问题. 错误信息: java.lang.IllegalStateException: Error processing condit ...
随机推荐
- router-link传参 query方式
router.js内的路由配置 { path: '/CreateProgress', name: 'CreateProgress', component:CreateProgress } 传参(q ...
- 洛谷P4436 游戏 [HNOI/AHOI2018]
正解:拓扑排序 解题报告: 传送门! 首先不难想到可以把麻油锁的一段先直接缩成一个点,然后预处理每个点能到达的最左和最右节点,然后就能O(1)地查询辣 所以难点在于预处理 可以想到,对于它给定的关于锁 ...
- 图片预览-兼容IE
直接贴代码吧: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- (转)Elasticsearch索引mapping的写入、查看与修改
mapping的写入与查看 首先创建一个索引: curl -XPOST "http://127.0.0.1:9200/productindex" {"acknowledg ...
- sdut2613(This is an A+B Problem)大数加法(乘法)
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...
- PAT 1070 Mooncake[一般]
1070 Mooncake (25)(25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Aut ...
- 1025 PAT Ranking[排序][一般]
1025 PAT Ranking (25)(25 分) Programming Ability Test (PAT) is organized by the College of Computer S ...
- 从 Zero 到 Hero ,一文掌握 Python
译文:开源中国 www.oschina.net/translate/learning-python-from-zero-to-hero 第一个问题,什么是 Python ?根据 Python 之父 G ...
- Python qq企业邮箱发送邮件
Python qq企业邮箱发送邮件 进入客户端设置: 下面是代码部分: from email.header import Header from email.mime.text import MIME ...
- iOS UI基础-5.0 QQ框架(Storyboard)
1.拉入TabBarController和4个Navigation 2.TabBarController关联Navigation 3.设置消息,拉入一个Button,设置背影 4.联系人,拉入一个Se ...