spring boot 整合 redis
自己开发环境需要安装 redis 服务,百度一下很多,下面主要说明Springboot 集成 redis 讲解
我的版本 java8 + redis3.0 + springboot 1.5.9。 Spring redis 集成了 jedis
redis 中存储的是 bytes
1 spring boot已经支持集成 redis,在 mvn 中只需添加依赖即可。pom 配置片段如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5..RELEASE</version>
</parent>
... <!-- 添加 redis 缓存支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
1.2 对 redis 进行配置,修改配置文件 application.properties
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=
## 连接超时时间(毫秒)
spring.redis.timeout=
2 连接 redis 需要 RedisConnection 和 RedisConnectionFactory,
RedisConnection 是通过 RedisConnectionFactory 进行创建。
RedisConnection 提供较低级的数据操作 (byte arrays)
2.1 创建配置类 RedisConfig
@Configuration
public class RedisConfig { @Autowired
private RedisConnectionFactory redisConnectionFactory; }
3 RedisTemplate
RedisTemplate 是 redis 模块的核心类,是对 redis 操作的较高抽象具有丰富的特性。他关注的是序列化和连接管理。他是线程安全的,提供了如下操作接口
HashOperations
HyperLogLogOperations
ListOperations
SetOperations
ValueOperations
ZSetOperations
Spring 还提供了一个 StringRedisTemplate,方便对 String 进行处理。java 中经常遇到对象,可以使用 jackson 进行对象-字符串 转化。可以提供一个工具类。
JsonUtil
public class JsonUtil {
private static ObjectMapper objectMapper = new ObjectMapper();
public static String convertObj2String(Object object) {
String s = null;
try {
s = objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return s;
}
public static <T> T convertString2Obj(String s, Class<T> clazz) {
T t = null;
try {
t = objectMapper.readValue(s, clazz);
} catch (IOException e) {
e.printStackTrace();
}
return t;
}
}
至此,redis 配置如下
@Configuration
public class RedisConfig { @Autowired
private RedisConnectionFactory redisConnectionFactory; @Bean
public RedisTemplate<String, String> redisTemplate(){
StringRedisTemplate redisTemplate = new StringRedisTemplate(redisConnectionFactory);
return redisTemplate;
} }
说明:默认情况下bean的名称和方法名称相同,你也可以使用name属性来指定。上面的 IOC 容器中注册了 ID为 redisTemplate 的 bean。
4 使用 redisTemplate 进行增删改查
@Service
public class RedisService { @Autowired
private StringRedisTemplate redisTemplate; /**
* 一周有多少秒
*/
private static final long WEEK_SECONDS = * * * ; /**
* 将 key,value 存放到redis数据库中,默认设置过期时间为一周
*
* @param key
* @param value
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, JsonUtil.convertObj2String(value), WEEK_SECONDS, TimeUnit.SECONDS);
} /**
* 将 key,value 存放到redis数据库中,设置过期时间单位是秒
*
* @param key
* @param value
* @param expireTime
*/
public void set(String key, Object value, long expireTime) {
redisTemplate.opsForValue().set(key, JsonUtil.convertObj2String(value), expireTime, TimeUnit.SECONDS);
} /**
* 判断 key 是否在 redis 数据库中
*
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
} /**
* 获取与 key 对应的对象
* @param key
* @param clazz 目标对象类型
* @param <T>
* @return
*/
public <T> T get(String key, Class<T> clazz) {
String s = get(key);
if (s == null) {
return null;
}
return JsonUtil.convertString2Obj(s, clazz);
} /**
* 获取 key 对应的字符串
* @param key
* @return
*/
public String get(String key) {
return redisTemplate.opsForValue().get(key);
} /**
* 删除 key 对应的 value
* @param key
*/
public void delete(String key) {
redisTemplate.delete(key);
} }
spring boot 整合 redis的更多相关文章
- SpringBoot入门系列(七)Spring Boot整合Redis缓存
前面介绍了Spring Boot 中的整合Mybatis并实现增删改查,.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/ ...
- Spring Boot 整合 Redis 和 JavaMailSender 实现邮箱注册功能
Spring Boot 整合 Redis 和 JavaMailSender 实现邮箱注册功能 开篇 现在的网站基本都有邮件注册功能,毕竟可以通过邮件定期的给用户发送一些 垃圾邮件 精选推荐
- (转)spring boot整合redis
一篇写的更清晰的文章,包括redis序列化:http://makaidong.com/ncjava/330749_5285125.html 1.项目目录结构 2.引入所需jar包 <!-- Sp ...
- Spring Boot2 系列教程(二十六)Spring Boot 整合 Redis
在 Redis 出现之前,我们的缓存框架各种各样,有了 Redis ,缓存方案基本上都统一了,关于 Redis,松哥之前有一个系列教程,尚不了解 Redis 的小伙伴可以参考这个教程: Redis 教 ...
- Spring Boot 整合 Redis 实现缓存操作
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』 本文提纲 ...
- spring boot整合redis,以及设置缓存过期时间
spring-boot 整合 redis 注:redis服务器要先开启 pom文件: <dependency> <groupId>org.springframework.boo ...
- spring boot 2.x 系列 —— spring boot 整合 redis
文章目录 一.说明 1.1 项目结构 1.2 项目主要依赖 二.整合 Redis 2.1 在application.yml 中配置redis数据源 2.2 封装redis基本操作 2.3 redisT ...
- Spring Boot2 系列教程(二十九)Spring Boot 整合 Redis
经过 Spring Boot 的整合封装与自动化配置,在 Spring Boot 中整合Redis 已经变得非常容易了,开发者只需要引入 Spring Data Redis 依赖,然后简单配下 red ...
- Spring Boot 整合Redis 实现缓存
本文提纲 一.缓存的应用场景 二.更新缓存的策略 三.运行 springboot-mybatis-redis 工程案例 四.springboot-mybatis-redis 工程代码配置详解 ...
随机推荐
- 强连通tarjan模版
#include<stdio.h> #include<iostream> #include<math.h> #include<queue> #inclu ...
- js获取checkbox中所有选中值及input后面所跟的文本
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- rsync同步数据---把左边的文件全部拷贝到右边
背景,配置好了ssh 使用rsa pulbic key的方式登陆远程服务器.//ssh-keygen -t rsa -b 2048 -f andy-rsync-key -P '' https://he ...
- MFC apps must not include windows.h
用VS2008建立一个DLL项目,一开始的时候不想用MFC, 所以选择的是使用标准Windows库. 使用了一段时间后又想用MFC了,所以把选项改成使用在共享 DLL 中使用 MFC. 但是编译的时候 ...
- C++中无法解析的外部符号错误
在编译C++程序的时候,如果引用了对应的头文件,但是调用一个函数的时候仍然出现" 无法解析的外部符号错误"的编译错误,比如: 无法解析的外部符号__imp__PathFileE ...
- grep命令经常使用參数及使用方法
1.grep介绍 grep命令是Linux系统中一种强大的文本搜索工具,它能使用正則表達式搜索文本.并把匹 配的行打印出来.grep全称Global Regular Expression Print, ...
- 一个简单RPC框架是怎样炼成的(IV)——实现RPC消息的编解码
之前我们制定了一个非常easy的RPC消息 的格式,可是还遗留了两个问题,上一篇解决掉了一个.还留下一个 我们并没有实现对应的encode和decode方法,没有基于能够跨设备的字符串传输,而是直接的 ...
- 通过Servlet生成验证码图片(转)
原文地址:http://www.cnblogs.com/xdp-gacl/p/3798190.html 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类, ...
- jQuery之前端国际化jQuery.i18n.properties[转]
http://www.ibm.com/developerworks/cn/web/1305_hezj_jqueryi18n/ jQuery.i18n.properties是一款轻量级的jQuery国际 ...
- Showing a tooltip
We can provide a balloon help for any of our widgets. #!/usr/bin/python # -*- coding: utf-8 -*- &quo ...