RedisTemplate 相关的api使用。
http://www.45fan.com/article.php?aid=1D2ojkZ7gc23Gzro

RedisTemplateConfiguration.java

经过亲身测试,其实上无须加上这些配置,如果你加上的话,你用的是jackson的序列化,到时候获取到的对象就是Map类型,如果你要是用的fastjson的序列化,你获取到的就是jsonObject。反正是你获取不到原来的对象,还得想办法重新转化一下,如果你啥都不处理的话,那么你只需要强制转化下就可以了。

对应的配置 去上面的文章找

  1. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  2. import com.fasterxml.jackson.annotation.PropertyAccessor;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.connection.RedisConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  9. import org.springframework.data.redis.serializer.StringRedisSerializer;
  10. @Configuration
  11. public class RedisTemplateConfiguration {
  12. /**
  13. * redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
  14. *
  15. * @param redisConnectionFactory
  16. * @return
  17. */
  18. @Bean
  19. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  20. RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
  21. redisTemplate.setConnectionFactory(redisConnectionFactory);
  22. // 使用Jackson2JsonRedisSerialize 替换默认序列化
  23. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  24. ObjectMapper objectMapper = new ObjectMapper();
  25. objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  26. jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  27. // 设置key和value的序列化规则
  28. redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
  29. redisTemplate.setKeySerializer(new StringRedisSerializer());
  30. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  31. redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  32. redisTemplate.afterPropertiesSet();
  33. return redisTemplate;
  34. }
  35. }

fastjson的序列化

https://blog.csdn.net/threelifeadv/article/details/122879037

给一个别人封装好的工具类,感觉挺全的,以后可以用这个工具类操作

工具类——RedisTemplateUtils

