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

相关文章

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

python版本切换工具pyenv的安装及用法

《python版本切换工具pyenv的安装及用法》Pyenv是管理Python版本的最佳工具之一,特别适合开发者和需要切换多个Python版本的用户,:本文主要介绍python版本切换工具pyen... 目录Pyenv 是什么?安装 Pyenv(MACOS)使用 Homebrew:配置 shell(zsh

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

MySQL游标和触发器的操作流程

《MySQL游标和触发器的操作流程》本文介绍了MySQL中的游标和触发器的使用方法,游标可以对查询结果集进行逐行处理,而触发器则可以在数据表发生更改时自动执行预定义的操作,感兴趣的朋友跟随小编一起看看... 目录游标游标的操作流程1. 定义游标2.打开游标3.利用游标检索数据4.关闭游标例题触发器触发器的基

在C#中分离饼图的某个区域的操作指南

《在C#中分离饼图的某个区域的操作指南》在处理Excel饼图时,我们可能需要将饼图的各个部分分离出来,以使它们更加醒目,Spire.XLS提供了Series.DataFormat.Percent属性,... 目录引言如何设置饼图各分片之间分离宽度的代码示例:从整个饼图中分离单个分片的代码示例:引言在处理

Python列表的创建与删除的操作指南

《Python列表的创建与删除的操作指南》列表(list)是Python中最常用、最灵活的内置数据结构之一,它支持动态扩容、混合类型、嵌套结构,几乎无处不在,但你真的会创建和删除列表吗,本文给大家介绍... 目录一、前言二、列表的创建方式1. 字面量语法(最常用)2. 使用list()构造器3. 列表推导式

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

Python+wxPython开发一个文件属性比对工具

《Python+wxPython开发一个文件属性比对工具》在日常的文件管理工作中,我们经常会遇到同一个文件存在多个版本,或者需要验证备份文件与源文件是否一致,下面我们就来看看如何使用wxPython模... 目录引言项目背景与需求应用场景核心需求运行结果技术选型程序设计界面布局核心功能模块关键代码解析文件大

MySQL基本表查询操作汇总之单表查询+多表操作大全

《MySQL基本表查询操作汇总之单表查询+多表操作大全》本文全面介绍了MySQL单表查询与多表操作的关键技术,包括基本语法、高级查询、表别名使用、多表连接及子查询等,并提供了丰富的实例,感兴趣的朋友跟... 目录一、单表查询整合(一)通用模版展示(二)举例说明(三)注意事项(四)Mapper简单举例简单查询

Nginx概念、架构、配置与虚拟主机实战操作指南

《Nginx概念、架构、配置与虚拟主机实战操作指南》Nginx是一个高性能的HTTP服务器、反向代理服务器、负载均衡器和IMAP/POP3/SMTP代理服务器,它支持高并发连接,资源占用低,功能全面且... 目录Nginx 深度解析:概念、架构、配置与虚拟主机实战一、Nginx 的概念二、Nginx 的特点