ylbtech-Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class
1.返回顶部
 
2.返回顶部
 
3.返回顶部
 
4.返回顶部
1、
  1. package com.ylbtech.api.platform.util;
  2.  
  3. import org.springframework.data.redis.core.RedisTemplate;
  4. import org.springframework.stereotype.Component;
  5.  
  6. import javax.annotation.Resource;
  7. import javax.validation.constraints.NotBlank;
  8. import java.time.Duration;
  9. import java.util.*;
  10. import java.util.concurrent.TimeUnit;
  11.  
  12. /**
  13. * Redis工具
  14. *
  15.  
  16. */
  17. @Component
  18. public class RedisUtils {
  19. @Resource private RedisTemplate<String, Object> redisTemplate;
  20.  
  21. // =============================common============================
  22.  
  23. /**
  24. * 设置缓存失效时间
  25. *
  26. * @param key 键
  27. * @param timeout 时间
  28. * @return {Boolean}
  29. */
  30. public Boolean setExpire(@NotBlank final String key, @NotBlank final Duration timeout) {
  31. if (timeout.getSeconds() > 0) {
  32. return this.redisTemplate.expire(key, timeout.getSeconds(), TimeUnit.SECONDS);
  33. }
  34. return false;
  35. }
  36.  
  37. /**
  38. * 获取缓存失效时间
  39. *
  40. * @param key 键
  41. * @return 时间(秒) 0为永久有效
  42. */
  43. public Long getExpire(@NotBlank final String key) {
  44. return this.redisTemplate.getExpire(key, TimeUnit.SECONDS);
  45. }
  46.  
  47. /**
  48. * key 是否存在
  49. *
  50. * @param key 键
  51. * @return {Boolean}
  52. */
  53. public Boolean hasKey(@NotBlank final String key) {
  54. return this.redisTemplate.hasKey(key);
  55. }
  56.  
  57. /**
  58. * 删除缓存
  59. *
  60. * @param keys 键
  61. */
  62. public Boolean delete(@NotBlank final String... keys) {
  63. return keys.length
  64. == Optional.ofNullable(this.redisTemplate.delete(Arrays.asList(keys))).orElse(-1L);
  65. }
  66.  
  67. // ============================String=============================
  68.  
  69. /**
  70. * 获取普通缓存
  71. *
  72. * @param key 键
  73. * @return 值
  74. */
  75. public Object getValue(@NotBlank final String key) {
  76. return this.redisTemplate.opsForValue().get(key);
  77. }
  78.  
  79. /**
  80. * 设置普通缓存
  81. *
  82. * @param key 键
  83. * @param value 值
  84. */
  85. public void setValue(@NotBlank final String key, @NotBlank final Object value) {
  86. this.redisTemplate.opsForValue().set(key, value);
  87. }
  88.  
  89. /**
  90. * 设置普通缓存
  91. *
  92. * @param key 键
  93. * @param value 值
  94. * @param timeout 时间 小于等于0时将设为无限期
  95. */
  96. public void setValue(
  97. @NotBlank final String key, @NotBlank final Object value, @NotBlank final Duration timeout) {
  98. this.redisTemplate.opsForValue().set(key, value, timeout);
  99. }
  100.  
  101. /**
  102. * 递增
  103. *
  104. * @param key 键
  105. * @param delta 要增加几(大于0)
  106. * @return 加上指定值之后 key 的值
  107. */
  108. public Long incrementValue(@NotBlank final String key, @NotBlank final long delta) {
  109. if (delta > 0) {
  110. throw new RuntimeException("递增因子必须大于0");
  111. }
  112. return this.redisTemplate.opsForValue().increment(key, delta);
  113. }
  114.  
  115. /**
  116. * 递减
  117. *
  118. * @param key 键
  119. * @param delta 要减少几(小于0)
  120. * @return 减少指定值之后 key 的值
  121. */
  122. public Long decrementValue(@NotBlank final String key, @NotBlank final long delta) {
  123. if (delta < 0) {
  124. throw new RuntimeException("递减因子必须大于0");
  125. }
  126. return this.redisTemplate.opsForValue().increment(key, -delta);
  127. }
  128.  
  129. // ================================Map=================================
  130.  
  131. /**
  132. * HashGet
  133. *
  134. * @param key 键
  135. * @param item 项
  136. * @return 值
  137. */
  138. public Object getHash(@NotBlank final String key, @NotBlank final String item) {
  139. return this.redisTemplate.opsForHash().get(key, item);
  140. }
  141.  
  142. /**
  143. * 获取hashKey对应的所有键值
  144. *
  145. * @param key 键
  146. * @return 对应的多个键值
  147. */
  148. public Map<Object, Object> getHash(@NotBlank final String key) {
  149. return this.redisTemplate.opsForHash().entries(key);
  150. }
  151.  
  152. /**
  153. * HashSet
  154. *
  155. * @param key 键
  156. * @param map 对应多个键值
  157. */
  158. public void putHash(@NotBlank final String key, @NotBlank final Map<String, Object> map) {
  159. this.redisTemplate.opsForHash().putAll(key, map);
  160. }
  161.  
  162. /**
  163. * HashSet 并设置时间
  164. *
  165. * @param key 键
  166. * @param map 对应多个键值
  167. * @param timeout 时间
  168. */
  169. public void putHash(
  170. @NotBlank final String key,
  171. @NotBlank final Map<String, Object> map,
  172. @NotBlank final Duration timeout) {
  173. this.redisTemplate.opsForHash().putAll(key, map);
  174. this.setExpire(key, timeout);
  175. }
  176.  
  177. /**
  178. * 向一张hash表中放入数据,如果不存在将创建
  179. *
  180. * @param key 键
  181. * @param item 项
  182. * @param value 值
  183. */
  184. public void putHash(
  185. @NotBlank final String key, @NotBlank final String item, @NotBlank final Object value) {
  186. this.redisTemplate.opsForHash().put(key, item, value);
  187. }
  188.  
  189. /**
  190. * 向一张hash表中放入数据,如果不存在将创建
  191. *
  192. * @param key 键
  193. * @param item 项
  194. * @param value 值
  195. * @param timeout 时间 注意:如果已存在的hash表有时间,这里将会替换原有的时间
  196. */
  197. public void putHash(
  198. @NotBlank final String key,
  199. @NotBlank final String item,
  200. @NotBlank final Object value,
  201. @NotBlank final Duration timeout) {
  202. this.redisTemplate.opsForHash().put(key, item, value);
  203. this.setExpire(key, timeout);
  204. }
  205.  
  206. /**
  207. * 删除hash表中的值
  208. *
  209. * @param key 键
  210. * @param item 项
  211. */
  212. public void deleteHash(@NotBlank final String key, @NotBlank final Object... item) {
  213. this.redisTemplate.opsForHash().delete(key, item);
  214. }
  215.  
  216. /**
  217. * 判断hash表中是否有该项的值
  218. *
  219. * @param key 键
  220. * @param item 项
  221. * @return {Boolean}
  222. */
  223. public Boolean hasKeyHash(@NotBlank final String key, @NotBlank final String item) {
  224. return this.redisTemplate.opsForHash().hasKey(key, item);
  225. }
  226.  
  227. /**
  228. * hash递增 如果不存在,就会创建一个 并把新增后的值返回
  229. *
  230. * @param key 键
  231. * @param item 项
  232. * @param by 要增加几(大于0)
  233. * @return 加上指定值之后 key 的值
  234. */
  235. public Double incrementHash(
  236. @NotBlank final String key, @NotBlank final String item, @NotBlank final double by) {
  237. return this.redisTemplate.opsForHash().increment(key, item, by);
  238. }
  239.  
  240. /**
  241. * hash递减
  242. *
  243. * @param key 键
  244. * @param item 项
  245. * @param by 要减少记(小于0)
  246. * @return 减少指定值之后 key 的值
  247. */
  248. public Double decrementHash(
  249. @NotBlank final String key, @NotBlank final String item, @NotBlank final double by) {
  250. return this.redisTemplate.opsForHash().increment(key, item, -by);
  251. }
  252.  
  253. // ============================set=============================
  254.  
  255. /**
  256. * 根据 key 获取 Set 中的所有值
  257. *
  258. * @param key 键
  259. * @return Set<Object>
  260. */
  261. public Set<Object> getSet(@NotBlank final String key) {
  262. return this.redisTemplate.opsForSet().members(key);
  263. }
  264.  
  265. /**
  266. * 根据 value 从一个 set 中查询,是否存在
  267. *
  268. * @param key 键
  269. * @param value 值
  270. * @return {Boolean}
  271. */
  272. public Boolean hasKeySet(@NotBlank final String key, @NotBlank final Object value) {
  273. return this.redisTemplate.opsForSet().isMember(key, value);
  274. }
  275.  
  276. /**
  277. * 将数据放入set缓存
  278. *
  279. * @param key 键
  280. * @param values 值
  281. * @return 放入个数
  282. */
  283. public Long addSet(@NotBlank final String key, @NotBlank final Object... values) {
  284. return this.redisTemplate.opsForSet().add(key, values);
  285. }
  286.  
  287. /**
  288. * 将set数据放入缓存
  289. *
  290. * @param key 键
  291. * @param timeout 时间
  292. * @param values 值
  293. * @return 放入个数
  294. */
  295. public Long addSet(
  296. @NotBlank final String key,
  297. @NotBlank final Duration timeout,
  298. @NotBlank final Object... values) {
  299. final Long num = this.redisTemplate.opsForSet().add(key, values);
  300. this.setExpire(key, timeout);
  301. return num;
  302. }
  303.  
  304. /**
  305. * 获取set缓存的长度
  306. *
  307. * @param key 键
  308. * @return 缓存的长度
  309. */
  310. public Long getSetSize(@NotBlank final String key) {
  311. return this.redisTemplate.opsForSet().size(key);
  312. }
  313.  
  314. /**
  315. * 移除值为value的
  316. *
  317. * @param key 键
  318. * @param values 值
  319. * @return 移除个数
  320. */
  321. public Long removeSet(@NotBlank final String key, @NotBlank final Object... values) {
  322. return this.redisTemplate.opsForSet().remove(key, values);
  323. }
  324. // ===============================list=================================
  325.  
  326. /**
  327. * 获取list缓存的内容
  328. *
  329. * @param key 键
  330. * @param start 开始
  331. * @param end 结束 0 到 -1代表所有值
  332. * @return list缓存的内容
  333. */
  334. public List<Object> getList(
  335. @NotBlank final String key, @NotBlank final Long start, @NotBlank final Long end) {
  336. return this.redisTemplate.opsForList().range(key, start, end);
  337. }
  338.  
  339. /**
  340. * 获取list缓存的长度
  341. *
  342. * @param key 键
  343. * @return list缓存的长度
  344. */
  345. public Long getListSize(@NotBlank final String key) {
  346. return this.redisTemplate.opsForList().size(key);
  347. }
  348.  
  349. /**
  350. * 通过索引 获取list中的值
  351. *
  352. * @param key 键
  353. * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
  354. * @return list中的值
  355. */
  356. public Object getListIndex(@NotBlank final String key, @NotBlank final Long index) {
  357. return this.redisTemplate.opsForList().index(key, index);
  358. }
  359.  
  360. /**
  361. * 将list放入缓存
  362. *
  363. * @param key 键
  364. * @param value 值
  365. * @return 放入个数
  366. */
  367. public Long pushList(@NotBlank final String key, @NotBlank final Object value) {
  368. return this.redisTemplate.opsForList().rightPush(key, value);
  369. }
  370.  
  371. /**
  372. * 将list放入缓存
  373. *
  374. * @param key 键
  375. * @param value 值
  376. * @param timeout 时间
  377. */
  378. public Long pushList(
  379. @NotBlank final String key, @NotBlank final Object value, @NotBlank final Duration timeout) {
  380. final Long num = this.redisTemplate.opsForList().rightPush(key, value);
  381. this.setExpire(key, timeout);
  382. return num;
  383. }
  384.  
  385. /**
  386. * 将list放入缓存
  387. *
  388. * @param key 键
  389. * @param value 值
  390. * @return 放入个数
  391. */
  392. public Long pushList(@NotBlank final String key, @NotBlank final List<Object> value) {
  393. return this.redisTemplate.opsForList().rightPushAll(key, value);
  394. }
  395.  
  396. /**
  397. * 将list放入缓存
  398. *
  399. * @param key 键
  400. * @param value 值
  401. * @param timeout 时间
  402. * @return 放入个数
  403. */
  404. public Long pushList(
  405. @NotBlank final String key,
  406. @NotBlank final List<Object> value,
  407. @NotBlank final Duration timeout) {
  408. final Long num = this.redisTemplate.opsForList().rightPushAll(key, value);
  409. this.setExpire(key, timeout);
  410. return num;
  411. }
  412.  
  413. /**
  414. * 根据索引修改 list 中的某条数据
  415. *
  416. * @param key 键
  417. * @param index 索引
  418. * @param value 值
  419. */
  420. public void updateListIndex(
  421. @NotBlank final String key, @NotBlank final Long index, @NotBlank final Object value) {
  422. this.redisTemplate.opsForList().set(key, index, value);
  423. }
  424.  
  425. /**
  426. * 移除N个值为value
  427. *
  428. * @param key 键
  429. * @param count 移除多少个
  430. * @param value 值
  431. * @return 移除个数
  432. */
  433. public Long removeList(
  434. @NotBlank final String key, @NotBlank final Long count, @NotBlank final Object value) {
  435. return this.redisTemplate.opsForList().remove(key, count, value);
  436. }
  437. }
