springboot整合redis配置

布满荆棘的人生 2024-03-27 14:56 137阅读 0赞

1、引入pom依赖

  1. <!-- redis 缓存操作 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>

2、增加配置类 RedisConfig

  1. package com.ruoyi.framework.config;
  2. import org.springframework.cache.annotation.CachingConfigurerSupport;
  3. import org.springframework.cache.annotation.EnableCaching;
  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.StringRedisSerializer;
  9. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  10. import com.fasterxml.jackson.annotation.PropertyAccessor;
  11. import com.fasterxml.jackson.databind.ObjectMapper;
  12. /**
  13. * redis配置
  14. */
  15. @Configuration
  16. @EnableCaching
  17. public class RedisConfig extends CachingConfigurerSupport
  18. {
  19. @Bean
  20. @SuppressWarnings(value = {
  21. "unchecked", "rawtypes" })
  22. public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
  23. {
  24. RedisTemplate<Object, Object> template = new RedisTemplate<>();
  25. template.setConnectionFactory(connectionFactory);
  26. FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
  27. ObjectMapper mapper = new ObjectMapper();
  28. mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  29. mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  30. serializer.setObjectMapper(mapper);
  31. template.setValueSerializer(serializer);
  32. // 使用StringRedisSerializer来序列化和反序列化redis的key值
  33. template.setKeySerializer(new StringRedisSerializer());
  34. template.afterPropertiesSet();
  35. return template;
  36. }
  37. }

3、自定义 redis 工具类

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

4、yml文件配置

  1. spring:
  2. # redis 配置
  3. redis:
  4. # 地址
  5. host: 127.0.0.1
  6. # 端口,默认为6379
  7. port: 6379
  8. # 密码
  9. password: 123456
  10. # 连接超时时间
  11. timeout: 10s
  12. lettuce:
  13. pool:
  14. max-active: 8 #最大连接数据库连接数,设 0 为没有限制
  15. max-idle: 8 #最大等待连接中的数量,设 0 为没有限制
  16. max-wait: -1ms #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
  17. min-idle: 0 #最小等待连接中的数量,设 0 为没有限制

发表评论

表情:
评论列表 (有 0 条评论,137人围观)

还没有评论,来说两句吧...

相关阅读