依赖

    <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. 方法型混淆js代码

    const fs = require('fs'); const acorn = require('acorn'); const walk = require("acorn-walk" ...

  2. java 备用待迁移

    Java基础 2018年如何快速学Java 泛型就这么简单 注解就这么简单 Druid数据库连接池就是这么简单 Object对象你真理解了吗? JDK10都发布了,nio你了解多少? COW奶牛!Co ...

  3. Gaze Estimation学习笔记(1)-Appearance-Based Gaze Estimation in the Wild

    目录 前言 简介 论文概述 论文主要内容 MPIIGaze数据集 引入CNN的新Gaze Estimation方法 人脸对齐与3D头部姿态判断 归一化 使用CNN进行视线检测 论文作者进行的实验及结果 ...

  4. eclispe: 修改所有文件默认编码为UTF-8

    1.修改 workspace text file encoding 依次点击windows -> Preferences -> general -> Workspace,修改如图的编 ...

  5. Heartbeat took longer than "00:00:01" at "09/06/2019 05:08:08 +00:00".

    .netcore在k8s+docker+linux,部署后,偶尔会报这样的警告 Warn:Microsoft.AspNetCore.Server.KestrelHeartbeat took longe ...

  6. 使用 Laravel-Swagger 编写接口文档(php)

    使用 Laravel-Swagger 编写接口文档 Table of Contents Swagger 文档管理 官方网站:https://swagger.io/ 快速编写你的 RESTFUL API ...

  7. 移动端Web框架

    [参考]移动端 UI设计尺寸(一)篇 [参考]移动端界面设计之尺寸篇 [参考]介绍几个移动web app开发框架 [参考]移动webapp前端ui用哪个框架好 H5 做移动开发也分两种,一种就是正常的 ...

  8. SDN实验---Ryu的源码分析

    一:安装Pycharm https://www.cnblogs.com/huozf/p/9304396.html(有可取之处) https://www.jetbrains.com/idea/buy/# ...

  9. (转)搭建Elasticsearch和kibana环境

    搭建Elasticsearch和kibana环境 作者:IT云清 原文:https://blog.csdn.net/weixin_39800144/article/details/81162002 1 ...

  10. kubernetes-通过VMware搭建k8s集群遇到的问题

    VMWare版本:14.13 Centos版本:CentOS-7-x86_64-DVD-1810.iso 遇到的问题:ping不通 报could not resolve host这个错误 解决办法:参 ...