如何使用Springboot中的RedisUtils工具类
发表于:2025-02-24 作者:千家信息网编辑
千家信息网最后更新 2025年02月24日,这篇文章将为大家详细讲解有关如何使用Springboot中的RedisUtils工具类,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。SpringBoot整合Redi
千家信息网最后更新 2025年02月24日如何使用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安全错误
数据库的锁怎样保障安全
管理服务器远程连接用户
马鞍山服务器机箱生产厂家
深圳晨海电脑网络技术
服务器上的小盘是多大的
东安区瀚广网络技术部
服务器登录时重启
广电网络技术人员招聘
魔兽世界联盟服务器分布图
鼓楼区网络技术研究专业服务
网络安全税务清理系统账号
温州计算机网络技术报考
关系数据库中的集合问题
全新的即时通讯软件开发
做软件开发需要研究生
利用网络技术黑他人的账号
池州oa管理软件开发定制
浦东新区安装网络技术欢迎咨询
四川网络安全集团基地
教育类软件开发背景
dedecms数据库字典
sew服务器F42报警
文广新局网络安全管理制度
天猫精灵显示服务器竟然出错了
日本服务器开发工程师招聘
欧盟网络安全主管机构
国内云服务器代理项目网络平台
洛阳华梦网络技术公司代运营
140万网络安全人才
苹果服务器怎么连接
druid 数据库监控