对 Redis 5种数据结构的常用操作进一步封装 RedisUtil

  1. import java.time.Duration;
  2. import java.util.Collection;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Set;
  7. import java.util.concurrent.TimeUnit;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.cache.annotation.CachingConfigurerSupport;
  10. import org.springframework.data.redis.connection.DataType;
  11. import org.springframework.data.redis.core.StringRedisTemplate;
  12. import org.springframework.stereotype.Component;
  13. /**
  14. * 对 Redis 5种数据结构进一步封装操作
  15. * 1. String 可以是字符串、整数或者浮点数
  16. * 2. List 一个链表,链表上的每个节点都包含了一个字符串
  17. * 3. Set 包含字符串的无序集合,并且被包含的每个字符串都是独一无二的、各不相同
  18. * 4. Hash 包含键值对的无序散列表
  19. * 5. Zset 字符串成员与浮点数分值之间的有序映射,元素的排列顺序由分值的大小决定
  20. *
  21. */
  22. @Component
  23. public class RedisUtil extends CachingConfigurerSupport{
  24. @Autowired
  25. private StringRedisTemplate stringRedisTemplate;
  26. public StringRedisTemplate getStringRedisTemplate() {
  27. return stringRedisTemplate;
  28. }
  29. // ==================== key 常用相关操作 ==================== //
  30. /**
  31. * 【Key】 删除 key
  32. * @param key
  33. * @return 成功删除存在的key时为 true
  34. */
  35. public Boolean delete(String key) {
  36. return stringRedisTemplate.delete(key);
  37. }
  38. /**
  39. * 【Key】 批量删除 key
  40. * @param keys
  41. * @return 返回成功删除的数量
  42. */
  43. public Long delete(Collection<String> keys) {
  44. return stringRedisTemplate.delete(keys);
  45. }
  46. /**
  47. * 【Key】 序列化 key
  48. * @param key
  49. * @return
  50. */
  51. public byte[] dump(String key) {
  52. return stringRedisTemplate.dump(key);
  53. }
  54. /**
  55. * 【Key】 是否存在 key
  56. * @return
  57. */
  58. public Boolean hasKey(String key) {
  59. return stringRedisTemplate.hasKey(key);
  60. }
  61. /**
  62. * 【Key】 设置 key 过期时间
  63. * @param key
  64. * @param timeout
  65. * @param unit 时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
  66. * @return
  67. */
  68. public Boolean expire(String key, long timeout, TimeUnit unit) {
  69. return stringRedisTemplate.expire(key, timeout, unit);
  70. }
  71. /**
  72. * 【Key】 设置 key 过期时间
  73. * @param key
  74. * @param date
  75. * @return
  76. */
  77. public Boolean expireAt(String key, Date date) {
  78. return stringRedisTemplate.expireAt(key, date);
  79. }
  80. /**
  81. * 【Key】 获取 key 的剩余过期时间
  82. * @param key
  83. * @param timeUnit
  84. * @return
  85. */
  86. public Long getExpire(String key, TimeUnit timeUnit) {
  87. return stringRedisTemplate.getExpire(key, timeUnit);
  88. }
  89. /**
  90. * 【Key】 获取 key 的剩余过期时间
  91. * @param key
  92. * @return
  93. */
  94. public Long getExpire(String key) {
  95. return stringRedisTemplate.getExpire(key);
  96. }
  97. /**
  98. * 【Key】 移除 key 的过期时间, key 将不会过期
  99. * @param key
  100. * @return
  101. */
  102. public Boolean persist(String key) {
  103. return stringRedisTemplate.persist(key);
  104. }
  105. /**
  106. * 【Key】 查找匹配的 key
  107. * @param pattern
  108. * @return
  109. */
  110. public Set<String> keys(String pattern) {
  111. return stringRedisTemplate.keys(pattern);
  112. }
  113. /**
  114. * 【Key】 将当前 Redis 数据库分片中的 key 移到指定的数据库分片中
  115. * @param key
  116. * @param dbIndex Redis 数据库分片索引
  117. * @return
  118. */
  119. public Boolean move(String key, int dbIndex) {
  120. return stringRedisTemplate.move(key, dbIndex);
  121. }
  122. /**
  123. * 【Key】 从当前 Redis 数据库分片中随机返回一个 key
  124. * @return
  125. */
  126. public String randomKey() {
  127. return stringRedisTemplate.randomKey();
  128. }
  129. /**
  130. * 【Key】 重命名 key 的名称
  131. * @param oldKey 当前 key 名称
  132. * @param newKey 新的 key 名称
  133. */
  134. public void rename(String oldKey, String newKey) {
  135. stringRedisTemplate.rename(oldKey, newKey);
  136. }
  137. /**
  138. * 【Key】 仅当 newKey 不存在时,将 oldKey 重命名为 newKey
  139. * @param oldKey
  140. * @param newKey
  141. * @return
  142. */
  143. public Boolean renameIfAbsent(String oldKey, String newKey) {
  144. return stringRedisTemplate.renameIfAbsent(oldKey, newKey);
  145. }
  146. /**
  147. * 【Key】 返回 key 所储存的值的类型
  148. * @param key
  149. * @return
  150. */
  151. public DataType type(String key) {
  152. return stringRedisTemplate.type(key);
  153. }
  154. // ==================== String 常用相关操作 ==================== //
  155. /**
  156. * 【String】 设置指定 key 的值
  157. * @param key
  158. * @param value
  159. */
  160. public void set(String key, String value) {
  161. stringRedisTemplate.opsForValue().set(key, value);
  162. }
  163. /**
  164. * 【String】 只有在 key 不存在时才设置 key 的值
  165. * @param key
  166. * @param value
  167. */
  168. public Boolean setIfAbsent(String key, String value) {
  169. return stringRedisTemplate.opsForValue().setIfAbsent(key, value);
  170. }
  171. /**
  172. * 【String】 设置指定 key 的值,并指定过期时间
  173. * @param key
  174. * @param value
  175. * @param timeout
  176. */
  177. public void set(String key, String value, Duration timeout) {
  178. stringRedisTemplate.opsForValue().set(key, value, timeout);
  179. }
  180. /**
  181. * 【String】 只有在 key 不存在时才设置 key 的值,并指定过期时间
  182. * @param key
  183. * @param value
  184. * @param timeout
  185. */
  186. public Boolean setIfAbsent(String key, String value, Duration timeout) {
  187. return stringRedisTemplate.opsForValue().setIfAbsent(key, value, timeout);
  188. }
  189. /**
  190. * 【String】 设置指定 key 的值,并指定过期时间
  191. * @param key
  192. * @param value
  193. * @param timeout
  194. * @param unit 时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
  195. */
  196. public void set(String key, String value, long timeout, TimeUnit unit) {
  197. stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
  198. }
  199. /**
  200. * 【String】 只有在 key 不存在时才设置 key 的值,并指定过期时间
  201. * @param key
  202. * @param value
  203. * @param timeout
  204. * @param unit
  205. */
  206. public Boolean setIfAbsent(String key, String value, long timeout, TimeUnit unit) {
  207. return stringRedisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit);
  208. }
  209. /**
  210. * 【String】 批量设置 key-value 对
  211. * @param map
  212. */
  213. public void multiSet(Map<String, String> map) {
  214. stringRedisTemplate.opsForValue().multiSet(map);
  215. }
  216. /**
  217. * 【String】 批量设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
  218. * @param map
  219. */
  220. public Boolean multiSetIfAbsent(Map<String, String> map) {
  221. return stringRedisTemplate.opsForValue().multiSetIfAbsent(map);
  222. }
  223. /**
  224. * 【String】 获取 key 的长度
  225. * @param key
  226. * @return
  227. */
  228. public Long size(String key) {
  229. return stringRedisTemplate.opsForValue().size(key);
  230. }
  231. /**
  232. * 【String】 增加(自增长), 负数则为自减
  233. * @param key
  234. * @param delta
  235. * @return 返回自增或自减后的值
  236. */
  237. public Long increment(String key, long delta) {
  238. return stringRedisTemplate.opsForValue().increment(key, delta);
  239. }
  240. /**
  241. * 【String】 增加(自增长), 负数则为自减
  242. * @param key
  243. * @param delta
  244. * @return 返回自增或自减后的值
  245. */
  246. public Double increment(String key, double delta) {
  247. return stringRedisTemplate.opsForValue().increment(key, delta);
  248. }
  249. /**
  250. * 【String】 追加值到末尾
  251. * @param key
  252. * @param value
  253. * @return
  254. */
  255. public Integer append(String key, String value) {
  256. return stringRedisTemplate.opsForValue().append(key, value);
  257. }
  258. /**
  259. * 【String】 获取指定 key 的值
  260. * @param key
  261. * @return
  262. */
  263. public String get(String key) {
  264. return stringRedisTemplate.opsForValue().get(key);
  265. }
  266. /**
  267. * 【String】 返回存储在字符串值的子串key,由偏移确定start和end(两者都包括)
  268. * 0表示第一个字符,1表示第二个字符,-1表示最后一个字符,-2表示倒数第二个字符,依此类推
  269. * @param key
  270. * @param start
  271. * @param end
  272. * @return
  273. */
  274. public String get(String key, long start, long end) {
  275. return stringRedisTemplate.opsForValue().get(key, start, end);
  276. }
  277. /**
  278. * 【String】 设置key到value,并返回 key 的旧值
  279. * @param key
  280. * @param value
  281. * @return
  282. */
  283. public String getAndSet(String key, String value) {
  284. return stringRedisTemplate.opsForValue().getAndSet(key, value);
  285. }
  286. /**
  287. * 【String】 批量获取 key 的值
  288. * @param keys
  289. * @return
  290. */
  291. public List<String> multiGet(Collection<String> keys) {
  292. return stringRedisTemplate.opsForValue().multiGet(keys);
  293. }
  294. // ==================== List 常用相关操作 ==================== //
  295. /**
  296. * 【List】 获取 key 的 index 索引的值
  297. * @param key
  298. * @param index
  299. * @return
  300. */
  301. public String lIndex(String key, long index) {
  302. return stringRedisTemplate.opsForList().index(key, index);
  303. }
  304. /**
  305. * 【List】 获取 key 列表指定范围内的值
  306. * @param key
  307. * @param start 开始位置,0表示第一位
  308. * @param end 结束位置,-1表示最后一位
  309. * @return
  310. */
  311. public List<String> lRange(String key, long start, long end) {
  312. return stringRedisTemplate.opsForList().range(key, start, end);
  313. }
  314. /**
  315. * 【List】 将一个值插入到列表头部
  316. * @param key
  317. * @param value
  318. * @return 返回列表的长度
  319. */
  320. public Long lLeftPush(String key, String value) {
  321. return stringRedisTemplate.opsForList().leftPush(key, value);
  322. }
  323. /**
  324. * 【List】 在 key 列表中的指定的值(pivot)的前一位添加指定的值(value),仅当 pivot 存在时有效
  325. * @param key
  326. * @param pivot
  327. * @param value
  328. * @return 返回列表的长度
  329. */
  330. public Long lLeftPush(String key, String pivot, String value) {
  331. return stringRedisTemplate.opsForList().leftPush(key, pivot, value);
  332. }
  333. /**
  334. * 【List】 将一个或多个值插入到列表头部
  335. * @param key
  336. * @param values
  337. * @return 返回列表的长度
  338. */
  339. public Long lLeftPushAll(String key, String... values) {
  340. return stringRedisTemplate.opsForList().leftPushAll(key, values);
  341. }
  342. /**
  343. * 【List】 将一个或多个值插入到列表头部
  344. * @param key
  345. * @param values
  346. * @return 返回列表的长度
  347. */
  348. public Long lLeftPushAll(String key, Collection<String> values) {
  349. return stringRedisTemplate.opsForList().leftPushAll(key, values);
  350. }
  351. /**
  352. * 【List】 仅当 key 列表存在的时候才添加值到列表头部
  353. * @param key
  354. * @param value
  355. * @return 返回列表的长度
  356. */
  357. public Long leftPushIfPresent(String key, String value) {
  358. return stringRedisTemplate.opsForList().leftPushIfPresent(key, value);
  359. }
  360. /**
  361. * 【List】 将一个值添加到列表尾部
  362. * @param key
  363. * @param value
  364. * @return 返回列表的长度
  365. */
  366. public Long lRightPush(String key, String value) {
  367. return stringRedisTemplate.opsForList().rightPush(key, value);
  368. }
  369. /**
  370. * 【List】 在 key 列表中的指定的值(pivot)的后一位添加指定的值(value),仅当 pivot 存在时有效
  371. * @param key
  372. * @param pivot
  373. * @param value
  374. * @return 返回列表的长度
  375. */
  376. public Long lRightPush(String key, String pivot, String value) {
  377. return stringRedisTemplate.opsForList().rightPush(key, pivot, value);
  378. }
  379. /**
  380. * 【List】 将一个或多个值插入到列表尾部
  381. * @param key
  382. * @param values
  383. * @return 返回列表的长度
  384. */
  385. public Long lRightPushAll(String key, String... values) {
  386. return stringRedisTemplate.opsForList().rightPushAll(key, values);
  387. }
  388. /**
  389. * 【List】 将一个或多个值插入到列表尾部
  390. * @param key
  391. * @param values
  392. * @return 返回列表的长度
  393. */
  394. public Long lRightPushAll(String key, Collection<String> values) {
  395. return stringRedisTemplate.opsForList().rightPushAll(key, values);
  396. }
  397. /**
  398. * 【List】 仅当 key 列表存在的时候才添加值到列表尾部
  399. * @param key
  400. * @param value
  401. * @return 返回列表的长度
  402. */
  403. public Long lRightPushIfPresent(String key, String value) {
  404. return stringRedisTemplate.opsForList().rightPushIfPresent(key, value);
  405. }
  406. /**
  407. * 【List】 设置 key 列表的index索引上的值
  408. * @param key
  409. * @param index 索引
  410. * @param value
  411. */
  412. public void lSet(String key, long index, String value) {
  413. stringRedisTemplate.opsForList().set(key, index, value);
  414. }
  415. /**
  416. * 【List】 移除并获取列表第一个元素
  417. * @param key
  418. * @return
  419. */
  420. public String lLeftPop(String key) {
  421. return stringRedisTemplate.opsForList().leftPop(key);
  422. }
  423. /**
  424. * 【List】 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
  425. * @param key
  426. * @param timeout 时间
  427. * @param unit 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
  428. * @return
  429. */
  430. public String lLeftPop(String key, long timeout, TimeUnit unit) {
  431. return stringRedisTemplate.opsForList().leftPop(key, timeout, unit);
  432. }
  433. /**
  434. * 【List】 移除并获取列表最后一个元素
  435. * @param key
  436. * @return
  437. */
  438. public String lRightPop(String key) {
  439. return stringRedisTemplate.opsForList().rightPop(key);
  440. }
  441. /**
  442. * 【List】 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
  443. * @param key
  444. * @param timeout 时间
  445. * @param unit 时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
  446. * @return
  447. */
  448. public String lRightPop(String key, long timeout, TimeUnit unit) {
  449. return stringRedisTemplate.opsForList().rightPop(key, timeout, unit);
  450. }
  451. /**
  452. * 【List】 从 sourcekey 的列表中删除最后一个元素,将其附加到 destinationkey 的列表中并返回其值。
  453. * @return
  454. */
  455. public String lRightPopAndLeftPush(String sourceKey, String destinationKey) {
  456. return stringRedisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey);
  457. }
  458. /**
  459. * 【List】 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
  460. * @param sourceKey
  461. * @param destinationKey
  462. * @param timeout
  463. * @param unit 时间单位, 天:TimeUnit.DAYS 小时:TimeUnit.HOURS 分钟:TimeUnit.MINUTES 秒:TimeUnit.SECONDS 毫秒:TimeUnit.MILLISECONDS
  464. * @return
  465. */
  466. public String lRightPopAndLeftPush(String sourceKey, String destinationKey, long timeout, TimeUnit unit) {
  467. return stringRedisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit);
  468. }
  469. /**
  470. * 【List】 从存储在键中的列表中删除等于值的元素的第一个计数事件。
  471. * count > 0:从头不开始删除 count 个值等于 value 的元素
  472. * count < 0:从尾部开始删除 count 个值等于 value 的元素
  473. * count = 0:删除等于value的所有元素。
  474. * @param key
  475. * @param count
  476. * @param value
  477. * @return
  478. */
  479. public Long lRemove(String key, long count, Object value) {
  480. return stringRedisTemplate.opsForList().remove(key, count, value);
  481. }
  482. /**
  483. * 【List】 裁剪 list, 范围从索引0开始,都是包含关系
  484. * @param key
  485. * @param start
  486. * @param end
  487. */
  488. public void lTrim(String key, long start, long end) {
  489. stringRedisTemplate.opsForList().trim(key, start, end);
  490. }
  491. /**
  492. * 【List】 获取列表的长度
  493. * @param key
  494. * @return
  495. */
  496. public Long lSize(String key) {
  497. return stringRedisTemplate.opsForList().size(key);
  498. }
  499. // ==================== Set 常用相关操作 ==================== //
  500. /**
  501. * 【Set】 添加元素
  502. * @param key
  503. * @param values
  504. * @return 返回值成功添加元素的个数
  505. */
  506. public Long sAdd(String key, String... values) {
  507. return stringRedisTemplate.opsForSet().add(key, values);
  508. }
  509. /**
  510. * 【Set】 移除元素
  511. * @param key
  512. * @param values
  513. * @return 返回值成功移除元素的个数
  514. */
  515. public Long sRemove(String key, Object... values) {
  516. return stringRedisTemplate.opsForSet().remove(key, values);
  517. }
  518. /**
  519. * 【Set】 移除set集合中的一个随机元素并返回该元素
  520. * @param key
  521. * @return 返回被移除的随机元素
  522. */
  523. public String sPop(String key) {
  524. return stringRedisTemplate.opsForSet().pop(key);
  525. }
  526. /**
  527. * 【Set】 将value值从一个集合移动到另一个集合
  528. * @param key
  529. * @param value
  530. * @param destKey
  531. * @return
  532. */
  533. public Boolean sMove(String key, String value, String destKey) {
  534. return stringRedisTemplate.opsForSet().move(key, value, destKey);
  535. }
  536. /**
  537. * 【Set】 获取集合的大小
  538. * @param key
  539. * @return
  540. */
  541. public Long sSize(String key) {
  542. return stringRedisTemplate.opsForSet().size(key);
  543. }
  544. /**
  545. * 【Set】 判断集合是否包含value
  546. * @param key
  547. * @param value
  548. * @return
  549. */
  550. public Boolean sIsMember(String key, Object value) {
  551. return stringRedisTemplate.opsForSet().isMember(key, value);
  552. }
  553. /**
  554. * 【Set】 获取两个集合的交集
  555. * @param key
  556. * @param otherKey
  557. * @return
  558. */
  559. public Set<String> sIntersect(String key, String otherKey){
  560. return stringRedisTemplate.opsForSet().intersect(key, otherKey);
  561. }
  562. /**
  563. * 【Set】 获取key集合与多个集合的交集
  564. * @param key
  565. * @param otherKeys
  566. * @return
  567. */
  568. public Set<String> sIntersect(String key, Collection<String> otherKeys) {
  569. return stringRedisTemplate.opsForSet().intersect(key, otherKeys);
  570. }
  571. /**
  572. * 【Set】key集合与otherKey集合的交集存储到destKey集合中
  573. * @param key
  574. * @param otherKey
  575. * @param destKey
  576. * @return
  577. */
  578. public Long sIntersectAndStore(String key, String otherKey, String destKey) {
  579. return stringRedisTemplate.opsForSet().intersectAndStore(key, otherKey, destKey);
  580. }
  581. /**
  582. * 【Set】 key集合与多个集合的交集存储到destKey集合中
  583. * @param key
  584. * @param otherKeys
  585. * @param destKey
  586. * @return
  587. */
  588. public Long sIntersectAndStore(String key, Collection<String> otherKeys,String destKey) {
  589. return stringRedisTemplate.opsForSet().intersectAndStore(key, otherKeys, destKey);
  590. }
  591. /**
  592. * 【Set】 获取两个集合的并集
  593. * @param key
  594. * @param otherKeys
  595. * @return
  596. */
  597. public Set<String> sUnion(String key, String otherKeys) {
  598. return stringRedisTemplate.opsForSet().union(key, otherKeys);
  599. }
  600. /**
  601. * 【Set】 获取key集合与多个集合的并集
  602. * @param key
  603. * @param otherKeys
  604. * @return
  605. */
  606. public Set<String> sUnion(String key, Collection<String> otherKeys) {
  607. return stringRedisTemplate.opsForSet().union(key, otherKeys);
  608. }
  609. /**
  610. * 【Set】 key集合与otherKey集合的并集存储到destKey中
  611. * @param key
  612. * @param otherKey
  613. * @param destKey
  614. * @return
  615. */
  616. public Long sUnionAndStore(String key, String otherKey, String destKey) {
  617. return stringRedisTemplate.opsForSet().unionAndStore(key, otherKey, destKey);
  618. }
  619. /**
  620. * 【Set】key集合与多个集合的并集存储到destKey中
  621. * @param key
  622. * @param otherKeys
  623. * @param destKey
  624. * @return
  625. */
  626. public Long sUnionAndStore(String key, Collection<String> otherKeys,String destKey) {
  627. return stringRedisTemplate.opsForSet().unionAndStore(key, otherKeys, destKey);
  628. }
  629. /**
  630. * 【Set】获取两个集合的差集
  631. * @param key
  632. * @param otherKey
  633. * @return
  634. */
  635. public Set<String> sDifference(String key, String otherKey) {
  636. return stringRedisTemplate.opsForSet().difference(key, otherKey);
  637. }
  638. /**
  639. * 【Set】 获取key集合与多个集合的差集
  640. * @param key
  641. * @param otherKeys
  642. * @return
  643. */
  644. public Set<String> sDifference(String key, Collection<String> otherKeys) {
  645. return stringRedisTemplate.opsForSet().difference(key, otherKeys);
  646. }
  647. /**
  648. * 【Set】 key集合与otherKey集合的差集存储到destKey中
  649. * @param key
  650. * @param otherKey
  651. * @param destKey
  652. * @return
  653. */
  654. public Long sDifferenceAndStore(String key, String otherKey, String destKey) {
  655. return stringRedisTemplate.opsForSet().differenceAndStore(key, otherKey,destKey);
  656. }
  657. /**
  658. * 【Set】 key集合与多个集合的差集存储到destKey中
  659. * @param key
  660. * @param otherKeys
  661. * @param destKey
  662. * @return
  663. */
  664. public Long sDifferenceAndStore(String key, Collection<String> otherKeys,String destKey) {
  665. return stringRedisTemplate.opsForSet().differenceAndStore(key, otherKeys,destKey);
  666. }
  667. /**
  668. * 【Set】 获取集合所有元素
  669. * @param key
  670. * @return
  671. */
  672. public Set<String> sMembers(String key) {
  673. return stringRedisTemplate.opsForSet().members(key);
  674. }
  675. /**
  676. * 【Set】 随机获取集合中的一个元素
  677. * @param key
  678. * @return
  679. */
  680. public String sRandomMember(String key) {
  681. return stringRedisTemplate.opsForSet().randomMember(key);
  682. }
  683. /**
  684. * 【Set】 随机获取集合中的一个元素
  685. * @param key
  686. * @return
  687. */
  688. public List<String> sRandomMembers(String key, long count) {
  689. return stringRedisTemplate.opsForSet().randomMembers(key, count);
  690. }
  691. /**
  692. * 【Set】 随机获取集合中count个元素并且去除重复的
  693. * @param key
  694. * @param count
  695. * @return
  696. */
  697. public Set<String> sDistinctRandomMembers(String key, long count) {
  698. return stringRedisTemplate.opsForSet().distinctRandomMembers(key, count);
  699. }
  700. // ==================== Hash 常用相关操作 ==================== //
  701. /**
  702. * 【Hash】 获取存储在哈希表中指定字段的值
  703. * @param key
  704. * @param hashKey
  705. * @return
  706. */
  707. public Object hGet(String key, Object hashKey) {
  708. return stringRedisTemplate.opsForHash().get(key, hashKey);
  709. }
  710. /**
  711. * 【Hash】 获取所有给定字段的值
  712. * @param key
  713. * @return
  714. */
  715. public Map<Object, Object> hGetAll(String key) {
  716. return stringRedisTemplate.opsForHash().entries(key);
  717. }
  718. /**
  719. * 【Hash】 获取给定字段的值
  720. * @param key
  721. * @param hashKeys
  722. * @return
  723. */
  724. public List<Object> hMultiGet(String key, Collection<Object> hashKeys) {
  725. return stringRedisTemplate.opsForHash().multiGet(key, hashKeys);
  726. }
  727. /**
  728. * 【Hash】 将value设置到指定的key
  729. * @param key
  730. * @param hashKey
  731. * @param value
  732. */
  733. public void hPut(String key, String hashKey, String value) {
  734. stringRedisTemplate.opsForHash().put(key, hashKey, value);
  735. }
  736. /**
  737. * 【Hash】 将map设置到指定的key
  738. * @param key
  739. * @param map
  740. */
  741. public void hPutAll(String key, Map<String, String> map) {
  742. stringRedisTemplate.opsForHash().putAll(key, map);
  743. }
  744. /**
  745. * 【Hash】 仅当hashKey不存在时才设置
  746. * @return
  747. */
  748. public Boolean hPutIfAbsent(String key, String hashKey, String value) {
  749. return stringRedisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
  750. }
  751. /**
  752. * 【Hash】 删除一个或多个哈希表字段
  753. * @param key
  754. * @param hashKeys
  755. * @return
  756. */
  757. public Long hDelete(String key, Object... hashKeys) {
  758. return stringRedisTemplate.opsForHash().delete(key, hashKeys);
  759. }
  760. /**
  761. * 【Hash】 查看哈希表 key 中,指定的字段是否存在
  762. * @param key
  763. * @param hashKey
  764. * @return
  765. */
  766. public Boolean hExists(String key, Object hashKey) {
  767. return stringRedisTemplate.opsForHash().hasKey(key, hashKey);
  768. }
  769. /**
  770. * 【Hash】 为哈希表 key 中的指定字段的整数值加上增量 increment
  771. * @param key
  772. * @param hashKey
  773. * @param delta
  774. * @return 返回增量后的数值
  775. */
  776. public Long hIncrement(String key, Object hashKey, long delta) {
  777. return stringRedisTemplate.opsForHash().increment(key, hashKey, delta);
  778. }
  779. /**
  780. * 【Hash】 为哈希表 key 中的指定字段的整数值加上增量 increment
  781. * @param key
  782. * @param hashKey
  783. * @param delta
  784. * @return 返回增量后的数值
  785. */
  786. public Double hIncrement(String key, Object hashKey, double delta) {
  787. return stringRedisTemplate.opsForHash().increment(key, hashKey, delta);
  788. }
  789. /**
  790. * 【Hash】 获取所有哈希表中的字段
  791. * @param key
  792. * @return
  793. */
  794. public Set<Object> hKeys(String key) {
  795. return stringRedisTemplate.opsForHash().keys(key);
  796. }
  797. /**
  798. * 【Hash】 获取哈希表中字段的数量
  799. * @param key
  800. * @return
  801. */
  802. public Long hSize(String key) {
  803. return stringRedisTemplate.opsForHash().size(key);
  804. }
  805. /**
  806. * 【Hash】 获取哈希表中所有值
  807. * @param key
  808. * @return
  809. */
  810. public List<Object> hValues(String key) {
  811. return stringRedisTemplate.opsForHash().values(key);
  812. }
  813. }

© 2020-2025 www.chenhuazhan.com All Rights Reserved 备案号:桂ICP备17004487号-1 粤公网安备44030002005146