redisTemplate操作Redis工具类

2024-05-05 21:08

本文主要是介绍redisTemplate操作Redis工具类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

public class RedisUtils {@Autowiredprivate StringRedisTemplate redisTemplate;/*** 写入缓存** @param key* @param value* @return*/public boolean set(final String key, String value) {boolean result = false;try {ValueOperations<String, String> operations = redisTemplate.opsForValue();byte[] bytes = key.getBytes("ISO8859-1");byte[] bytes2 = value.getBytes("ISO8859-1");String str = new String(bytes, "UTF-8");String str2 = new String(bytes2, "UTF-8");operations.set(str, str2);result = true;} catch (Exception e) {e.printStackTrace();} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}return result;}/*** 写入缓存设置时效时间** @param key* @param value* @return*/public boolean setExp(final String key, String value, Long expireTime) {boolean result = false;try {ValueOperations<String, String> operations = redisTemplate.opsForValue();operations.set(key, value);redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);result = true;} catch (Exception e) {e.printStackTrace();} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}return result;}/*** 批量删除对应的value** @param keys*/public void removes(final String... keys) {for (String key : keys) {remove(key);}}/*** 批量删除key** @param pattern*/public void removes(final String pattern) {try {Set<String> keys = redisTemplate.keys(pattern);if (keys.size() > 0)redisTemplate.delete(keys);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 删除对应的value** @param key*/public void remove(final String key) {try {if (exists(key)) {redisTemplate.delete(key);}} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 判断缓存中是否有对应的key** @param key* @return*/public boolean exists(final String key) {try {return redisTemplate.hasKey(key);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 读取缓存** @param key* @return*/public String get(final String key) {try {ValueOperations<String, String> operations = redisTemplate.opsForValue();String result = operations.get(key);return result;} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 哈希 添加** @param key* @param field* @param value*/public void hmSet(String key, String field, String value) {try {HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();hash.put(key, field, value);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/**** @param key* @param field* @param value* @param expireTime*/public void hmSet(String key, String field, String value,Long expireTime) {try {HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();hash.put(key, field, value);redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 哈希获取数据** @param key* @param hashKey* @return*/public String hmGet(String key, String hashKey) {try {HashOperations<String, String, String> hash = redisTemplate.opsForHash();return hash.get(key, hashKey);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}public Map<String, String> hmGetAll(String key) {try {HashOperations<String, String, String> hash = redisTemplate.opsForHash();return hash.entries(key);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}public void hmDel(String key, String field) {try {HashOperations<String, String, String> hash = redisTemplate.opsForHash();hash.delete(key, field);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}public void hmdels(String key, List<Long> fields) {try {HashOperations<String, String, String> hash = redisTemplate.opsForHash();if (CollectionUtils.isEmpty(fields)) {return;}for (Long field : fields) {hash.delete(key, field.toString());}} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 列表添加** @param key* @param value*/public void lSet(String key, String value) {try {ListOperations<String, String> list = redisTemplate.opsForList();list.rightPush(key, value);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}public boolean lSets(String key, String... value) {try {ListOperations<String, String> list = redisTemplate.opsForList();list.rightPushAll(key, value);return true;} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 列表获取** @param key* @param startIndex* @param endIndex* @return*/public List<String> lGet(String key, long startIndex, long endIndex) {try {ListOperations<String, String> list = redisTemplate.opsForList();return list.range(key, startIndex, endIndex);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}public Long lGetSize(String key) {try {ListOperations<String, String> list = redisTemplate.opsForList();return list.size(key);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}public boolean ldel(String key, long var2, Object var4) {try {ListOperations<String, String> list = redisTemplate.opsForList();return list.remove(key,var2,var4) >0;} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 集合添加** @param key* @param value*/public void sSet(String key, String value) {try {SetOperations<String, String> set = redisTemplate.opsForSet();set.add(key, value);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}public void sSet(String key, String value,Long expireTime) {try {SetOperations<String, String> set = redisTemplate.opsForSet();set.add(key, value);redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 集合获取** @param key* @return*/public Set<String> sGet(String key) {try {SetOperations<String, String> set = redisTemplate.opsForSet();return set.members(key);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 有序集合添加** @param key* @param value* @param scoure*/public void zsset(String key, String value, double scoure) {try {ZSetOperations<String, String> zset = redisTemplate.opsForZSet();zset.add(key, value, scoure);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 有序集合获取** @param key* @param scoure* @param scoure1* @return*/public Set<String> zsget(String key, double scoure, double scoure1) {try {ZSetOperations<String, String> zset = redisTemplate.opsForZSet();return zset.rangeByScore(key, scoure, scoure1);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 获取所有set* @param key* @return*/public Set<String> zsetGetAll(String key) {try {ZSetOperations<String, String> zset = redisTemplate.opsForZSet();return zset.range(key, 0, -1);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 获取范围的元素来自start于end从下令从低分到高分排序集。* @param key* @param start* @param end* @return*/public Set<String> rangeByScore(String key, long start, long end) {try {ZSetOperations<String, String> zset = redisTemplate.opsForZSet();return zset.rangeByScore(key, start, end);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}public Double getScore(String key, String member) {try {ZSetOperations<String, String> zset = redisTemplate.opsForZSet();return zset.score(key,member);} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}}/*** 删除zset元素* @param key* @param val* @return*/public boolean zdel(String key, String... val) {try {ZSetOperations<String, String> zset = redisTemplate.opsForZSet();zset.remove(key,val);return zset.remove(key,val) > 0;} catch (Exception e) {e.printStackTrace();} finally {RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());}return false;}public int removeListValue(String key, List<String> values) {int result = 0;if (values != null && values.size() > 0) {for (String value : values) {if (ldel(key, Long.valueOf(1),value)) {result++;}}}return result;}
}

 

这篇关于redisTemplate操作Redis工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/962681

相关文章

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

Java Multimap实现类与操作的具体示例

《JavaMultimap实现类与操作的具体示例》Multimap出现在Google的Guava库中,它为Java提供了更加灵活的集合操作,:本文主要介绍JavaMultimap实现类与操作的... 目录一、Multimap 概述Multimap 主要特点:二、Multimap 实现类1. ListMult

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