2、
5.返回顶部
 
 
6.返回顶部
 
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class的更多相关文章

  1. Java魔法堂:深入正则表达式API

    目录 一.前言 二.正则表达式的使用诉求 三.java.util.regex包 四.java.lang.String实例 五.最短路径实现诉求 六.Java支持的正则表达式功能语法 七.总结 八.参考 ...

  2. Expo大作战(二十七)--expo sdk api之Util(expo自带工具类),tackSnapshotAsync,Svg,SQLite

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  3. BMap:WEB 服务API

    ylbtech-Map-Baidu: WEB 服务API 百度地图Web服务API为开发者提供http/https接口,即开发者通过http/https形式发起检索请求,获取返回json或xml格式的 ...

  4. Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage

    ylbtech-Java-Class-Miniprogram:com.ylbtech.common.utils.miniprogram.TemplateMessage 1.返回顶部 1.1. pack ...

  5. Java并发编程:线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  6. Java设计模式4:单例模式

    前言 非常重要,单例模式是各个Java项目中必不可少的一种设计模式.本文的关注点将重点放在单例模式的写法以及每种写法的线程安全性上.所谓"线程安全性"的意思就是保证在创建单例对象的 ...

  7. Atitit 实现java的linq 以及与stream api的比较

    Atitit 实现java的linq 以及与stream api的比较 1.1. Linq 和stream api的关系,以及主要优缺点1 1.2. Linq 与stream api的适用场景1 1. ...

  8. Java魔法堂:类加载器入了个门

    一.前言 <Java魔法堂:类加载机制入了个门>中提及整个类加载流程中只有加载阶段作为码农的我们可以入手干预,其余均由JVM处理.本文将记录加载阶段的核心组件——类加载器的相关信息,以便日 ...

  9. Java并发编程:如何创建线程?

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

