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 ...
随机推荐
- Parity game---poj1733
Description Now and then you play the following game with your friend. Your friend writes down a seq ...
- 【python+opencv】直线检测+圆检测
Python+OpenCV图像处理—— 直线检测 直线检测理论知识: 1.霍夫变换(Hough Transform) 霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进 ...
- Shiro的使用
前言 相比有做过企业级开发的童鞋应该都有做过权限安全之类的功能吧,最先开始我采用的是建用户表,角色表,权限表,之后在拦截器中对每一个请求进行拦截,再到数据库中进行查询看当前用户是否有该权限,这样的设计 ...
- Shell的>/dev/null、2>&1、2>1
转载自:http://dos2unix.cn/link/480 1. 标准输入stdin文件描述符为0,标准输出stdout文件描述符为1,标准错误stderr文件描述符为2 2. /dev/null ...
- [py]flask从0到1-模板/增删改查
flask知识点 1.后端渲染html到前端 render_template 2.后端获取前端数据 request.args.get 3.前端获取后端数据 模板 4.警示消息 flash {{ get ...
- [py][mx]xadmin注册切换主题功能和网站名称修改
注册主题 这里将基础的设置放到users模块 users/adminx.py from xadmin import views class BaseSetting(object): enable_th ...
- 如何给Pycharm加上头行 # *_*coding:utf-8 *_*?
File>Setting>Editor>Code Style>File and Code Templates>Python Script 后面加上 # *_*codin ...
- Razor中的@:和语法
用Razor实现流畅编程 Razor尽量减少编写一个视图模板需要敲入的字符数,实现快速流畅的编程工作流.与大部分模板的语法不同,你不必在HTML中为了明确地标记出服务模块 的开始和结束而中断编程.Ra ...
- Django实现cookie&session以及认证系统
COOKIE&SESSION 知识储备 由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie的工作原理是:由服务器产生内 ...
- http协议基础(六)报文首部
http请求和响应报文内容比较多,会分为大概四部分更新,最近比较忙,没太多时间整理- - 首先来看看报文结构吧 1.http请求报文 http请求报文由方法.URI.http版本.http首部字段等构 ...