https://blog.csdn.net/threelifeadv/article/details/122879037

  1. package com.example.demo.mideng;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.redis.connection.DataType;
  4. import org.springframework.data.redis.core.Cursor;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.data.redis.core.ScanOptions;
  7. import org.springframework.data.redis.core.ZSetOperations;
  8. import org.springframework.stereotype.Component;
  9. import java.util.Collection;
  10. import java.util.Date;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Set;
  14. import java.util.concurrent.TimeUnit;
  15. /**
  16. * @author: 公众号:干货食堂
  17. * Project Name: demo
  18. * File Name: RedisTemplateUtils
  19. * Package Name: com.example.demo.mideng
  20. * Date: 2022/7/2 17:29
  21. * Copyright (c) 2022,All Rights Reserved.
  22. * @Description
  23. */
  24. @Component
  25. public class RedisTemplateUtils {
  26. @Autowired
  27. private RedisTemplate redisTemplate;
  28. //========================RedisTemplate操作string类型数据============================================
  29. /**
  30. * @param key
  31. * @param value
  32. * @return void
  33. * @description string设置 key和 value的值
  34. * @author chengjunyu
  35. * @date 2022/2/11
  36. */
  37. public void set(String key, Object value) {
  38. redisTemplate.opsForValue().set(key, value);
  39. }
  40. /**
  41. * @param key
  42. * @param value
  43. * @param seconds
  44. * @param timeUnit
  45. * @return void
  46. * @description string设置 key和 value的值并设置过期时间和时间单位
  47. * @author chengjunyu
  48. * @date 2022/2/12
  49. */
  50. public void setWithExpire(String key, Object value, Long seconds, TimeUnit timeUnit) {
  51. redisTemplate.opsForValue().set(key, value, seconds, timeUnit);
  52. }
  53. /**
  54. * @param * @param key
  55. * @return java.lang.Object
  56. * @description string获取 key对应的 value值
  57. * @author chengjunyu
  58. * @date 2022/2/11
  59. */
  60. public Object get(String key) {
  61. return redisTemplate.opsForValue().get(key);
  62. }
  63. /**
  64. * @param * @param key
  65. * @return boolean
  66. * @description 判断在 redis中是不是存在对应的 key值,有的话就返回 true,没有就返回 false
  67. * @author chengjunyu
  68. * @date 2022/2/11
  69. */
  70. public Boolean hasKey(String key) {
  71. return redisTemplate.hasKey(key);
  72. }
  73. /**
  74. * @param key
  75. * @return boolean
  76. * @description 删除redis中对应的key值
  77. * @author chengjunyu
  78. * @date 2022/2/11
  79. */
  80. public Boolean del(String key) {
  81. return redisTemplate.delete(key);
  82. }
  83. /**
  84. * @param keys
  85. * @return long
  86. * @description 批量删除 redis中对应的 key值,其中 keys是数组 keys:Collection<K> keys
  87. * @author chengjunyu
  88. * @date 2022/2/11
  89. */
  90. public Long batchDel(Collection<String> keys) {
  91. return redisTemplate.delete(keys);
  92. }
  93. /**
  94. * @param key
  95. * @return byte[]
  96. * @description 把 key值序列化成 byte[]类型
  97. * @author chengjunyu
  98. * @date 2022/2/11
  99. */
  100. public byte[] dump(String key) {
  101. return redisTemplate.dump(key);
  102. }
  103. /**
  104. * @param * @param key
  105. * @param seconds
  106. * @return void
  107. * @description 对传入的 key值设置过期时间
  108. * @author chengjunyu
  109. * @date 2022/2/11
  110. */
  111. public Boolean expire(String key, long seconds) {
  112. return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
  113. }
  114. /**
  115. * @param * @param key
  116. * @param date
  117. * @return boolean
  118. * @description 对传入的 key值设置过期日期
  119. * @author chengjunyu
  120. * @date 2022/2/11
  121. */
  122. public Boolean expireAt(String key, Date date) {
  123. return redisTemplate.expireAt(key, date);
  124. }
  125. /**
  126. * @param * @param key
  127. * @return java.util.Set<java.lang.String>
  128. * @description 模糊查询,返回一个没有重复的 Set类型
  129. * @author chengjunyu
  130. * @date 2022/2/11
  131. */
  132. public Set<String> getStringKeys(String key) {
  133. return redisTemplate.keys(key);
  134. }
  135. /**
  136. * @param oldKey
  137. * @param newKey
  138. * @return void
  139. * @description 根据新的 key的名称修改 redis中老的 key的名称
  140. * @author chengjunyu
  141. * @date 2022/2/12
  142. */
  143. public void rename(String oldKey, String newKey) {
  144. redisTemplate.rename(oldKey, newKey);
  145. }
  146. /**
  147. * @param oldKey
  148. * @param newKey
  149. * @return boolean
  150. * @description 重命名旧的 key值
  151. * @author chengjunyu
  152. * @date 2022/2/12
  153. */
  154. public Boolean renameIfAbsent(String oldKey, String newKey) {
  155. return redisTemplate.renameIfAbsent(oldKey, newKey);
  156. }
  157. /**
  158. * @param key
  159. * @return org.springframework.data.redis.connection.DataType
  160. * @description 获取 key值的类型
  161. * @author chengjunyu
  162. * @date 2022/2/12
  163. */
  164. public DataType type(String key) {
  165. return redisTemplate.type(key);
  166. }
  167. /**
  168. * @param
  169. * @return java.lang.String
  170. * @description 随机从 redis中获取一个 key
  171. * @author chengjunyu
  172. * @date 2022/2/12
  173. */
  174. public Object randomKey() {
  175. return redisTemplate.randomKey();
  176. }
  177. /**
  178. * @param key
  179. * @return void
  180. * @description 获取当前 key的剩下的过期时间
  181. * @author chengjunyu
  182. * @date 2022/2/12
  183. */
  184. public Long getExpire(String key) {
  185. return redisTemplate.getExpire(key);
  186. }
  187. /**
  188. * @param key
  189. * @param timeUnit
  190. * @return java.lang.Long
  191. * @description 获取 key剩余的过期时间,同时设置时间单位
  192. * @author chengjunyu
  193. * @date 2022/2/12
  194. */
  195. public Long getExpire(String key, TimeUnit timeUnit) {
  196. return redisTemplate.getExpire(key, timeUnit);
  197. }
  198. /**
  199. * @param key
  200. * @return java.lang.Boolean
  201. * @description 将 key进行持久化
  202. * @author chengjunyu
  203. * @date 2022/2/12
  204. */
  205. public Boolean persist(String key) {
  206. return redisTemplate.persist(key);
  207. }
  208. /**
  209. * @param key
  210. * @param dbIndex
  211. * @return java.lang.Boolean
  212. * @description 将当前数据库的 key移动到指定 redis中数据库当中
  213. * @author chengjunyu
  214. * @date 2022/2/12
  215. */
  216. public Boolean move(String key, int dbIndex) {
  217. return redisTemplate.move(key, dbIndex);
  218. }
  219. /**
  220. * @param key
  221. * @param start
  222. * @param end
  223. * @return java.lang.String
  224. * @description 截取 key的子字符串
  225. * @author chengjunyu
  226. * @date 2022/2/12
  227. */
  228. public String subString(String key, long start, long end) {
  229. return redisTemplate.opsForValue().get(key, start, end);
  230. }
  231. /**
  232. * @param key
  233. * @param value
  234. * @return java.lang.Object
  235. * @description 设置 key跟 value的值,同时获取 key的值
  236. * @author chengjunyu
  237. * @date 2022/2/12
  238. */
  239. public Object getAndSet(String key, Object value) {
  240. return redisTemplate.opsForValue().getAndSet(key, value);
  241. }
  242. /**
  243. * @param keys
  244. * @return java.util.List<java.lang.Object>
  245. * @description 设置多个 key跟 value的值,同时获取 key的值
  246. * @author chengjunyu
  247. * @date 2022/2/12
  248. */
  249. public List<Object> multiGetSet(List<String> keys) {
  250. return redisTemplate.opsForValue().multiGet(keys);
  251. }
  252. /**
  253. * @param * @param key
  254. * @param value
  255. * @return java.lang.Integer
  256. * @description 获取原来的 key的值后在后面新增上新的字符串
  257. * @author chengjunyu
  258. * @date 2022/2/12
  259. */
  260. public Integer append(String key, String value) {
  261. return redisTemplate.opsForValue().append(key, value);
  262. }
  263. /**
  264. * @param * @param key
  265. * @return java.lang.Long
  266. * @description value 值 +1
  267. * @author chengjunyu
  268. * @date 2022/2/12
  269. */
  270. public Long increment(String key) {
  271. return redisTemplate.opsForValue().increment(key);
  272. }
  273. /**
  274. * @param key
  275. * @param increment
  276. * @return java.lang.Long
  277. * @description 增量方式增加或减少 long值
  278. * @author chengjunyu
  279. * @date 2022/2/12
  280. */
  281. public Long incrementLong(String key, long increment) {
  282. return redisTemplate.opsForValue().increment(key, increment);
  283. }
  284. /**
  285. * @param key
  286. * @param increment
  287. * @return void
  288. * @description 增量方式增加double值
  289. * @author chengjunyu
  290. * @date 2022/2/12
  291. */
  292. public void incrementDouble(String key, double increment) {
  293. redisTemplate.opsForValue().increment(key, increment);
  294. }
  295. /**
  296. * @param * @param map
  297. * @return java.lang.Boolean
  298. * @description 不存在即新增map的操作
  299. * @author chengjunyu
  300. * @date 2022/2/12
  301. */
  302. public Boolean multiSetIfAbsent(Map<? extends String, ?> map) {
  303. return redisTemplate.opsForValue().multiSetIfAbsent(map);
  304. }
  305. /**
  306. * @param * @param map
  307. * @return void
  308. * @description 保存 map集合
  309. * @author chengjunyu
  310. * @date 2022/2/12
  311. */
  312. public void multiSet(Map<String, String> map) {
  313. redisTemplate.opsForValue().multiSet(map);
  314. }
  315. /**
  316. * @param * @param keys
  317. * @return java.util.List<java.lang.Object>
  318. * @description 获取 map集合
  319. * @author chengjunyu
  320. * @date 2022/2/12
  321. */
  322. public List<Object> multiGet(List<String> keys) {
  323. return redisTemplate.opsForValue().multiGet(keys);
  324. }
  325. /**
  326. * @param key
  327. * @return java.lang.Long
  328. * @description 获取指定 key的字符串的长度
  329. * @author chengjunyu
  330. * @date 2022/2/12
  331. */
  332. public Long sizeString(String key) {
  333. return redisTemplate.opsForValue().size(key);
  334. }
  335. /**
  336. * @param key
  337. * @param value
  338. * @param offset
  339. * @return void
  340. * @description 根据偏移量 offset 的值,覆盖重写 value的值
  341. * @author chengjunyu
  342. * @date 2022/2/12
  343. */
  344. public void offsetValue(String key, Object value, Long offset) {
  345. redisTemplate.opsForValue().set(key, value, offset);
  346. }
  347. /**
  348. * @param key
  349. * @param offset
  350. * @return java.lang.Boolean
  351. * @description 对 key所储存的字符串值,获取指定偏移量上的位(bit)
  352. * @author chengjunyu
  353. * @date 2022/2/12
  354. */
  355. public Boolean getOffsetValue(String key, Long offset) {
  356. return redisTemplate.opsForValue().getBit(key, offset);
  357. }
  358. /**
  359. * @param key
  360. * @param value
  361. * @return java.lang.Boolean
  362. * @description 重新设置 key对应的值,如果存在返回 false,否则返回 true
  363. * @author chengjunyu
  364. * @date 2022/2/12
  365. */
  366. public Boolean setIfAbsent(String key, Object value) {
  367. return redisTemplate.opsForValue().setIfAbsent(key, value);
  368. }
  369. //========================RedisTemplate操作hash类型数据============================================
  370. /**
  371. * @param key
  372. * @param field
  373. * @param value
  374. * @return void
  375. * @description 新增map值
  376. * @author chengjunyu
  377. * @date 2022/2/12
  378. */
  379. public void put(String key, Object field, Object value) {
  380. redisTemplate.opsForHash().put(key, field, value);
  381. }
  382. /**
  383. * @param key
  384. * @param map
  385. * @return void
  386. * @description 以map集合的形式添加键值对
  387. * @author chengjunyu
  388. * @date 2022/2/12
  389. */
  390. public void putAll(String key, Map<Object, Object> map) {
  391. redisTemplate.opsForHash().putAll(key, map);
  392. }
  393. /**
  394. * @param key
  395. * @param field
  396. * @return java.lang.Object
  397. * @description 获取 map中指定的 key值,如果存在则返回值,没有就返回null
  398. * @author chengjunyu
  399. * @date 2022/2/12
  400. */
  401. public Object getMapValue(String key, String field) {
  402. return redisTemplate.opsForHash().get(key, field);
  403. }
  404. /**
  405. * @param * @param key
  406. * @return java.util.Map<java.lang.Object, java.lang.Object>
  407. * @description 根据 key获取 Map对象
  408. * @author chengjunyu
  409. * @date 2022/2/12
  410. */
  411. public Map<Object, Object> getMap(String key) {
  412. return redisTemplate.opsForHash().entries(key);
  413. }
  414. /**
  415. * @param key
  416. * @param hashKey
  417. * @param value
  418. * @return java.lang.Boolean
  419. * @description 当 hashKey不存在的时候,进行设置 map的值
  420. * @author chengjunyu
  421. * @date 2022/2/12
  422. */
  423. public Boolean putIfAbsent(String key, Object hashKey, Object value) {
  424. return redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
  425. }
  426. /**
  427. * @param key
  428. * @param fields
  429. * @return java.lang.Long
  430. * @description 删除多个map的字段
  431. * @author chengjunyu
  432. * @date 2022/2/12
  433. */
  434. public Long del(String key, List<Object> fields) {
  435. return redisTemplate.opsForHash().delete(key, fields);
  436. }
  437. /**
  438. * @param * @param key
  439. * @param field
  440. * @return java.lang.Boolean
  441. * @description 查看 hash表中指定字段是否存在
  442. * @author chengjunyu
  443. * @date 2022/2/12
  444. */
  445. public Boolean hasKey(String key, Object field) {
  446. return redisTemplate.opsForHash().hasKey(key, field);
  447. }
  448. /**
  449. * @param key
  450. * @param field
  451. * @param increment
  452. * @return java.lang.Long
  453. * @description 给 map中指定字段的整数值加上 long型增量 increment
  454. * @author chengjunyu
  455. * @date 2022/2/12
  456. */
  457. public Long incrementLong(String key, Object field, long increment) {
  458. return redisTemplate.opsForHash().increment(key, field, increment);
  459. }
  460. /**
  461. * @param key
  462. * @param field
  463. * @param increment
  464. * @return java.lang.Double
  465. * @description 给 map中指定字段的整数值加上 double型增量 increment
  466. * @author chengjunyu
  467. * @date 2022/2/12
  468. */
  469. public Double incrementDouble(String key, Object field, double increment) {
  470. return redisTemplate.opsForHash().increment(key, field, increment);
  471. }
  472. /**
  473. * @param key
  474. * @return java.util.Set<java.lang.Object>
  475. * @description 获取 map中的所有字段
  476. * @author chengjunyu
  477. * @date 2022/2/12
  478. */
  479. public Set<Object> keys(String key) {
  480. return redisTemplate.opsForHash().keys(key);
  481. }
  482. /**
  483. * @param key
  484. * @return java.util.Set<java.lang.Object>
  485. * @description 获取 map中所有字段的数量
  486. * @author chengjunyu
  487. * @date 2022/2/12
  488. */
  489. public Long sizeHash(String key) {
  490. return redisTemplate.opsForHash().size(key);
  491. }
  492. /**
  493. * @param key
  494. * @return java.util.List<java.lang.Object>
  495. * @description 获取hash表中存在的所有的值
  496. * @author chengjunyu
  497. * @date 2022/2/12
  498. */
  499. public List<Object> values(String key) {
  500. return redisTemplate.opsForHash().values(key);
  501. }
  502. /**
  503. * @param key
  504. * @param scanOptions
  505. * @return org.springframework.data.redis.core.Cursor<java.util.Map.Entry < java.lang.Object, java.lang.Object>>
  506. * @description 查看匹配的键值对
  507. * @author chengjunyu
  508. * @date 2022/2/12
  509. */
  510. public Cursor<Map.Entry<Object, Object>> scan(String key, ScanOptions scanOptions) {
  511. return redisTemplate.opsForHash().scan(key, scanOptions);
  512. }
  513. //========================RedisTemplate操作list类型数据============================================
  514. /**
  515. * @param key
  516. * @param value
  517. * @return java.lang.Long
  518. * @description 把值添加在 list的最前面
  519. * @author chengjunyu
  520. * @date 2022/2/12
  521. */
  522. public Long leftPush(String key, Object value) {
  523. return redisTemplate.opsForList().leftPush(key, value);
  524. }
  525. /**
  526. * @param key
  527. * @param values
  528. * @return java.lang.Long
  529. * @description 把值添加在 list
  530. * @author chengjunyu
  531. * @date 2022/2/12
  532. */
  533. public Long leftPush(String key, Object... values) {
  534. return redisTemplate.opsForList().leftPushAll(key, values);
  535. }
  536. /**
  537. * @param key
  538. * @param value
  539. * @return java.lang.Long
  540. * @description 直接把一个新的 list添加到老的 list上面去
  541. * @author chengjunyu
  542. * @date 2022/2/12
  543. */
  544. public Long leftPushAll(String key, List<Object> value) {
  545. return redisTemplate.opsForList().leftPushAll(key, value);
  546. }
  547. /**
  548. * @param key
  549. * @param value
  550. * @return long
  551. * @description List存在的时候就加入新的值
  552. * @author chengjunyu
  553. * @date 2022/2/12
  554. */
  555. public long leftPushIfPresent(String key, Object value) {
  556. return redisTemplate.opsForList().leftPushIfPresent(key, value);
  557. }
  558. /**
  559. * @param key
  560. * @param value
  561. * @return java.lang.Long
  562. * @description 把值添加在 list的最后面
  563. * @author chengjunyu
  564. * @date 2022/2/12
  565. */
  566. public long rightPush(String key, Object value) {
  567. return redisTemplate.opsForList().rightPush(key, value);
  568. }
  569. /**
  570. * @param key
  571. * @param values
  572. * @return long
  573. * @description 把值添加在 list的最后面
  574. * @author chengjunyu
  575. * @date 2022/2/12
  576. */
  577. public long rightPushAll(String key, Object... values) {
  578. return redisTemplate.opsForList().rightPushAll(key, values);
  579. }
  580. /**
  581. * @param key
  582. * @param values
  583. * @return long
  584. * @description 把值添加在 list的最后面
  585. * @author chengjunyu
  586. * @date 2022/2/12
  587. */
  588. public long rightPushAll(String key, List<Object> values) {
  589. return redisTemplate.opsForList().rightPushAll(key, values);
  590. }
  591. /**
  592. * @param key
  593. * @param index
  594. * @return java.lang.Object
  595. * @description 根据索引获取 list中的值
  596. * @author chengjunyu
  597. * @date 2022/2/12
  598. */
  599. public Object index(String key, long index) {
  600. return redisTemplate.opsForList().index(key, index);
  601. }
  602. /**
  603. * @param key
  604. * @param start
  605. * @param end
  606. * @return java.util.List<java.lang.Object>
  607. * @description 获取 list中开始索引到结束索引的所有值
  608. * @author chengjunyu
  609. * @date 2022/2/12
  610. */
  611. public List<Object> range(String key, long start, long end) {
  612. return redisTemplate.opsForList().range(key, start, end);
  613. }
  614. /**
  615. * @param key
  616. * @return java.lang.Object
  617. * @description 移除并获取列表中第一个元素
  618. * @author chengjunyu
  619. * @date 2022/2/12
  620. */
  621. public Object leftPop(String key) {
  622. return redisTemplate.opsForList().leftPop(key);
  623. }
  624. /**
  625. * @param key
  626. * @param seconds
  627. * @param timeUnit
  628. * @return java.lang.Object
  629. * @description 移除并获取列表中第一个元素
  630. * @author chengjunyu
  631. * @date 2022/2/12
  632. */
  633. public Object leftPop(String key, Long seconds, TimeUnit timeUnit) {
  634. return redisTemplate.opsForList().leftPop(key, seconds, timeUnit);
  635. }
  636. /**
  637. * @param key
  638. * @return java.lang.Object
  639. * @description 移除并获取列表中最后一个元素
  640. * @author chengjunyu
  641. * @date 2022/2/12
  642. */
  643. public Object rightPop(String key) {
  644. return redisTemplate.opsForList().rightPop(key);
  645. }
  646. /**
  647. * @param key
  648. * @param seconds
  649. * @param timeUnit
  650. * @return java.lang.Object
  651. * @description 移除并获取列表中最后一个元素
  652. * @author chengjunyu
  653. * @date 2022/2/12
  654. */
  655. public Object rightPop(String key, Long seconds, TimeUnit timeUnit) {
  656. return redisTemplate.opsForList().rightPop(key, seconds, timeUnit);
  657. }
  658. /**
  659. * @param sourceKey
  660. * @param destinationKey
  661. * @return java.lang.Object
  662. * @description 从一个队列的右边弹出一个元素并将这个元素放入另一个指定队列的最左边
  663. * @author chengjunyu
  664. * @date 2022/2/12
  665. */
  666. public Object rightPopAndLeftPush(String sourceKey, String destinationKey) {
  667. return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey);
  668. }
  669. /**
  670. * @param sourceKey
  671. * @param destinationKey
  672. * @param seconds
  673. * @param timeUnit
  674. * @return java.lang.Object
  675. * @description 从一个队列的右边弹出一个元素并将这个元素放入另一个指定队列的最左边
  676. * @author chengjunyu
  677. * @date 2022/2/12
  678. */
  679. public Object rightPopAndLeftPush(String sourceKey, String destinationKey, long seconds, TimeUnit timeUnit) {
  680. return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, seconds, timeUnit);
  681. }
  682. /**
  683. * @param key
  684. * @return java.lang.Long
  685. * @description 获取 list的大小
  686. * @author chengjunyu
  687. * @date 2022/2/12
  688. */
  689. public Long sizeList(String key) {
  690. return redisTemplate.opsForList().size(key);
  691. }
  692. /**
  693. * @param key
  694. * @param start
  695. * @param end
  696. * @return void
  697. * @description 剪裁 List列表
  698. * @author chengjunyu
  699. * @date 2022/2/12
  700. */
  701. public void trim(String key, long start, long end) {
  702. redisTemplate.opsForList().trim(key, start, end);
  703. }
  704. /**
  705. * @param key
  706. * @param index
  707. * @param value
  708. * @return java.lang.Long
  709. * @description 删除集合中值等于value的元素
  710. * @author chengjunyu
  711. * @date 2022/2/12
  712. */
  713. public Long remove(String key, long index, Object value) {
  714. return redisTemplate.opsForList().remove(key, index, value);
  715. }
  716. //========================RedisTemplate操作set类型数据============================================
  717. /**
  718. * @param key
  719. * @param values
  720. * @return java.lang.Long
  721. * @description 添加元素到 set中
  722. * @author chengjunyu
  723. * @date 2022/2/12
  724. */
  725. public Long add(String key, Collection<Object> values) {
  726. return redisTemplate.opsForSet().add(key, values);
  727. }
  728. /**
  729. * @param key
  730. * @return java.lang.Object
  731. * @description 从 set中删除一个随机元素,并返回该元素
  732. * @author chengjunyu
  733. * @date 2022/2/12
  734. */
  735. public Object pop(String key) {
  736. return redisTemplate.opsForSet().pop(key);
  737. }
  738. /**
  739. * @param key
  740. * @return long
  741. * @description 获取 set集合的大小
  742. * @author chengjunyu
  743. * @date 2022/2/12
  744. */
  745. public Long sizeSet(String key) {
  746. return redisTemplate.opsForSet().size(key);
  747. }
  748. /**
  749. * @param key
  750. * @param value
  751. * @return java.lang.Boolean
  752. * @description 判断 set集合中是否存在value值
  753. * @author chengjunyu
  754. * @date 2022/2/12
  755. */
  756. public Boolean isMember(String key, Object value) {
  757. return redisTemplate.opsForSet().isMember(key, value);
  758. }
  759. /**
  760. * @param key
  761. * @param otherKey
  762. * @return java.util.Set<java.lang.Object>
  763. * @description 获取两个集合的交集并返回一个集合
  764. * @author chengjunyu
  765. * @date 2022/2/12
  766. */
  767. public Set<Object> intersect(String key, String otherKey) {
  768. return redisTemplate.opsForSet().intersect(key, otherKey);
  769. }
  770. /**
  771. * @param key
  772. * @param collection
  773. * @return java.util.Set<java.lang.Object>
  774. * @description 获取两个集合的交集并返回一个集合
  775. * @author chengjunyu
  776. * @date 2022/2/12
  777. */
  778. public Set<Object> intersect(String key, Collection<String> collection) {
  779. return redisTemplate.opsForSet().intersect(key, collection);
  780. }
  781. /**
  782. * @param key1
  783. * @param key2
  784. * @param destKey
  785. * @return java.lang.Long
  786. * @description 获取两个集合交集,并存储到 destKey
  787. * @author chengjunyu
  788. * @date 2022/2/12
  789. */
  790. public Long intersectAndStore(String key1, String key2, String destKey) {
  791. return redisTemplate.opsForSet().intersectAndStore(key1, key2, destKey);
  792. }
  793. /**
  794. * @param key
  795. * @param key1
  796. * @return java.util.Set<java.lang.Object>
  797. * @description 获取两个集合的并集
  798. * @author chengjunyu
  799. * @date 2022/2/12
  800. */
  801. public Set<Object> union(String key, String key1) {
  802. return redisTemplate.opsForSet().union(key, key1);
  803. }
  804. /**
  805. * @param key
  806. * @param collection
  807. * @return java.util.Set<java.lang.Object>
  808. * @description 获取两个集合的并集
  809. * @author chengjunyu
  810. * @date 2022/2/12
  811. */
  812. public Set<Object> union(String key, Collection<String> collection) {
  813. return redisTemplate.opsForSet().union(key, collection);
  814. }
  815. /**
  816. * @param key
  817. * @param key1
  818. * @param destKey
  819. * @return java.lang.Long
  820. * @description 获取两个集合的并集,并存储到 destKey
  821. * @author chengjunyu
  822. * @date 2022/2/12
  823. */
  824. public Long unionAndStore(String key, String key1, String destKey) {
  825. return redisTemplate.opsForSet().unionAndStore(key, key1, destKey);
  826. }
  827. /**
  828. * @param key
  829. * @param key1
  830. * @return java.util.Set<java.lang.Object>
  831. * @description 获取两个集合的差集
  832. * @author chengjunyu
  833. * @date 2022/2/12
  834. */
  835. public Set<Object> difference(String key, String key1) {
  836. return redisTemplate.opsForSet().difference(key, key1);
  837. }
  838. /**
  839. * @param key
  840. * @param collection
  841. * @return java.util.Set<java.lang.Object>
  842. * @description 获取两个集合的差集
  843. * @author chengjunyu
  844. * @date 2022/2/12
  845. */
  846. public Set<Object> difference(String key, Collection<String> collection) {
  847. return redisTemplate.opsForSet().difference(key, collection);
  848. }
  849. /**
  850. * @param key
  851. * @param key1
  852. * @param destKey
  853. * @return java.lang.Long
  854. * @description 获取两个集合的差集,并存储到 destKey
  855. * @author chengjunyu
  856. * @date 2022/2/12
  857. */
  858. public Long differenceAndStore(String key, String key1, String destKey) {
  859. return redisTemplate.opsForSet().differenceAndStore(key, key1, destKey);
  860. }
  861. /**
  862. * @param key
  863. * @return java.util.Set<java.lang.Object>
  864. * @description 获取集合中的所有元素
  865. * @author chengjunyu
  866. * @date 2022/2/12
  867. */
  868. public Set<Object> members(String key) {
  869. return redisTemplate.opsForSet().members(key);
  870. }
  871. /**
  872. * @param key
  873. * @return java.lang.Object
  874. * @description 随机获取集合中一个元素
  875. * @author chengjunyu
  876. * @date 2022/2/12
  877. */
  878. public Object randomMember(String key) {
  879. return redisTemplate.opsForSet().randomMember(key);
  880. }
  881. /**
  882. * @param key
  883. * @param count
  884. * @return java.util.List<java.lang.Object>
  885. * @description 随机获取集合中 count个元素,返回一个 List集合
  886. * @author chengjunyu
  887. * @date 2022/2/12
  888. */
  889. public List<Object> randomMembers(String key, long count) {
  890. return redisTemplate.opsForSet().randomMembers(key, count);
  891. }
  892. /**
  893. * @param key
  894. * @param count
  895. * @return java.util.Set<java.lang.Object>
  896. * @description 随机获取集合中 count个元素,去重后返回一个 Set集合
  897. * @author chengjunyu
  898. * @date 2022/2/12
  899. */
  900. public Set<Object> distinctRandomMembers(String key, long count) {
  901. return redisTemplate.opsForSet().distinctRandomMembers(key, count);
  902. }
  903. /**
  904. * @param key
  905. * @param scanOptions
  906. * @return org.springframework.data.redis.core.Cursor<java.lang.Object>
  907. * @description 遍历 set
  908. * @author chengjunyu
  909. * @date 2022/2/12
  910. */
  911. public Cursor<Object> scanSet(String key, ScanOptions scanOptions) {
  912. return redisTemplate.opsForSet().scan(key, scanOptions);
  913. }
  914. /**
  915. * @param key
  916. * @param objects
  917. * @return java.lang.Long
  918. * @description 移除元素
  919. * @author chengjunyu
  920. * @date 2022/2/12
  921. */
  922. public Long remove(String key, Collection<Object> objects) {
  923. return redisTemplate.opsForSet().remove(key, objects);
  924. }
  925. //========================RedisTemplate操作zset类型数据============================================
  926. /**
  927. * @param key
  928. * @param value
  929. * @param score
  930. * @return java.lang.Boolean
  931. * @description 添加元素到 zset,从小到大排序
  932. * @author chengjunyu
  933. * @date 2022/2/12
  934. */
  935. public Boolean add(String key, Object value, double score) {
  936. return redisTemplate.opsForZSet().add(key, value, score);
  937. }
  938. /**
  939. * @param key
  940. * @param value
  941. * @param score
  942. * @return java.lang.Double
  943. * @description 增加元素的 score值同时返回增加后的 score值
  944. * @author chengjunyu
  945. * @date 2022/2/12
  946. */
  947. public Double incrementScore(String key, Object value, double score) {
  948. return redisTemplate.opsForZSet().incrementScore(key, value, score);
  949. }
  950. /**
  951. * @param key
  952. * @param object
  953. * @return java.lang.Long
  954. * @description 返回 zset元素在集合的从小到大排名
  955. * @author chengjunyu
  956. * @date 2022/2/12
  957. */
  958. public Long rank(String key, Object object) {
  959. return redisTemplate.opsForZSet().rank(key, object);
  960. }
  961. /**
  962. * @param key
  963. * @param object
  964. * @return java.lang.Long
  965. * @description 返回 zset元素在集合的由大到小排名
  966. * @author chengjunyu
  967. * @date 2022/2/12
  968. */
  969. public Long reverseRank(String key, Object object) {
  970. return redisTemplate.opsForZSet().reverseRank(key, object);
  971. }
  972. /**
  973. * @param key
  974. * @param start
  975. * @param end
  976. * @return java.util.Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple < java.lang.Object>>
  977. * @description 获取 zset集合中指定区间的元素
  978. * @author chengjunyu
  979. * @date 2022/2/12
  980. */
  981. public Set<ZSetOperations.TypedTuple<Object>> reverseRangeWithScores(String key, long start, long end) {
  982. return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
  983. }
  984. /**
  985. * @param key
  986. * @param min
  987. * @param max
  988. * @return java.util.Set<java.lang.Object>
  989. * @description 查询 zset集合中的元素并从小到大排序
  990. * @author chengjunyu
  991. * @date 2022/2/12
  992. */
  993. public Set<Object> reverseRangeByScore(String key, double min, double max) {
  994. return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
  995. }
  996. /**
  997. * @param key
  998. * @param min
  999. * @param max
  1000. * @param start
  1001. * @param end
  1002. * @return java.util.Set<java.lang.Object>
  1003. * @description 从高到低进行排序,然后获取最小与最大值之间的值
  1004. * @author chengjunyu
  1005. * @date 2022/2/12
  1006. */
  1007. public Set<Object> reverseRangeByScore(String key, double min, double max, long start, long end) {
  1008. return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max, start, end);
  1009. }
  1010. /**
  1011. * @param key
  1012. * @param min
  1013. * @param max
  1014. * @return java.util.Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple < java.lang.Object>>
  1015. * @description 查询 zset集合中的元素并从小到大排序
  1016. * @author chengjunyu
  1017. * @date 2022/2/12
  1018. */
  1019. public Set<ZSetOperations.TypedTuple<Object>> reverseRangeByScoreWithScores(String key, double min, double max) {
  1020. return redisTemplate.opsForZSet().reverseRangeByScoreWithScores(key, min, max);
  1021. }
  1022. /**
  1023. * @param key
  1024. * @param min
  1025. * @param max
  1026. * @return java.lang.Long
  1027. * @description 根据score值获取元素数量
  1028. * @author chengjunyu
  1029. * @date 2022/2/12
  1030. */
  1031. public Long count(String key, double min, double max) {
  1032. return redisTemplate.opsForZSet().count(key, min, max);
  1033. }
  1034. /**
  1035. * @param key
  1036. * @return java.lang.Long
  1037. * @description 获取 zset集合的大小
  1038. * @author chengjunyu
  1039. * @date 2022/2/12
  1040. */
  1041. public Long sizeZset(String key) {
  1042. return redisTemplate.opsForZSet().size(key);
  1043. }
  1044. /**
  1045. * @param key
  1046. * @return java.lang.Long
  1047. * @description 获取 zset集合的大小
  1048. * @author chengjunyu
  1049. * @date 2022/2/12
  1050. */
  1051. public Long zCard(String key) {
  1052. return redisTemplate.opsForZSet().zCard(key);
  1053. }
  1054. /**
  1055. * @param key
  1056. * @param value
  1057. * @return java.lang.Double
  1058. * @description 获取集合中 key、value元素的 score值
  1059. * @author chengjunyu
  1060. * @date 2022/2/12
  1061. */
  1062. public Double score(String key, Object value) {
  1063. return redisTemplate.opsForZSet().score(key, value);
  1064. }
  1065. /**
  1066. * @param key
  1067. * @param start
  1068. * @param end
  1069. * @return java.lang.Long
  1070. * @description 移除 zset中指定索引元素
  1071. * @author chengjunyu
  1072. * @date 2022/2/12
  1073. */
  1074. public Long removeRange(String key, long start, long end) {
  1075. return redisTemplate.opsForZSet().removeRange(key, start, end);
  1076. }
  1077. /**
  1078. * @param key
  1079. * @param min
  1080. * @param max
  1081. * @return java.lang.Long
  1082. * @description 移除 zset中指定 score范围的集合成员
  1083. * @author chengjunyu
  1084. * @date 2022/2/12
  1085. */
  1086. public Long removeRangeByScore(String key, double min, double max) {
  1087. return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
  1088. }
  1089. /**
  1090. * @param key
  1091. * @param key1
  1092. * @param destKey
  1093. * @return java.lang.Long
  1094. * @description 获取 key和 key1的并集并存储在 destKey中
  1095. * @author chengjunyu
  1096. * @date 2022/2/12
  1097. */
  1098. public Long zSetUnionAndStore(String key, String key1, String destKey) {
  1099. return redisTemplate.opsForZSet().unionAndStore(key, key1, destKey);
  1100. }
  1101. /**
  1102. * @param key
  1103. * @param collection
  1104. * @param destKey
  1105. * @return java.lang.Long
  1106. * @description 获取 key和 collection集合的并集并存储在 destKey中
  1107. * @author chengjunyu
  1108. * @date 2022/2/12
  1109. */
  1110. public Long zSetUnionAndStore(String key, Collection<String> collection, String destKey) {
  1111. return redisTemplate.opsForZSet().unionAndStore(key, collection, destKey);
  1112. }
  1113. /**
  1114. * @param key
  1115. * @param key1
  1116. * @param destKey
  1117. * @return java.lang.Long
  1118. * @description 获取 key和 key1的交集并存储在 destKey中
  1119. * @author chengjunyu
  1120. * @date 2022/2/12
  1121. */
  1122. public Long zSetIntersectAndStore(String key, String key1, String destKey) {
  1123. return redisTemplate.opsForZSet().intersectAndStore(key, key1, destKey);
  1124. }
  1125. /**
  1126. * @param key
  1127. * @param collection
  1128. * @param destKey
  1129. * @return java.lang.Long
  1130. * @description 获取 key和 collection集合的交集并存储在 destKey中
  1131. * @author chengjunyu
  1132. * @date 2022/2/12
  1133. */
  1134. public Long zSetIntersectAndStore(String key, Collection<String> collection, String destKey) {
  1135. return redisTemplate.opsForZSet().intersectAndStore(key, collection, destKey);
  1136. }
  1137. /**
  1138. * @param key
  1139. * @param values
  1140. * @return java.lang.Long
  1141. * @description 删除多个values的值
  1142. * @author chengjunyu
  1143. * @date 2022/2/12
  1144. */
  1145. public Long remove(String key, Object... values) {
  1146. return redisTemplate.opsForZSet().remove(key, values);
  1147. }
  1148. }

