springboot笔记10——整合Redis
依赖
<dependencies>
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Redis 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
配置
spring:
redis:
port: 6379
host: 127.0.0.1
password: redis
database: 0
Redis工具类
/**
* @description:
* @author: Jotal
* @time: 2019/8/17 21:29
*/
@Component("redisUtils")
public class RedisUtil {
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
* @Description: 获取
* @Param: [key]
* @Return: java.lang.String
* @Author: Jotal
* @Date: 2019/8/17 21:39
*/
public String get(String key) {
try {
if (StringUtils.isEmpty(key)) {
return null;
}
return stringRedisTemplate.opsForValue().get(key);
} catch (Exception e) {
System.out.println(String.format("redis缓存获取key的值异常!key:%s", key));
e.printStackTrace();
}
return null;
}
/**
* @Description: 设置键值对
* @Param: [key, value]
* @Return: java.lang.Boolean
* @Author: Jotal
* @Date: 2019/8/17 21:43
*/
public Boolean set(String key,String value) {
try {
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
return false;
}
stringRedisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
System.out.println(String.format("redis缓存设置键值对!key:%s,value:%s", key,value));
e.printStackTrace();
}
return false;
}
/**
* @Description: 删除键值对
* @Param: [key]
* @Return: java.lang.Boolean
* @Author: Jotal
* @Date: 2019/8/17 21:47
*/
public Boolean del(String key) {
try {
if (StringUtils.isEmpty(key)) {
return false;
}
return stringRedisTemplate.delete(key);
} catch (Exception e) {
System.out.println(String.format("redis删除键值对!key:%s", key));
e.printStackTrace();
}
return false;
}
/**
* @Description: 设置键值对和缓存时间,单位为秒
* @Param: [key, value, time]
* @Return: java.lang.Boolean
* @Author: Jotal
* @Date: 2019/8/17 21:49
*/
public Boolean setEX(String key, String value, Long time) {
try {
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
return false;
}
stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
//设置缓存时间
//stringRedisTemplate.expire(key,time, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
System.out.println("设置缓存异常");
e.printStackTrace();
}
return false;
}
/**
* @Description: 获取key的缓存时间
* @Param: [key]
* @Return: java.lang.Long
* @Author: Jotal
* @Date: 2019/8/17 21:55
*/
public Long getExpireTime(String key) {
try {
if (StringUtils.isEmpty(key)) {
return null;
}
return stringRedisTemplate.getExpire(key,TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println("获取缓存异常");
e.printStackTrace();
}
return null;
}
}
单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class Springboot10RedisApplicationTests {
//@Resource是根据名字来自动装配 @Autowired是根据类型来自动装配
@Resource
private RedisUtil redisUtils;
@Test
public void setTest() {
Boolean bl = redisUtils.set("jotal", "jotal1314");
log.info("设置键值对"+bl);
}
@Test
public void getTest() {
String value = redisUtils.get("welcome");
log.info("获取值:"+value);
}
@Test
public void testDelete() {
Boolean flag = redisUtils.del("jotal1");
log.info("testDelete:"+flag);
}
@Test
public void testSetEX() {
Boolean flag = redisUtils.setEX("welcome","www",1000L);
log.info("testSetEX:"+flag);
}
@Test
public void testGetExpireTime() {
Long time = redisUtils.getExpireTime("welcome");
log.info("testSetEX:"+time);
}
}
观察Redis数据库中的键值对变化!
springboot笔记10——整合Redis的更多相关文章
- SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传
SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...
- springboot学习笔记-3 整合redis&mongodb
一.整合redis 1.1 建立实体类 @Entity @Table(name="user") public class User implements Serializable ...
- SpringBoot: 10.整合mybatis(转)
需求:通过使用 SpringBoot+SpringMVC+MyBatis 整合实现一个对数据库中的 t_user 表的 CRUD 的操作 1.创建maven项目,添加项目所需依赖 <!--spr ...
- 【快学springboot】11.整合redis实现session共享
前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...
- redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)
平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...
- SpringBoot缓存篇Ⅱ --- 整合Redis以及序列化机制
一.Redis环境搭建 系统默认是使用ConcurrentMapCacheManager,然后获取和创建ConcurrentMapCache类型的缓存组件,再将数据保存在ConcurrentMap中 ...
- 25、springboot与缓存整合Redis
默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中 docker: 安装Redis: 查看官方文档: 添加约束 <dependency> <gro ...
- springboot 2.x整合redis,spring aop实现接口缓存
pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- springboot笔记08——整合swagger2
Swagger是什么? Swagger是一个RESTFUL 接口的文档在线自动生成和功能测试的框架.利用swagger2的注解可以快速的在项目中构建Api接口文档,并且提供了测试API的功能. Spr ...
随机推荐
- NIOBuffer 缓冲区
Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但是,Channel,Buffer 和 Select ...
- fastjson WriteClassName,Double类型不打3.3D
方式一: public class SerializeConfigX extends SerializeConfig { public SerializeConfigX() { put(Double. ...
- python skimage图像处理(二)
python skimage图像处理(二) This blog is from: https://www.jianshu.com/p/66e6261f0279 图像简单滤波 对图像进行滤波,可以有两 ...
- 一个android任务提醒程序
需求: 运行建立多个提醒,每个提醒设置一个时间,到达指定时间后跳出提醒窗体 每个提醒有一个执行按钮,点击后保存执行记录,并且当天不再触发提醒窗体 提醒出现后如果任务还没执行,那么需要在30分钟后再次提 ...
- 如何防止Hangfire重复作业在连续执行30分钟后重新启动(How to prevent a Hangfire recurring job from restarting after 30 minutes of continuous execution)
var options = new SqlServerStorageOptions { InvisibilityTimeout = TimeSpan .FromMinutes(30)//默认值}; G ...
- Anaconda(三)
五.TensorFlow安装 这一天由于版本问题走了太多弯路.之前用的conda版本是最新的,自带Python3.7.5,装了之后倒是各种包都能装,用命令: pip install xxx conda ...
- Egret HTML5游戏开发指南
Egret HTML5游戏开发指南 下载地址:https://pan.baidu.com/s/1fuxllvmRhWXoWDwH4gxN9g 关注微信公众号获取提取码: 输入:egrt 获取提取码
- CentOS / RHEL 内核升级
1. 查看当前内核版本 [root@192.168.118.11 ~]#cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core) [ro ...
- 部署TiDB集群
架构图 节点规划 120.52.146.213 Control Machine 120.52.146.214 PD1_TiDB1 120.52.146.215 PD2_TiDB2 120.52.146 ...
- java.lang.ClassNotFoundException: org.apache.http.impl.client.HttpClientBuilder
添加依赖即可:compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6' ,注意是apache的包