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 ...
随机推荐
- Cesium原理篇:6 Render模块(6: Instance实例化)【转】
https://www.cnblogs.com/fuckgiser/p/6027520.html 最近研究Cesium的实例化,尽管该技术需要在WebGL2.0,也就是OpenGL ES3.0才支持. ...
- Jenkins 发布项目到远程服务器上
最近公司弄一个项目,jenkins在本地服务器,需要打包发布到远程的阿里云服务器上,弄了好一阵子. 这里记录下中间的几个坑. 这个Remote DIrectory 很重要,到时候时候会拷贝到这个目录下 ...
- [原]globalmapper设置高程配色(globalmapper自定义配色方案)
1.使用的globalmapper版本:1.8以上(之前的版本也应该支持) 2.将全球DEM加载进去 (零时找的小DEM 全球7级) 3.右击此处,选择“高程图例选项” 4.选择 配置-着色器选项 ...
- FilelistCreator --- 导出文件列表神器 及其他好用工具
https://www.sttmedia.com/ Standard Software WordCreator: Creates readable words, sentences or texts ...
- Python向excel中写入数据的方法 方法简单
最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 xlwt依赖包,安装的方法就很简单,直接 p ...
- Scrapy爬虫Demo 爬取资讯分类
爬取新浪网导航页所有下所有大类.小类.小类里的子链接,以及子链接页面的新闻内容. 效果演示图: items.py import scrapy import sys reload(sys) sys.se ...
- django中安全sql注入等
模拟sql注入 使用原生sql语句编写login登录逻辑 class LoginUnsafeView(View): def get(self, request): return render(requ ...
- weui.js汉字乱码
2019-6-25 11:04:13 星期二 min.js 源文件中会自带乱码: 鍙栨秷: 取消; 纭畾: 确定; 方案: 把weui.js用notepad++打开, 搜索乱码字符, 替换掉, ...
- (原)使用ass字幕文件通过ffmpeg给视频添加字幕的一些研究
使用ass字幕文件通过ffmpeg给视频添加字幕的一些研究 Author:lihaiping1603@aliyun.com Create:2019-09-04 最近对ffmpeg给视频文件添加字幕效果 ...
- 修改TestStand Testsocket 从非0开始
Issue Details I am running the parallel process model or batch model and want my test sockets to be ...