如何使用Springboot中的RedisUtils工具类
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章将为大家详细讲解有关如何使用Springboot中的RedisUtils工具类,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。SpringBoot整合Redi
千家信息网最后更新 2026年11月03日如何使用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安全错误
数据库的锁怎样保障安全
华服互联网科技靠谱吗
科技金融是不是互联网金融
互联网科技官网
互联网是不是新科技
深圳互联网络科技有限公司
火传媒上海互联网科技
互联网科技英文翻译
湖南洣水互联网科技有限公司扣款
随着互联网科技发展
天津腾达互联网科技有限公司
威龙国际互联网科技公司
互联网科技如何创业
无锡工业互联网科技有限公司
互联网第三次科技革命特征
北京市互联网新科技
皮皮虾互联网科技有限公司
互联网科技数字货币
互联网与数字科技关系
全球科技与互联网公司市值变化
互联网科技的前景
上海挚品互联网科技
齐家互联网科技
唐信互联网科技
奇士达互联网科技
互联网 科技服务平台
互联网科技博主是什么意思
中国科技互联网公司收入
知域互联网科技有限公司大骗子
信承诺互联网科技有限公司
互联网高科技行业有哪些企业