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

相关文章

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表