/*** spring redis 工具类**/
@Component
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public class RedisService {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate StringRedisTemplate stringRedisTemplate;/*** 缓存基本的对象,Integer、String、实体类等* @param prefix redis前缀* @param key 缓存的键值* @param value 缓存的值* @param <T> T* @return 缓存的对象*/public <T> ValueOperations<String, T> setCacheObject(KeyPrefix prefix, String key, T value) {ValueOperations<String, T> operation = redisTemplate.opsForValue();// 生成真正的keyString realKey = prefix.getPrefix() + key;operation.set(realKey, value);return operation;}/*** 缓存基本的对象,Integer、String、实体类等* @param prefix redis前缀* @param key 缓存的键值* @param value 缓存的值* @param timeout 时间* @param timeUnit 时间颗粒度* @return 缓存的对象*/public <T> ValueOperations<String, T> setCacheObject(KeyPrefix prefix, String key, T value, Integer timeout, TimeUnit timeUnit) {ValueOperations<String, T> operation = redisTemplate.opsForValue();String realKey = prefix.getPrefix() + key;operation.set(realKey, value, timeout, timeUnit);return operation;}/*** 缓存基本的对象,Integer、String、实体类等(使用StringRedisSerializer序列化)* @param prefix redis前缀* @param key 缓存的键值* @param value 缓存的值* @param timeout 时间* @param timeUnit 时间颗粒度* @return 缓存的对象*/public ValueOperations<String, String> setCacheObjectByRedisSerializer(KeyPrefix prefix, String key, String value, Integer timeout, TimeUnit timeUnit) {ValueOperations<String, String> operation = stringRedisTemplate.opsForValue();String realKey = prefix.getPrefix() + key;operation.set(realKey, value, timeout, timeUnit);return operation;}/*** 设置值加一* @param prefix redis前缀* @param key 缓存键值* @return 缓存键值对应的数据*/public Long incrementCacheObject(KeyPrefix prefix, String key) {ValueOperations<String, Long> operation = redisTemplate.opsForValue();String realKey = prefix.getPrefix() + key;return operation.increment(realKey);}/*** 获得缓存的基本对象。* @param prefix redis前缀* @param key 缓存键值* @return 缓存键值对应的数据*/public <T> T getCacheObject(KeyPrefix prefix, String key) {ValueOperations<String, T> operation = redisTemplate.opsForValue();String realKey = prefix.getPrefix() + key;return operation.get(realKey);}/*** 获得缓存的基本对象。* @param prefix redis前缀* @return 缓存键值对应的数据*/public <T> T getCacheObject(KeyPrefix prefix) {ValueOperations<String, T> operation = redisTemplate.opsForValue();String realKey = prefix.getPrefix();return operation.get(realKey);}/*** 删除单个对象* @param prefix redis前缀* @param key 缓存键值*/public void deleteObject(KeyPrefix prefix, String key) {redisTemplate.delete(prefix.getPrefix() + key);}/*** 删除集合对象* @param collection collection*/public void deleteObject(Collection collection) {redisTemplate.delete(collection);}/*** 缓存List数据* @param prefix redis前缀* @param key 缓存的键值* @param dataList 待缓存的List数据* @return 缓存的对象*/public <T> ListOperations<String, T> setCacheList(KeyPrefix prefix, String key, List<T> dataList) {ListOperations listOperation = redisTemplate.opsForList();if (null != dataList) {String realKey = prefix.getPrefix() + key;listOperation.leftPushAll(realKey,dataList);}return listOperation;}/*** 获得缓存的list对象* @param prefix redis前缀* @param key 缓存的键值* @return 缓存键值对应的数据*/public <T> List<T> getCacheList(KeyPrefix prefix, String key) {List<T> dataList = new ArrayList<T>();ListOperations<String, T> listOperation = redisTemplate.opsForList();String realKey = prefix.getPrefix() + key;Long size = listOperation.size(realKey);if(size == null){return null;}for (int i = 0; i < size; i++) {dataList.add(listOperation.index(realKey, i));}return dataList;}/*** 删除List所以数据* @param prefix redis前缀* @param key 缓存的键值* @param value 缓存的键值* @param count= 0 删除所有匹配的元素; > 0 删除匹配元素开始,从左到右最多count个元素; < 0 删除匹配元素开始,从右到左最多count个元素*/public void deleteCacheList(KeyPrefix prefix, String key, String value, int count) {ListOperations listOperation = redisTemplate.opsForList();String realKey = prefix.getPrefix() + key;listOperation.remove(realKey, count, value);}/*** 缓存Set* @param prefix redis前缀* @param key 缓存键值* @param dataSet 缓存的数据* @return 缓存数据的对象*/public <T> BoundSetOperations<String, T> setCacheSet(KeyPrefix prefix, String key, Set<T> dataSet) {String realKey = prefix.getPrefix() + key;BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(realKey);if(CollectionUtils.isEmpty(dataSet)){return null;}for (T t : dataSet) {setOperation.add(t);}return setOperation;}/*** 获得缓存的set* @param prefix redis前缀* @param key 缓存键值* @return Set<T>*/public <T> Set<T> getCacheSet(KeyPrefix prefix, String key) {Set<T> dataSet;String realKey = prefix.getPrefix() + key;BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(realKey);dataSet = operation.members();return dataSet;}/*** 缓存Map* @param prefix redis前缀* @param key 缓存键值* @param dataMap dataMap* @return HashOperations*/public <T> HashOperations<String, String, T> setCacheMap(KeyPrefix prefix, String key, Map<String, T> dataMap) {HashOperations hashOperations = redisTemplate.opsForHash();String realKey = prefix.getPrefix() + key;if (null != dataMap) {for (Map.Entry<String, T> entry : dataMap.entrySet()) {hashOperations.put(realKey, entry.getKey(), entry.getValue());}}return hashOperations;}/*** 缓存Map key* @param prefix prefix* @param key key* @param hashKey hashKey* @param hashValue hashValue*/public void setCacheMapKey(KeyPrefix prefix, String key, String hashKey, String hashValue) {String realKey = prefix.getPrefix() + key;redisTemplate.opsForHash().put(realKey, hashKey, hashValue);}/*** 获得缓存的Map* @param prefix redis前缀* @param key 缓存键值* @return Map*/public <T> Map<String, T> getCacheMap(KeyPrefix prefix, String key) {String realKey = prefix.getPrefix() + key;return (Map<String, T>) redisTemplate.opsForHash().entries(realKey);}/*** 删除Hash缓存的一个或多个元素* @param prefix prefix* @param key key* @param hashKeys hashKeys* @return Long*/public Long deleteCacheMapFieId(KeyPrefix prefix, String key, Object... hashKeys) {String realKey = prefix.getPrefix() + key;return redisTemplate.opsForHash().delete(realKey, hashKeys);}/*** 获取缓存Map中key* @param prefix prefix* @param key key* @param hashKey hashKey* @return Object*/public Object getCacheMapFieId(KeyPrefix prefix, String key, String hashKey) {String realKey = prefix.getPrefix() + key;return redisTemplate.opsForHash().get(realKey, hashKey);}/*** 获得缓存的基本对象列表* * @param pattern 字符串前缀* @return 对象列表*/public Collection<String> keys(String pattern) {return redisTemplate.keys(pattern);}/*** 获取当前时间到第二天的秒数* @param date 当前时间* @return int*/private static int getTomorrowSeconds(Date date){LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());LocalDateTime tomorrow = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);long seconds = ChronoUnit.SECONDS.between(localDateTime, tomorrow);return (int)seconds;}/*** 插入ZSet数据* @param prefix prefix* @param key key* @param value value* @param score 排序权重* @param <T> T* @return Boolean*/public <T> Boolean setZSetObject(KeyPrefix prefix, String key, T value, Long score) {String realKey = prefix.getPrefix() + key;return redisTemplate.opsForZSet().add(realKey, value, score);}/*** 移除ZSet消息* @param prefix prefix* @param key key* @param value value* @param <T> T* @return Boolean*/public <T> Boolean removeZSet(KeyPrefix prefix, String key, T value) {String realKey = prefix.getPrefix() + key;Long remove = redisTemplate.opsForZSet().remove(realKey, value);return remove != null && remove > 0;}/*** 拉取最新需要被消费的消息* @param prefix prefix* @param key key* @param <T> T* @return Set*/public <T> Set<T> pullZSetRecords(KeyPrefix prefix, String key) {String realKey = prefix.getPrefix() + key;Set<T> records = redisTemplate.opsForZSet().rangeByScore(realKey, 0, System.currentTimeMillis());if (records == null) {return null;}return records;}
}

引用接口
1、KeyPrefix

public interface KeyPrefix {int expireSeconds();String getPrefix();
}

2、BasePrefix

public abstract class BasePrefix implements KeyPrefix {private int expireSeconds;private String prefix;public BasePrefix(String prefix) {this(0, prefix);}public BasePrefix(int expireSeconds, String prefix) {this.expireSeconds = expireSeconds;this.prefix = prefix;}public int expireSeconds() {return this.expireSeconds;}public String getPrefix() {String className = this.getClass().getSimpleName();return className + ":" + this.prefix;}
}

3、CrmStuCodeKey

public class CrmStuCodeKey extends BasePrefix {public static CrmStuCodeKey CODE = new CrmStuCodeKey("code");public CrmStuCodeKey(String prefix) {super(prefix);}
}

Spring Redis工具类相关推荐

  1. springboot 集成 redis 工具类

    添加依赖 <!-- SpringBoot Boot Redis --><dependency><groupId>org.springframework.boot&l ...

  2. 【Spring之轨迹】结合 @Scheduled 实现定时将 Redis 缓存刷入数据库(配 Redis 工具类与例子实战)

    -- 目录 -- 0. 假设已配置好 SSM 环境 1. 配置文件 2. 定时服务 3. cron 解释 ① cron 参数(按顺序依次为) ② 特殊符号 4. Redis 工具类 5. 例子实战 0 ...

  3. Redis工具类封装RedisUtils

    本文参考:https://blog.it-follower.com/posts/2563248908.html SpringBoot项目集成Redis相当简单,只需要pom中加入对应依赖 <de ...

  4. SpringBoot Redis工具类封装

    SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable).看了很多博客后, ...

  5. Springboot集成Redis和Redis工具类

    目录 1.导入依赖 2.修改配置文件 3.测试 4.使用redis存入对象 5.redis自定义封装 RedisUtils工具类 1.导入依赖 <dependency><groupI ...

  6. java实现redis工具类及其调用

    网上很多redis工具类需要配套ssm,spring等,这是一套可以直接用的redis取参数的工具类(String类型) 一.jedis-2.1.0.jar下载,放到项目的lib下 网盘地址(4tn5 ...

  7. Java开发中的工具类——基于JedisPool的Redis工具类

    目录 一.Maven依赖 二.Redis配置类 三.使用@Cacheable注解进行数据缓存 四.自定义Redis工具类及使用 4.1 序列化工具类 4.2 redis客户端工具类 4.3 redis ...

  8. 微服务使用redis操作实例包含redis工具类

    1.单机redis使用工具类 application.properties配置内容 #redis配置 #单机模式 #redis数据库索引,默认为0 spring.redis.database=0 #r ...

  9. Redis工具类封装RedisUtils(两种)

    RedisTemplate工具类1 本文参考:https://blog.it-follower.com/posts/2563248908.html SpringBoot项目集成Redis相当简单,只需 ...

最新文章

  1. TabLayout 遇到那些坑 tab标签不显示问题
  2. 30幅非常漂亮的微距摄影作品欣赏
  3. protected private public
  4. 为什么你总成为不了架构师?
  5. 自定义浏览器协议,实现web程序调用本地程序
  6. LeetCode 1004.最长连续1的个数
  7. 《深入理解Hadoop(原书第2版)》——2.3Hadoop系统的组成
  8. CrossPHP框架的常用操作
  9. JSP页面元素,内置对象及request详解
  10. Big Sur恢复Catalina ? macOS Big Sur降级的三种方法 !
  11. 目标检测综述——两阶段检测器
  12. SE Block (Sequeze and Excitation)
  13. 2018年最新最全的全国省市区五级四级三级地址数据库的SQL下载
  14. html5 sms短信发送_使用电子邮件免费向手机发送短信(SMS)
  15. 移动页面HTML5自适应手机屏幕宽度
  16. 【华为OD机试 2023最新 】 网上商城优惠活动(C++)
  17. 解决新电脑开机无法跳过联网(找不到网络控制流进程)
  18. CASS10.1任意断面(渠道断面)土方计算
  19. c++基础知识点(6)类的继承,构造,析构顺序,虚继承等
  20. Linux Vm虚拟机配置环境错误 在登录界面循环往复,登录不进去问题

热门文章

  1. 实现二进制数到十进制的转换#
  2. 基于行的帧内编码快速算法
  3. 面试官:Zookeeper怎么解决读写、双写并发不一致问题,以及共享锁的实现原理?
  4. 千寻位置服务器地址在哪打开,千寻位置服务器地址和端口
  5. 计算机毕业设计Java宠物用品交易网站(源码+系统+mysql数据库+lw文档)
  6. java计算机毕业设计计算机类专业考研交流学习平台MyBatis+系统+LW文档+源码+调试部署
  7. (Android学习)Bundle
  8. AD画原理图去除网格线
  9. ai如何复制文字并对齐_AI里面怎么将图片中的文字对?
  10. HTML标签图文详解(三)