使用

  1. @Autowired
  2. private RedisTemplateUtils redisTemplateUtils;
  3. Dog dog = new Dog();
  4. dog.setName("zz");
  5. dog.setColor("cc2");
  6. redisTemplate.opsForValue().set("token",dog,30, TimeUnit.MINUTES);
  7. Dog dd = (Dog)redisTemplateUtils.get("token");
  8. System.out.println(dd);

API的使用

@Resource
private RedisTemplate redisTemplate;

set数据类型

获取set数据类型

  1. Set<Integer> followers = redisTemplate.opsForSet()
  2. .members(RedisKeyConstant.followers.getKey() + dinerId);

两个set类型key的交集

  1. Set<Integer> dinerIds = redisTemplate.opsForSet().intersect(loginDinerKey, dinerKey);

添加数据

  1. redisTemplate.opsForSet().add(RedisKeyConstant.following.getKey() + dinerId, followDinerId);

移除数据

  1. redisTemplate.opsForSet().remove(RedisKeyConstant.following.getKey() + dinerId, followDinerId);

hash数据类型

保存

  1. redisTemplate.opsForHash().putAll(key, restaurantMap);

list类型数据

获取list的前10条数据

  1. // 从 Redis 取十条最新评论
  2. List<LinkedHashMap> reviews = redisTemplate.opsForList().range(key, 0, NINE);

