【SpringBoot】十七、SpringBoot中整合Redis
发布日期:2021-06-30 21:30:02
浏览次数:4
分类:技术文章
本文共 4834 字,大约阅读时间需要 16 分钟。
本次,我们以IDEA + SpringBoot作为 Java中整合Redis的使用 的测试环境,如果对创建SpringBoot项目有不清楚的地方,可以参考我的博客:
- 首先,我们需要导入Redis的maven依赖
org.springframework.boot spring-boot-starter-data-redis
- 其次,我们需要在配置文件中配置你的Redis配置信息,我使用的是 .yml文件格式
# redis配置spring: redis: # r服务器地址 host: 127.0.0.1 # 服务器端口 port: 6379 # 数据库索引(默认0) database: 0 jedis: pool: # 连接池最大连接数(使用负值表示没有限制) max-active: 50 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: 3000ms # 连接池中的最大空闲连接数 max-idle: 20 # 连接池中的最小空闲连接数 min-idle: 2 # 连接超时时间(毫秒) timeout: 5000ms
- 然后,我们需要创建一个RedisUtil来对Redis数据库进行操作
package com.zyxx.test.utils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;/** * @ClassName RedisUtil * @Author Lizhou * @Date 2019-08-03 17:29:29 * @Version 1.0 **/@Componentpublic class RedisUtil { @Autowired private RedisTemplatetemplate; /** * 读取数据 * * @param key * @return */ public String get(final String key) { return template.opsForValue().get(key); } /** * 写入数据 */ public boolean set(final String key, String value) { boolean res = false; try { template.opsForValue().set(key, value); res = true; } catch (Exception e) { e.printStackTrace(); } return res; } /** * 根据key更新数据 */ public boolean update(final String key, String value) { boolean res = false; try { template.opsForValue().getAndSet(key, value); res = true; } catch (Exception e) { e.printStackTrace(); } return res; } /** * 根据key删除数据 */ public boolean del(final String key) { boolean res = false; try { template.delete(key); res = true; } catch (Exception e) { e.printStackTrace(); } return res; } /** * 是否存在key */ public boolean hasKey(final String key) { boolean res = false; try { res = template.hasKey(key); } catch (Exception e) { e.printStackTrace(); } return res; } /** * 给指定的key设置存活时间 * 默认为-1,表示永久不失效 */ public boolean setExpire(final String key, long seconds) { boolean res = false; try { if (0 < seconds) { res = template.expire(key, seconds, TimeUnit.SECONDS); } } catch (Exception e) { e.printStackTrace(); } return res; } /** * 获取指定key的剩余存活时间 * 默认为-1,表示永久不失效,-2表示该key不存在 */ public long getExpire(final String key) { long res = 0; try { res = template.getExpire(key, TimeUnit.SECONDS); } catch (Exception e) { e.printStackTrace(); } return res; } /** * 移除指定key的有效时间 * 当key的有效时间为-1即永久不失效和当key不存在时返回false,否则返回true */ public boolean persist(final String key) { boolean res = false; try { res = template.persist(key); } catch (Exception e) { e.printStackTrace(); } return res; } }
- 最后,我们可以使用单元测试来检测我们在RedisUtil中写的操作Redis数据库的方法
package com.zyxx.test;import com.zyxx.test.utils.RedisUtil;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;@RunWith(SpringRunner.class)@SpringBootTestpublic class TestApplicationTest { @Resource private RedisUtil redisUtil; @Test public void setRedis() { boolean res = redisUtil.set("jay", "周杰伦 - 《以父之名》"); System.out.println(res); } @Test public void getRedis() { String res = redisUtil.get("jay"); System.out.println(res); } @Test public void updateRedis() { boolean res = redisUtil.update("jay", "周杰伦 - 《夜的第七章》"); System.out.println(res); } @Test public void delRedis() { boolean res = redisUtil.del("jay"); System.out.println(res); } @Test public void hasKey() { boolean res = redisUtil.hasKey("jay"); System.out.println(res); } @Test public void expire() { boolean res = redisUtil.setExpire("jay", 100); System.out.println(res); } @Test public void getExpire() { long res = redisUtil.getExpire("jay"); System.out.println(res); } @Test public void persist() { boolean res = redisUtil.persist("jay"); System.out.println(res); } }
- 推荐使用Redis客户端(redis-desktop-manager)来查看Redis数据库中的数据
- 至此,我们在日常项目中整合Redis的基本使用操作就完成了,但在实际项目中,可能会涉及到更复杂的用法,可以根据你的业务需求调整Redis的使用即可。
如您在阅读中发现不足,欢迎留言!!!
转载地址:https://lizhou.blog.csdn.net/article/details/98358258 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月28日 18时48分25秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Oracle创建表,修改表(添加列、修改列、删除列、修改表的名称以及修改列名)
2019-05-01
SQL命令分类(DDL、DML、DCL、TCL以及事务)
2019-05-01
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate 报错
2019-05-01
kafka集群搭建
2019-05-01
Python正则表达式,从入门到实战,精华都在这里!
2019-05-01
喜欢玩王者荣耀的有福了,用 Python 获取英雄皮肤壁纸
2019-05-01
一名数据分析师的SQL学习历程
2019-05-01
图解《金字塔原理》,7步掌握其精华!
2019-05-01
数据分析入门选择Python还是SQL?七个常用操作对比!
2019-05-01
最后一台,i7+6核电脑
2019-05-01
19款国产手机无一幸免:15分钟破解人脸识别,打印眼镜让刷脸形同虚设
2019-05-01
通俗讲解深度学习中的图像分割技术
2019-05-01
Github标星4.8K!微软开源的可视化工具,未免太酷炫了吧
2019-05-01
这可能是近 2 年进腾讯的最好机会!
2019-05-01
比正则快M倍以上!Python替换字符串的新姿势
2019-05-01
用SVM来识别魔方颜色(实践篇)
2019-05-01
期待已久的微信功能,终于要来了!
2019-05-01
手把手教你搭建计算机视觉开发环境
2019-05-01
超级详细,“保姆级”Python优质学习攻略来啦!
2019-05-01
用 Python 写的文档批量翻译工具,效果竟然超越付费软件?
2019-05-01