随机推荐

  1. 【运维】使用Serv-U搭建FTP服务器

    1.先安装好Serv-U,并作为系统服务安装 2.打开Serv-U,新建一个域 3.添加用户 4.解决阿里云专有网络的一个问题 遇到一个情景:需要使用Serv-U进行FTP更新软件,其中使用PASV的 ...

  2. JavaScript--字符串与JSON对象相互转换

    JSON.parse() 兼容性:Chrome,Firefox (Gecko) 3.5 (1.9.1),IE 8.0,Safari 4.0 JSON.parse('[1, 5, "false ...

  3. 20175223 MySQL

    目录 完成结果 要求 1 :导入world.sql 要求 2 :CityWanna.java CityWanna.java 要求 3 :CountryWanna.java CountryWanna.j ...

  4. idea Maven 一键 mvn clean package

    文章目录 方法一 方法二 方法一 方法二

  5. Linux下tomcat启动慢,阻塞

    声明:本文为转载,请尊重版权,原文地址: https://www.cnblogs.com/songjinju/p/7505564.html 这两天在linux部署完tomcat以后,发现每次启动都非常 ...

  6. js实现图片预览、压缩、上传

    先看几个对象:Blob.ArrayBuffer.File.fileReader.formData 详细解释请参考:https://www.cnblogs.com/youhong/p/10875190. ...

  7. 使用soapui进行webservice接口测试

    一.web service(SOAP)与HTTP接口的区别   1.什么是web service WebService就是Web服务的意思,对应的应用层协议为SOAP(相当于HTTP协议),可理解为远 ...

  8. ldap认证服务的搭建

    1. Ldap服务介绍 LDAP 全称轻量级目录访问协议(英文:Lightweight Directory Access Protocol),是一个运行在 TCP/IP 上的目录访问协议.目录是一个特 ...

  9. [Java Performance] 线程及同步的性能之线程池/ThreadPoolExecutors/ForkJoinPool

    线程池和ThreadPoolExecutors   虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理 ...

  10. 线性方程组迭代算法——Gauss-Seidel迭代算法的python实现

    原理: 请看本人博客:线性方程组的迭代求解算法——原理 代码: import numpy as np max=100#迭代次数上限 Delta=0.01 m=2#阶数:矩阵为2阶 n=3#维数:3X3 ...