如何使用Springboot中的RedisUtils工具类
发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,这篇文章将为大家详细讲解有关如何使用Springboot中的RedisUtils工具类,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。SpringBoot整合Redi
千家信息网最后更新 2025年11月11日如何使用Springboot中的RedisUtils工具类
这篇文章将为大家详细讲解有关如何使用Springboot中的RedisUtils工具类,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
SpringBoot整合Redis
引入Redis依赖
org.springframework.boot spring-boot-starter-data-redis 设置Redis的Template
RedisConfig.java
package cn.wideth.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.RedisSerializer;/** * 编写配置类,构造RedisTemplate * 这个springboot已经帮我们配了, * 但是默认object,我想改成string */@Configurationpublic class RedisConfig { @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(factory); // 设置key的序列化方式 template.setKeySerializer(RedisSerializer.string()); // 设置value的序列化方式 template.setValueSerializer(RedisSerializer.json()); // 设置hash的key的序列化方式 template.setHashKeySerializer(RedisSerializer.string()); // 设置hash的value的序列化方式 template.setHashValueSerializer(RedisSerializer.json()); template.afterPropertiesSet(); return template; }} 设置Redis连接信息
redis操作5种常见的数据类型
Redis工具类
redisTemplate API
1.opsForValue -> String
2.opsForSet -> Set
3.opsForHash -> hash
4.opsForZset -> SortSet
5.opsForList -> list队列
代码
package cn.wideth.util.other;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.*;import java.util.List;import java.util.Set;import java.util.concurrent.TimeUnit;public class RedisUtils { @Autowired private RedisTemplate redisTemplate; private static double size = Math.pow(2, 32); /** * 写入缓存 * @param key * @param offset 位 8Bit=1Byte * @return */ public boolean setBit(String key, long offset, boolean isShow) { boolean result = false; try { ValueOperations operations = redisTemplate.opsForValue(); operations.setBit(key, offset, isShow); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存 * * @param key * @param offset * @return */ public boolean getBit(String key, long offset) { boolean result = false; try { ValueOperations operations = redisTemplate.opsForValue(); result = operations.getBit(key, offset); } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存设置时效时间 * @param key * @param value * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 批量删除对应的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 删除对应的value * * @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存中是否有对应的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 读取缓存 * * @param key * @return */ public Object get(final String key) { Object result = null; ValueOperations operations = redisTemplate.opsForValue(); result = operations.get(key); return result; } /** * 哈希 添加 * * @param key * @param hashKey * @param value */ public void hmSet(String key, Object hashKey, Object value) { HashOperations hash = redisTemplate.opsForHash(); hash.put(key, hashKey, value); } /** * 哈希获取数据 * * @param key * @param hashKey * @return */ public Object hmGet(String key, Object hashKey) { HashOperations hash = redisTemplate.opsForHash(); return hash.get(key, hashKey); } /** * 列表添加 * * @param k * @param v */ public void lPush(String k, Object v) { ListOperations list = redisTemplate.opsForList(); list.rightPush(k, v); } /** * 列表获取 * * @param k * @param l * @param l1 * @return */ public List 关于"如何使用Springboot中的RedisUtils工具类"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
有序
缓存
序列
数据
方式
工具
篇文章
时候
更多
第一次
哈希
不错
实用
代码
信息
内容
分数
名称
常见
文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库中查询当月总金额
软件开发阶段会计分录
网络安全 数据安全 区别
我们为什么要研究数据库
登陆界面数据库
fbi飞机网络安全吗
安徽保险软件开发定制公司
网络安全威胁场景
pathway分析数据库
霸王之业体验服务器是什么
上海网络技术价格查询
静安区特殊软件开发推荐厂家
公寓的共用网络安全吗
数据库 表 字段 编码作用
眼镜行业软件开发哪些分类
怎么备份整个数据库
excel服务器哪个好用
网络安全与信息化办
hp服务器 bios
虚拟服务器与虚拟主机
叶辰网络技术靠谱么
网络安全成长课程
数据库bean配置文件
服务器如何安装宝搭
wincc数据库sql访问
数据库安全设备有什么用
服务器如何配置u盘启动
lbe数据库
日本电信网络安全
网络安全与信息化办