依赖

    <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的更多相关文章

  1. SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传

    SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...

  2. springboot学习笔记-3 整合redis&mongodb

    一.整合redis 1.1 建立实体类 @Entity @Table(name="user") public class User implements Serializable ...

  3. SpringBoot: 10.整合mybatis(转)

    需求:通过使用 SpringBoot+SpringMVC+MyBatis 整合实现一个对数据库中的 t_user 表的 CRUD 的操作 1.创建maven项目,添加项目所需依赖 <!--spr ...

  4. 【快学springboot】11.整合redis实现session共享

    前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...

  5. redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)

    平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...

  6. SpringBoot缓存篇Ⅱ --- 整合Redis以及序列化机制

    一.Redis环境搭建 系统默认是使用ConcurrentMapCacheManager,然后获取和创建ConcurrentMapCache类型的缓存组件,再将数据保存在ConcurrentMap中 ...

  7. 25、springboot与缓存整合Redis

    默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中 docker: 安装Redis: 查看官方文档: 添加约束 <dependency> <gro ...

  8. springboot 2.x整合redis,spring aop实现接口缓存

    pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  9. springboot笔记08——整合swagger2

    Swagger是什么? Swagger是一个RESTFUL 接口的文档在线自动生成和功能测试的框架.利用swagger2的注解可以快速的在项目中构建Api接口文档,并且提供了测试API的功能. Spr ...

随机推荐

  1. java实现开根号的运算

    面试的时候,偶然被问到,开根号的实现,虽然给面试官讲解了思路,但是没有实际实现过,今天闲来无事,就把自己的思路写一下,做个笔记. 如果某个数字正好可以开根号为2个整数,例如1,4,9等,那就很简单了. ...

  2. Automated Generation of VNF Deployment Rules Using Infrastructure Affinity Characterization

    标题:Automated Generation of VNF Deployment Rules Using Infrastructure Affinity Characterization 使用基础结 ...

  3. SpringBoot——Profile多环境支持

    1.多profile文件形式 主配置文件编写时, 文件名可以是application-{profile}.properties/yml 默认使用的application.properties的配置. ...

  4. win10安装grafana

    1.下载grafana-6.2.5.windows-amd64.msi 2.以管理员身份打开CMD 3.输入 msiexec /i 程序的完整路径 msiexec /i xxx.msi

  5. 试图从目录中执行 CGI、ISAPI 或其他可执行程序

    首先来看我遇到问题时的情况,直接上图!   从上图的错误提示信息可以看出,是权限不够,被拒绝访问,开始我以为是我的程序的php程序的原因,但是其他站点没事啊,就对这个站点的权限重新分配了下,给了最高权 ...

  6. Xamarin图表开发基础教程(13)OxyPlot框架支持的其它图表

    Xamarin图表开发基础教程(13)OxyPlot框架支持的其它图表 除了以上提到的图表外,OxyPlot组件还包含了6种类型的其它图表,分别为等高线图.箱线图.饼图.热图.散点图和散点误差图,如图 ...

  7. 查找算法(3)--Interpolation search--插值查找

    1. 插值查找 (1)说明 在介绍插值查找之前,首先考虑一个新问题,为什么上述算法一定要是折半,而不是折四分之一或者折更多呢? 打个比方,在英文字典里面查“apple”,你下意识翻开字典是翻前面的书页 ...

  8. PHP $$符号的作用与使用方法

    php中$$符号的定义与作用 在PHP中单个美元符号变量($str),表示一个名为str的普通变量,它可以存储字符串.整数.数组.布尔等任何类型的值. 双美元符号的变量($$str):表示一个可变变量 ...

  9. Entity Framework Core for Console

    包 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCor ...

  10. Difference between java.lang.RuntimeException and java.lang.Exception

    In Java, there are two types of exceptions: checked exceptions and un-checked exceptions. A checked ...