写入数据

最新的数据在前面。

  1. redisTemplate.opsForList().leftPush(key, reviews);

管道技术

管道为什么快?平常的是 一个插入请求ok之后,再接着插入下一个,管道是等插入完了之后再统一获取响应。

批量插入hash类型

一个是逐行插入,一个是批量插入。
2w条数据,逐行插入大概是2分钟。批量插入30秒左右

  1. package com.imooc.restaurants.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.google.common.collect.Maps;
  4. import com.imooc.commons.constant.RedisKeyConstant;
  5. import com.imooc.commons.model.pojo.Restaurant;
  6. import com.imooc.restaurants.RestaurantApplicationTest;
  7. import com.imooc.restaurants.mapper.RestaurantMapper;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.junit.jupiter.api.Test;
  10. import org.springframework.data.redis.core.RedisCallback;
  11. import org.springframework.data.redis.core.RedisTemplate;
  12. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  13. import org.springframework.data.redis.serializer.StringRedisSerializer;
  14. import javax.annotation.Resource;
  15. import java.util.List;
  16. import java.util.Map;
  17. @Slf4j
  18. public class RestaurantTest extends RestaurantApplicationTest {
  19. @Resource
  20. private RedisTemplate redisTemplate;
  21. @Resource
  22. private RestaurantMapper restaurantMapper;
  23. // 逐行插入
  24. @Test
  25. void testSyncForHash() {
  26. List<Restaurant> restaurants = restaurantMapper.findAll();
  27. long start = System.currentTimeMillis();
  28. restaurants.forEach(restaurant -> {
  29. Map<String, Object> restaurantMap = BeanUtil.beanToMap(restaurant);
  30. String key = RedisKeyConstant.restaurants.getKey() + restaurant.getId();
  31. redisTemplate.opsForHash().putAll(key, restaurantMap);
  32. });
  33. long end = System.currentTimeMillis();
  34. log.info("执行时间:{}", end - start); // 执行时间:118957
  35. }
  36. // Pipeline 管道插入
  37. @Test
  38. void testSyncForHashPipeline() {
  39. List<Restaurant> restaurants = restaurantMapper.findAll();
  40. long start = System.currentTimeMillis();
  41. List<Long> list = redisTemplate.executePipelined((RedisCallback<Long>) connection -> {
  42. for (Restaurant restaurant : restaurants) {
  43. try {
  44. String key = RedisKeyConstant.restaurants.getKey() + restaurant.getId();
  45. Map<String, Object> restaurantMap = BeanUtil.beanToMap(restaurant);
  46. StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  47. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  48. Map<byte[], byte[]> restaurantStringMap = Maps.newHashMap();
  49. restaurantMap.forEach((k, v) -> {
  50. restaurantStringMap.put(stringRedisSerializer.serialize(k), jackson2JsonRedisSerializer.serialize(v));
  51. });
  52. connection.hMSet(stringRedisSerializer.serialize(key), restaurantStringMap);
  53. } catch (Exception e) {
  54. log.info(restaurant.toString());
  55. continue;
  56. }
  57. }
  58. return null;
  59. });
  60. long end = System.currentTimeMillis();
  61. log.info("执行时间:{}", end - start); // 执行时间:35606
  62. }
  63. }