SpringBoot中操作spring redis的工具类
场景
SpringBoot+Vue+Redis实现前后端分离的字典缓存机制:
https://blog.csdn.net/badao_liumang_qizhi/article/details/108333996
在SpringBoot后台中会使用到Redis去缓存一些数据。
Windows下Redis服务端的安装与配置
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/107486313
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
首先pom文件中引入相关依赖。
<!-- redis 缓存操作 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
然后在application.yml配置Redis服务端的连接
# Spring配置
spring:# 资源信息messages:# 国际化资源文件路径basename: i18n/messagesprofiles:active: druid# 文件上传servlet:multipart:# 单个文件大小max-file-size: 10MB# 设置总上传的文件大小max-request-size: 20MB# 服务模块devtools:restart:# 热部署开关enabled: true# redis 配置redis:# 地址#本地测试用host: 127.0.0.1port: 6379password: 123456# 连接超时时间timeout: 10slettuce:pool:# 连接池中的最小空闲连接min-idle: 0# 连接池中的最大空闲连接max-idle: 8# 连接池的最大数据库连接数max-active: 8# #连接池最大阻塞等待时间(使用负值表示没有限制)max-wait: -1ms
这里配置的Redis服务端是我自己的本地
然后封装一个Spring Redis的工具类 用来对Redis 进行缓存和获取数据等。
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;/*** spring redis 工具类***/
@SuppressWarnings(value = { "unchecked", "rawtypes" })
@Component
public class RedisCache
{@Autowiredpublic RedisTemplate redisTemplate;/*** 缓存基本的对象,Integer、String、实体类等** @param key 缓存的键值* @param value 缓存的值* @return 缓存的对象*/public <T> ValueOperations<String, T> setCacheObject(String key, T value){ValueOperations<String, T> operation = redisTemplate.opsForValue();operation.set(key, value);return operation;}/*** 缓存基本的对象,Integer、String、实体类等** @param key 缓存的键值* @param value 缓存的值* @param timeout 时间* @param timeUnit 时间颗粒度* @return 缓存的对象*/public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit){ValueOperations<String, T> operation = redisTemplate.opsForValue();operation.set(key, value, timeout, timeUnit);return operation;}/*** 获得缓存的基本对象。** @param key 缓存键值* @return 缓存键值对应的数据*/public <T> T getCacheObject(String key){ValueOperations<String, T> operation = redisTemplate.opsForValue();return operation.get(key);}/*** 删除单个对象** @param key*/public void deleteObject(String key){redisTemplate.delete(key);}/*** 删除集合对象** @param collection*/public void deleteObject(Collection collection){redisTemplate.delete(collection);}/*** 缓存List数据** @param key 缓存的键值* @param dataList 待缓存的List数据* @return 缓存的对象*/public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList){ListOperations listOperation = redisTemplate.opsForList();if (null != dataList){int size = dataList.size();for (int i = 0; i < size; i++){listOperation.leftPush(key, dataList.get(i));}}return listOperation;}/*** 获得缓存的list对象** @param key 缓存的键值* @return 缓存键值对应的数据*/public <T> List<T> getCacheList(String key){List<T> dataList = new ArrayList<T>();ListOperations<String, T> listOperation = redisTemplate.opsForList();Long size = listOperation.size(key);for (int i = 0; i < size; i++){dataList.add(listOperation.index(key, i));}return dataList;}/*** 缓存Set** @param key 缓存键值* @param dataSet 缓存的数据* @return 缓存数据的对象*/public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet){BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);Iterator<T> it = dataSet.iterator();while (it.hasNext()){setOperation.add(it.next());}return setOperation;}/*** 获得缓存的set** @param key* @return*/public <T> Set<T> getCacheSet(String key){Set<T> dataSet = new HashSet<T>();BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key);dataSet = operation.members();return dataSet;}/*** 缓存Map** @param key* @param dataMap* @return*/public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap){HashOperations hashOperations = redisTemplate.opsForHash();if (null != dataMap){for (Map.Entry<String, T> entry : dataMap.entrySet()){hashOperations.put(key, entry.getKey(), entry.getValue());}}return hashOperations;}/*** 获得缓存的Map** @param key* @return*/public <T> Map<String, T> getCacheMap(String key){Map<String, T> map = redisTemplate.opsForHash().entries(key);return map;}/*** 获得缓存的基本对象列表** @param pattern 字符串前缀* @return 对象列表*/public Collection<String> keys(String pattern){return redisTemplate.keys(pattern);}
}
那么在进行设置数据缓存时
public static void setDictCache(String key, List<SysDictData> dictDatas){SpringUtils.getBean(RedisCache.class).setCacheObject(key, dictDatas);}
这里的key就是设置的缓存的键,后面的对象的list就是要缓存的数据。
这里获取RedisCache的方式是通过
SpringUtils.getBean(RedisCache.class)
因为这是在一个不受spring管理的工具类中。
如果是在其他受spring管理的bean中的话直接通过注解获取RedisCache即可。
同理在获取缓存的数据时
Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
同理其他工具类方法都可使用。
SpringBoot中操作spring redis的工具类相关推荐
- SpringBoot中使用Hibernate Validator校验工具类
1.说明 在Spring Boot已经集成Hibernate Validator校验器的情况下, 对于配置了校验注解的请求参数, 框架会自动校验其参数, 但是如果想手动校验一个加了注解的普通对象, 比 ...
- Spring Boot中自带的这些工具类,你用过几个?
最近发现同事写了不少重复的工具类,发现其中很多功能,Spring 自带的都有.于是整理了本文,希望能够帮助到大家! 断言 断言是一个逻辑判断,用于检查不应该发生的情况 Assert 关键字在 JDK1 ...
- SpringBoot 那些自带 Buff 的工具类,你用过几个?
今日推荐 推荐一个 Java 接口快速开发框架干掉Random:这个类已经成为获取随机数的王者Docker + Intellij IDEA,提升 10 倍生产力!笑出腹肌的注释,都是被代码耽误的诗人! ...
- 分享一个nodejs中koa操作redis的工具类 基于 ioredis
分享一个node 操作redis的工具类 基于ioredis redis.js const config = require(':config/server.base.config'); const ...
- java redis remove_最全的Java操作Redis的工具类
RedisUtil 当前版本:1.1 增加更全的方法,对以前的部分方法进行了规范命名,请放心替换成新版本. 介绍 最全的Java操作Redis的工具类,使用StringRedisTemplate实现, ...
- springBoot中shiro与Redis整合的配置文件
springBoot中shiro与Redis整合的配置文件 整合依赖: < ...
- java aes 工具类_Java中的AES加解密工具类:AESUtils
本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.constants.SysConsta ...
- 在SpringBoot中使用Spring Session解决分布式会话共享问题
在SpringBoot中使用Spring Session解决分布式会话共享问题 问题描述: 每次当重启服务器时,都会导致会员平台中已登录的用户掉线.这是因为每个用户的会话信息及状态都是由session ...
- Spring 的优秀工具类盘点---转
第 1 部分: 文件资源操作和 Web 相关工具类 http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/ 文件资源操作 文件资源的 ...
最新文章
- linux ip_conntrack 连接满导致网络丢包
- c语言 文件截断,c – 是否有一种正统的方法来避免编译器警告C4309 – “二进制文件输出的”常数值的截断“?...
- 6.3、分区表、分区表索引--Oracle模式对象
- Discovering versions from the identity service failed when creating the password plugin.
- 内容自适应编码中的不同粒度
- 晚上答辩的理论知识准备
- 儒林外史每回概括简短10字_早安心语正能量经典短句 一句话的简短励志语录
- java numberformat 方法_java.text.NumberFormat用法
- 萌新程序员找工作该怎么写简历?
- OpenCV图像处理(9)——边缘提取方法对比
- 韩国军事网络指挥中心遭到网络攻击
- SM1、SM2 、SM3、 SM4算法
- IPFS BZZ Chia,项目这么多,怎么选择?
- 字符串(昵称)过滤特殊符号和表情(通用)
- LTspice绘制方波
- hdf heg 批量拼接_python调用HEG工具批量处理MODIS数据
- 黄金分割法 ( 三分法 )
- 磁共振检查头部能检测出什么_头部核磁共振可以检查什么?
- Java使用阿里云视频点播
- 瘦臀瘦腿的最快方法是什么?
热门文章
- 华为p40鸿蒙系统价格有好高,鸿蒙系统版本华为P50新机的曝光,华为P40处境悲惨价格骤降...
- vue怎么运行html,怎样运行一个vue.js项目
- 插值法补齐缺失数据_关于数据清洗的常见方式
- shell排序_Java后端技术精选:希尔排序
- Linux下安装Dubbo运行环境
- python汽车仿真_汽车山羊问题的分析以及Python和MATLAB仿真实验
- python吃内存还是cpu_Python2 得到 CPU 和内存信息要怎么实现呢?
- c语言考试算法,c语言考试常用算法docx.docx
- python程序设计典型例题-python 大题 典型
- clover引导macos big sur_安装MAC OS系统看完后能让你马上用上最新苹果系统