1. package com.zara.utils.redis;
    2. import org.apache.shiro.util.CollectionUtils;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.data.redis.core.RedisTemplate;
    5. import org.springframework.stereotype.Component;
    6. import java.util.List;
    7. import java.util.Map;
    8. import java.util.Set;
    9. import java.util.concurrent.TimeUnit;
    10. /**
    11. * @author : [Zara-cat]
    12. * @version : [v1.0]
    13. * @className : RedisUtil
    14. * @description : [redis 工具类,方便使用 redisTemplate 的 api]
    15. * @createTime : [2021/12/2 13:49]
    16. * @updateUser : [Zara-cat]
    17. * @updateTime : [2021/12/2 13:49]
    18. * @updateRemark : [描述说明本次修改内容]
    19. */
    20. @Component
    21. /**
    22. * common
    23. * 1. boolean expire(String key[键],long time[时间(秒)]) 指定缓存失效时间
    24. * 2. long getExpire(String key) 根据 key 获取过期时间
    25. * 3. boolean hasKey(String key) 判断 key 是否存在
    26. * 4. void del(String... key) 删除缓存 【可以一个或传递多个key】
    27. * String
    28. * 5. Object get(String key) 普通缓存获取
    29. * 6. boolean set(String key, Object value) 普通缓存放入
    30. * 7. boolean set(String key, Object value, long time) 普通缓存放入并设置时间
    31. * 时间(秒) time要大于0 如果time小于等于0 将设置无限期
    32. * 8. long incr(String key, long delta) 递增 参数2:要增加几(大于0)
    33. * 9. long decr(String key, long delta) 递减 参数2:要减少几(小于0)
    34. * 10. boolean append(String key,String str) 追加字符 参数2:要追加的字符
    35. * Map
    36. * 11. Object hget(String key, String item) HashGet 参数1:键 不能为null 参数2:item 项 不能为null
    37. * 12. Map<Object, Object> hmget(String key) 获取hashKey对应的所有键值
    38. * 13. hmset(String key, Map<String, Object> map) HashSet 参数1: 键 参数2: 对应多个键值
    39. * 14. boolean hmset(String key, Map<String, Object> map, long time) HashSet 并设置时间
    40. * 参数1:键 参数2:对应多个键值 参数3:时间(秒)
    41. * 15. boolean hset(String key, String item, Object value) 向一张hash表中放入数据,如果不存在将创建
    42. * 16. boolean hset(String key, String item, Object value, long time) 向一张hash表中放入数据,如果不存在将创建
    43. * time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
    44. * 17. void hdel(String key, Object... item) 删除hash表中的值 参数2:item 项 可以使多个 不能为null
    45. * 18. boolean hHasKey(String key, String item) 判断hash表中是否有该项的值
    46. * 19. double hincr(String key, String item, double by) hash递增 如果不存在,就会创建一个 并把新增后的值返回
    47. * 参数3:要增加几(大于0)
    48. * 20. double hdecr(String key, String item, double by) hash递减 参数3:要减少记(小于0)
    49. * set
    50. * 21. Set<Object> sGet(String key) 根据key获取Set中的所有值
    51. * 22. boolean sHasKey(String key, Object value) 根据value从一个set中查询,是否存在
    52. * 23. long sSet(String key, Object... values) 将数据放入set缓存
    53. * 24. long sSetAndTime(String key, long time, Object... values) 将set数据放入缓存
    54. * 参数2:时间(秒)
    55. * 25. long sGetSetSize(String key) 获取set缓存的长度
    56. * 26. long setRemove(String key, Object... values) 移除值为value的
    57. * list
    58. * 27. List<Object> lGet(String key, long start, long end) 获取list缓存的内容
    59. * 参数1:开始 参数2:结束 0到-1代表说有值
    60. * 28. long lGetListSize(String key) 获取list缓存的长度
    61. * 29. Object lGetIndex(String key, long index) 通过索引 获取list中的值
    62. * 参数2:index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
    63. * 30. boolean lSet(String key, Object value) 将值放入缓存
    64. * 31. boolean lSet(String key, Object value, long time) 将值放入缓存
    65. * 32. boolean lSet(String key, List<Object> value) 将list放入缓存
    66. * 33. boolean lSet(String key, List<Object> value, long time) 将list放入缓存
    67. * 34. boolean lUpdateIndex(String key, long index, Object value) 根据索引修改list中的某条数据
    68. * 参数1: 键 参数2:索引 参数3:值
    69. * 35. long lRemove(String key, long count, Object value) 移除N个值为value
    70. * 参数1:键 参数2:移除多少个 参数3:值 return:移除的个数
    71. *
    72. */
    73. public final class RedisUtil {
    74. @Autowired
    75. private RedisTemplate<String,Object> redisTemplate;
    76. // =============================common============================
    77. /**
    78. * 指定缓存失效时间
    79. * @param key 键
    80. * @param time 时间(秒)
    81. * @return 成功/失败
    82. */
    83. public boolean expire(String key,long time){
    84. try {
    85. if (time > 0){
    86. redisTemplate.expire(key,time, TimeUnit.SECONDS);
    87. }
    88. return true;
    89. } catch (Exception e) {
    90. e.printStackTrace();
    91. return false;
    92. }
    93. }
    94. /**
    95. * 根据 key 获取过期时间
    96. * @param key 键 不能为null
    97. * @return 时间(秒) 返回 0 代表为永久有效
    98. */
    99. public long getExpire(String key){
    100. return redisTemplate.getExpire(key,TimeUnit.SECONDS);
    101. }
    102. /**
    103. * 判断 key 是否存在
    104. * @param key 键
    105. * @return true 存在 false 不存在
    106. */
    107. public boolean hasKey(String key){
    108. try {
    109. return redisTemplate.hasKey(key);
    110. } catch (Exception e) {
    111. e.printStackTrace();
    112. return false;
    113. }
    114. }
    115. /**
    116. * 删除缓存
    117. * @param key 可以传一个值 或多个
    118. */
    119. public void del(String... key){
    120. if (key !=null && key.length > 0){
    121. if (key.length == 1){
    122. redisTemplate.delete(key[0]);
    123. }else {
    124. redisTemplate.delete(CollectionUtils.asList(key));
    125. }
    126. }
    127. }
    128. // ============================String=============================
    129. /**
    130. * 普通缓存获取
    131. * @param key 键
    132. * @return 值
    133. */
    134. public Object get(String key){
    135. return key == null ? null : redisTemplate.opsForValue().get(key);
    136. }
    137. /**
    138. * 普通缓存放入
    139. * @param key 键
    140. * @param value 值
    141. * @return true 成功 / false 失败
    142. */
    143. public boolean set(String key,Object value){
    144. try {
    145. redisTemplate.opsForValue().set(key,value);
    146. return true;
    147. } catch (Exception e) {
    148. e.printStackTrace();
    149. return false;
    150. }
    151. }
    152. /**
    153. * 普通缓存放入并设置时间
    154. * @param key 键
    155. * @param value 值
    156. * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
    157. * @return true 成功 / false 失败
    158. */
    159. public boolean set(String key,Object value,long time){
    160. try {
    161. if (time > 0){
    162. redisTemplate.opsForValue().set(key,value,time,TimeUnit.SECONDS);
    163. }else {
    164. set(key,value);
    165. }
    166. return true;
    167. } catch (Exception e) {
    168. e.printStackTrace();
    169. return false;
    170. }
    171. }
    172. /**
    173. * 递增
    174. * @param key 键
    175. * @param delta 要增加几(大于0)
    176. */
    177. public long incr(String key, long delta) {
    178. if (delta < 0) {
    179. throw new RuntimeException("递增因子必须大于0");
    180. }
    181. return redisTemplate.opsForValue().increment(key, delta);
    182. }
    183. /**
    184. * 递减
    185. * @param key 键
    186. * @param delta 要减少几(小于0)
    187. */
    188. public long decr(String key, long delta) {
    189. if (delta < 0) {
    190. throw new RuntimeException("递减因子必须大于0");
    191. }
    192. return redisTemplate.opsForValue().decrement(key,delta);
    193. //return redisTemplate.opsForValue().increment(key, -delta);
    194. }
    195. public long strLen(String key){
    196. return redisTemplate.opsForValue().get(key).toString().length();
    197. }
    198. /*
    199. * 追加字符
    200. * @param key 键
    201. * @param str 要追加的字符
    202. * */
    203. public boolean append(String key,String str){
    204. try {
    205. redisTemplate.opsForValue().append(key,str);
    206. return true;
    207. }catch (Exception e){
    208. return false;
    209. }
    210. }
    211. // ================================Map=================================
    212. /**
    213. * HashGet
    214. * @param key 键 不能为null
    215. * @param item 项 不能为null
    216. */
    217. public Object hget(String key, String item) {
    218. return redisTemplate.opsForHash().get(key, item);
    219. }
    220. /**
    221. * 获取hashKey对应的所有键值
    222. * @param key 键
    223. * @return 对应的多个键值
    224. */
    225. public Map<Object, Object> hmget(String key) {
    226. return redisTemplate.opsForHash().entries(key);
    227. }
    228. /**
    229. * HashSet
    230. * @param key 键
    231. * @param map 对应多个键值
    232. */
    233. public boolean hmset(String key, Map<String, Object> map) {
    234. try {
    235. redisTemplate.opsForHash().putAll(key, map);
    236. return true;
    237. } catch (Exception e) {
    238. e.printStackTrace();
    239. return false;
    240. }
    241. }
    242. /**
    243. * HashSet 并设置时间
    244. * @param key 键
    245. * @param map 对应多个键值
    246. * @param time 时间(秒)
    247. * @return true成功 false失败
    248. */
    249. public boolean hmset(String key, Map<String, Object> map, long time) {
    250. try {
    251. redisTemplate.opsForHash().putAll(key, map);
    252. if (time > 0) {
    253. expire(key, time);
    254. }
    255. return true;
    256. } catch (Exception e) {
    257. e.printStackTrace();
    258. return false;
    259. }
    260. }
    261. /**
    262. * 向一张hash表中放入数据,如果不存在将创建
    263. *
    264. * @param key 键
    265. * @param item 项
    266. * @param value 值
    267. * @return true 成功 false失败
    268. */
    269. public boolean hset(String key, String item, Object value) {
    270. try {
    271. redisTemplate.opsForHash().put(key, item, value);
    272. return true;
    273. } catch (Exception e) {
    274. e.printStackTrace();
    275. return false;
    276. }
    277. }
    278. /**
    279. * 向一张hash表中放入数据,如果不存在将创建
    280. *
    281. * @param key 键
    282. * @param item 项
    283. * @param value 值
    284. * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
    285. * @return true 成功 false失败
    286. */
    287. public boolean hset(String key, String item, Object value, long time) {
    288. try {
    289. redisTemplate.opsForHash().put(key, item, value);
    290. if (time > 0) {
    291. expire(item, time);
    292. }
    293. return true;
    294. } catch (Exception e) {
    295. e.printStackTrace();
    296. return false;
    297. }
    298. }
    299. /**
    300. * 删除hash表中的值
    301. *
    302. * @param key 键 不能为null
    303. * @param item 项 可以使多个 不能为null
    304. */
    305. public void hdel(String key, Object... item) {
    306. redisTemplate.opsForHash().delete(key, item);
    307. }
    308. /**
    309. * 判断hash表中是否有该项的值
    310. *
    311. * @param key 键 不能为null
    312. * @param item 项 不能为null
    313. * @return true 存在 false不存在
    314. */
    315. public boolean hHasKey(String key, String item) {
    316. return redisTemplate.opsForHash().hasKey(key, item);
    317. }
    318. /**
    319. * hash递增 如果不存在,就会创建一个 并把新增后的值返回
    320. *
    321. * @param key 键
    322. * @param item 项
    323. * @param by 要增加几(大于0)
    324. */
    325. public double hincr(String key, String item, double by) {
    326. return redisTemplate.opsForHash().increment(key, item, by);
    327. }
    328. /**
    329. * hash递减
    330. *
    331. * @param key 键
    332. * @param item 项
    333. * @param by 要减少记(小于0)
    334. */
    335. public double hdecr(String key, String item, double by) {
    336. return redisTemplate.opsForHash().increment(key, item, -by);
    337. }
    338. // ============================set=============================
    339. /**
    340. * 根据key获取Set中的所有值
    341. * @param key 键
    342. */
    343. public Set<Object> sGet(String key) {
    344. try {
    345. return redisTemplate.opsForSet().members(key);
    346. } catch (Exception e) {
    347. e.printStackTrace();
    348. return null;
    349. }
    350. }
    351. /**
    352. * 根据value从一个set中查询,是否存在
    353. *
    354. * @param key 键
    355. * @param value 值
    356. * @return true 存在 false不存在
    357. */
    358. public boolean sHasKey(String key, Object value) {
    359. try {
    360. return redisTemplate.opsForSet().isMember(key, value);
    361. } catch (Exception e) {
    362. e.printStackTrace();
    363. return false;
    364. }
    365. }
    366. /**
    367. * 将数据放入set缓存
    368. *
    369. * @param key 键
    370. * @param values 值 可以是多个
    371. * @return 成功个数
    372. */
    373. public long sSet(String key, Object... values) {
    374. try {
    375. return redisTemplate.opsForSet().add(key, values);
    376. } catch (Exception e) {
    377. e.printStackTrace();
    378. return 0;
    379. }
    380. }
    381. /**
    382. * 将set数据放入缓存
    383. *
    384. * @param key 键
    385. * @param time 时间(秒)
    386. * @param values 值 可以是多个
    387. * @return 成功个数
    388. */
    389. public long sSetAndTime(String key, long time, Object... values) {
    390. try {
    391. Long count = redisTemplate.opsForSet().add(key, values);
    392. if (time > 0)
    393. expire(key, time);
    394. return count;
    395. } catch (Exception e) {
    396. e.printStackTrace();
    397. return 0;
    398. }
    399. }
    400. /**
    401. * 获取set缓存的长度
    402. *
    403. * @param key 键
    404. */
    405. public long sGetSetSize(String key) {
    406. try {
    407. return redisTemplate.opsForSet().size(key);
    408. } catch (Exception e) {
    409. e.printStackTrace();
    410. return 0;
    411. }
    412. }
    413. /**
    414. * 移除值为value的
    415. *
    416. * @param key 键
    417. * @param values 值 可以是多个
    418. * @return 移除的个数
    419. */
    420. public long setRemove(String key, Object... values) {
    421. try {
    422. Long count = redisTemplate.opsForSet().remove(key, values);
    423. return count;
    424. } catch (Exception e) {
    425. e.printStackTrace();
    426. return 0;
    427. }
    428. }
    429. // ===============================list=================================
    430. /**
    431. * 获取list缓存的内容
    432. *
    433. * @param key 键
    434. * @param start 开始
    435. * @param end 结束 0 到 -1代表所有值
    436. */
    437. public List<Object> lGet(String key, long start, long end) {
    438. try {
    439. return redisTemplate.opsForList().range(key, start, end);
    440. } catch (Exception e) {
    441. e.printStackTrace();
    442. return null;
    443. }
    444. }
    445. /**
    446. * 获取list缓存的长度
    447. *
    448. * @param key 键
    449. */
    450. public long lGetListSize(String key) {
    451. try {
    452. return redisTemplate.opsForList().size(key);
    453. } catch (Exception e) {
    454. e.printStackTrace();
    455. return 0;
    456. }
    457. }
    458. /**
    459. * 通过索引 获取list中的值
    460. *
    461. * @param key 键
    462. * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
    463. */
    464. public Object lGetIndex(String key, long index) {
    465. try {
    466. return redisTemplate.opsForList().index(key, index);
    467. } catch (Exception e) {
    468. e.printStackTrace();
    469. return null;
    470. }
    471. }
    472. /**
    473. * 将list放入缓存
    474. *
    475. * @param key 键
    476. * @param value 值
    477. */
    478. public boolean lSet(String key, Object value) {
    479. try {
    480. redisTemplate.opsForList().rightPush(key, value);
    481. return true;
    482. } catch (Exception e) {
    483. e.printStackTrace();
    484. return false;
    485. }
    486. }
    487. /**
    488. * 将list放入缓存
    489. * @param key 键
    490. * @param value 值
    491. * @param time 时间(秒)
    492. */
    493. public boolean lSet(String key, Object value, long time) {
    494. try {
    495. redisTemplate.opsForList().rightPush(key, value);
    496. if (time > 0)
    497. expire(key, time);
    498. return true;
    499. } catch (Exception e) {
    500. e.printStackTrace();
    501. return false;
    502. }
    503. }
    504. /**
    505. * 将list放入缓存
    506. *
    507. * @param key 键
    508. * @param value 值
    509. * @return
    510. */
    511. public boolean lSet(String key, List<Object> value) {
    512. try {
    513. redisTemplate.opsForList().rightPushAll(key, value);
    514. return true;
    515. } catch (Exception e) {
    516. e.printStackTrace();
    517. return false;
    518. }
    519. }
    520. /**
    521. * 将list放入缓存
    522. *
    523. * @param key 键
    524. * @param value 值
    525. * @param time 时间(秒)
    526. * @return
    527. */
    528. public boolean lSet(String key, List<Object> value, long time) {
    529. try {
    530. redisTemplate.opsForList().rightPushAll(key, value);
    531. if (time > 0)
    532. expire(key, time);
    533. return true;
    534. } catch (Exception e) {
    535. e.printStackTrace();
    536. return false;
    537. }
    538. }
    539. /**
    540. * 根据索引修改list中的某条数据
    541. *
    542. * @param key 键
    543. * @param index 索引
    544. * @param value 值
    545. * @return
    546. */
    547. public boolean lUpdateIndex(String key, long index, Object value) {
    548. try {
    549. redisTemplate.opsForList().set(key, index, value);
    550. return true;
    551. } catch (Exception e) {
    552. e.printStackTrace();
    553. return false;
    554. }
    555. }
    556. /**
    557. * 移除N个值为value
    558. *
    559. * @param key 键
    560. * @param count 移除多少个
    561. * @param value 值
    562. * @return 移除的个数
    563. */
    564. public long lRemove(String key, long count, Object value) {
    565. try {
    566. Long remove = redisTemplate.opsForList().remove(key, count, value);
    567. return remove;
    568. } catch (Exception e) {
    569. e.printStackTrace();
    570. return 0;
    571. }
    572. }
    573. }