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操作PDF文档的主流库使用指南

《Python操作PDF文档的主流库使用指南》PDF因其跨平台、格式固定的特性成为文档交换的标准,然而,由于其复杂的内部结构,程序化操作PDF一直是个挑战,本文主要为大家整理了Python操作PD... 目录一、 基础操作1.PyPDF2 (及其继任者 pypdf)2.PyMuPDF / fitz3.Fre

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND

Redis MCP 安装与配置指南

《RedisMCP安装与配置指南》本文将详细介绍如何安装和配置RedisMCP,包括快速启动、源码安装、Docker安装、以及相关的配置参数和环境变量设置,感兴趣的朋友一起看看吧... 目录一、Redis MCP 简介二、安www.chinasem.cn装 Redis MCP 服务2.1 快速启动(推荐)2.

Python使用openpyxl读取Excel的操作详解

《Python使用openpyxl读取Excel的操作详解》本文介绍了使用Python的openpyxl库进行Excel文件的创建、读写、数据操作、工作簿与工作表管理,包括创建工作簿、加载工作簿、操作... 目录1 概述1.1 图示1.2 安装第三方库2 工作簿 workbook2.1 创建:Workboo

基于Python实现简易视频剪辑工具

《基于Python实现简易视频剪辑工具》这篇文章主要为大家详细介绍了如何用Python打造一个功能完备的简易视频剪辑工具,包括视频文件导入与格式转换,基础剪辑操作,音频处理等功能,感兴趣的小伙伴可以了... 目录一、技术选型与环境搭建二、核心功能模块实现1. 视频基础操作2. 音频处理3. 特效与转场三、高

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

Ubuntu 24.04启用root图形登录的操作流程

《Ubuntu24.04启用root图形登录的操作流程》Ubuntu默认禁用root账户的图形与SSH登录,这是为了安全,但在某些场景你可能需要直接用root登录GNOME桌面,本文以Ubuntu2... 目录一、前言二、准备工作三、设置 root 密码四、启用图形界面 root 登录1. 修改 GDM 配

Redis中Stream详解及应用小结

《Redis中Stream详解及应用小结》RedisStreams是Redis5.0引入的新功能,提供了一种类似于传统消息队列的机制,但具有更高的灵活性和可扩展性,本文给大家介绍Redis中Strea... 目录1. Redis Stream 概述2. Redis Stream 的基本操作2.1. XADD