springboot整合redis(单机版)

╰+攻爆jí腚メ 2024-03-02 10:01 80阅读 0赞

引入maven

  1. <!-- redis -->
  2. <!-- 集成redis依赖 -->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-data-redis</artifactId>
  6. </dependency>
  7. <!-- redis连接池 -->
  8. <dependency>
  9. <groupId>org.apache.commons</groupId>
  10. <artifactId>commons-pool2</artifactId>
  11. </dependency>

yml配置

  1. spring:
  2. redis:
  3. host: 127.0.0.1:6379
  4. database: 0
  5. password:
  6. #redis连接池配置
  7. lettuce:
  8. pool:
  9. max-active: 32
  10. max-idle: 8
  11. max-wait: 16

配置config

  1. @Configuration
  2. public class RedisAutoConfigure {
  3. /**
  4. * RedisTemplate配置
  5. * @param
  6. */
  7. @Bean
  8. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
  9. RedisTemplate<String, Object> template = new RedisTemplate<>();
  10. template.setConnectionFactory(connectionFactory);
  11. template.setKeySerializer(new StringRedisSerializer());
  12. template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
  13. return template;
  14. }
  15. }

util工具类

  1. package com.user.utils;
  2. import org.springframework.data.redis.core.RedisTemplate;
  3. import org.springframework.stereotype.Component;
  4. import javax.annotation.Resource;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.concurrent.TimeUnit;
  10. @Component
  11. public class RedisUtil {
  12. @Resource
  13. private RedisTemplate<String,Object> redisTemplate;
  14. /**
  15. * 给一个指定的 key 值附加过期时间
  16. *
  17. * @param key
  18. * @param time
  19. * @return
  20. */
  21. public boolean expire(String key, long time) {
  22. return Boolean.TRUE.equals(redisTemplate.expire(key, time, TimeUnit.SECONDS));
  23. }
  24. /**
  25. * 根据key 获取过期时间
  26. *
  27. * @param key
  28. * @return
  29. */
  30. public long getTime(String key) {
  31. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  32. }
  33. /**
  34. * 根据key 获取过期时间
  35. *
  36. * @param key
  37. * @return
  38. */
  39. public boolean hasKey(String key) {
  40. return redisTemplate.hasKey(key);
  41. }
  42. /**
  43. * 移除指定key 的过期时间
  44. *
  45. * @param key
  46. * @return
  47. */
  48. public boolean persist(String key) {
  49. return redisTemplate.boundValueOps(key).persist();
  50. }
  51. //- - - - - - - - - - - - - - - - - - - - - String类型 - - - - - - - - - - - - - - - - - - - -
  52. /**
  53. * 根据key获取值
  54. *
  55. * @param key 键
  56. * @return 值
  57. */
  58. public Object get(String key) {
  59. return key == null ? null : redisTemplate.opsForValue().get(key);
  60. }
  61. /**
  62. * 将值放入缓存
  63. *
  64. * @param key 键
  65. * @param value 值
  66. * @return true成功 false 失败
  67. */
  68. public void set(String key, String value) {
  69. redisTemplate.opsForValue().set(key, value);
  70. }
  71. /**
  72. * 将值放入缓存并设置时间
  73. *
  74. * @param key 键
  75. * @param value 值
  76. * @param time 时间(秒) -1为无期限
  77. * @return true成功 false 失败
  78. */
  79. public void set(String key, String value, long time) {
  80. if (time > 0) {
  81. redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
  82. } else {
  83. redisTemplate.opsForValue().set(key, value);
  84. }
  85. }
  86. /**
  87. * 批量添加 key (重复的键会覆盖)
  88. *
  89. * @param keyAndValue
  90. */
  91. public void batchSet(Map<String, String> keyAndValue) {
  92. redisTemplate.opsForValue().multiSet(keyAndValue);
  93. }
  94. /**
  95. * 批量添加 key-value 只有在键不存在时,才添加
  96. * map 中只要有一个key存在,则全部不添加
  97. *
  98. * @param keyAndValue
  99. */
  100. public void batchSetIfAbsent(Map<String, String> keyAndValue) {
  101. redisTemplate.opsForValue().multiSetIfAbsent(keyAndValue);
  102. }
  103. /**
  104. * 对一个 key-value 的值进行加减操作,
  105. * 如果该 key 不存在 将创建一个key 并赋值该 number
  106. * 如果 key 存在,但 value 不是长整型 ,将报错
  107. *
  108. * @param key
  109. * @param number
  110. */
  111. public Long increment(String key, long number) {
  112. return redisTemplate.opsForValue().increment(key, number);
  113. }
  114. /**
  115. * 对一个 key-value 的值进行加减操作,
  116. * 如果该 key 不存在 将创建一个key 并赋值该 number
  117. * 如果 key 存在,但 value 不是 纯数字 ,将报错
  118. *
  119. * @param key
  120. * @param number
  121. */
  122. public Double increment(String key, double number) {
  123. return redisTemplate.opsForValue().increment(key, number);
  124. }
  125. //- - - - - - - - - - - - - - - - - - - - - set类型 - - - - - - - - - - - - - - - - - - - -
  126. /**
  127. * 将数据放入set缓存
  128. *
  129. * @param key 键
  130. * @return
  131. */
  132. public void sSet(String key, String value) {
  133. redisTemplate.opsForSet().add(key, value);
  134. }
  135. /**
  136. * 获取变量中的值
  137. *
  138. * @param key 键
  139. * @return
  140. */
  141. public Set<Object> members(String key) {
  142. return redisTemplate.opsForSet().members(key);
  143. }
  144. /**
  145. * 随机获取变量中指定个数的元素
  146. *
  147. * @param key 键
  148. * @param count 值
  149. * @return
  150. */
  151. public void randomMembers(String key, long count) {
  152. redisTemplate.opsForSet().randomMembers(key, count);
  153. }
  154. /**
  155. * 随机获取变量中的元素
  156. *
  157. * @param key 键
  158. * @return
  159. */
  160. public Object randomMember(String key) {
  161. return redisTemplate.opsForSet().randomMember(key);
  162. }
  163. /**
  164. * 弹出变量中的元素
  165. *
  166. * @param key 键
  167. * @return
  168. */
  169. public Object pop(String key) {
  170. return redisTemplate.opsForSet().pop("setValue");
  171. }
  172. /**
  173. * 获取变量中值的长度
  174. *
  175. * @param key 键
  176. * @return
  177. */
  178. public long size(String key) {
  179. return redisTemplate.opsForSet().size(key);
  180. }
  181. /**
  182. * 根据value从一个set中查询,是否存在
  183. *
  184. * @param key 键
  185. * @param value 值
  186. * @return true 存在 false不存在
  187. */
  188. public boolean sHasKey(String key, Object value) {
  189. return redisTemplate.opsForSet().isMember(key, value);
  190. }
  191. /**
  192. * 检查给定的元素是否在变量中。
  193. *
  194. * @param key 键
  195. * @param obj 元素对象
  196. * @return
  197. */
  198. public boolean isMember(String key, Object obj) {
  199. return redisTemplate.opsForSet().isMember(key, obj);
  200. }
  201. /**
  202. * 转移变量的元素值到目的变量。
  203. *
  204. * @param key 键
  205. * @param value 元素对象
  206. * @param destKey 元素对象
  207. * @return
  208. */
  209. public boolean move(String key, String value, String destKey) {
  210. return redisTemplate.opsForSet().move(key, value, destKey);
  211. }
  212. /**
  213. * 批量移除set缓存中元素
  214. *
  215. * @param key 键
  216. * @param values 值
  217. * @return
  218. */
  219. public void remove(String key, Object... values) {
  220. redisTemplate.opsForSet().remove(key, values);
  221. }
  222. /**
  223. * 通过给定的key求2个set变量的差值
  224. *
  225. * @param key 键
  226. * @param destKey 键
  227. * @return
  228. */
  229. public Set<Set> difference(String key, String destKey) {
  230. return Collections.singleton(redisTemplate.opsForSet().difference(key, destKey));
  231. }
  232. //- - - - - - - - - - - - - - - - - - - - - hash类型 - - - - - - - - - - - - - - - - - - - -
  233. /**
  234. * 加入缓存
  235. *
  236. * @param key 键
  237. * @param map 键
  238. * @return
  239. */
  240. public void add(String key, Map<String, Object> map) {
  241. redisTemplate.opsForHash().putAll(key, map);
  242. }
  243. /**
  244. * 获取 key 下的 所有 hashkey 和 value
  245. *
  246. * @param key 键
  247. * @return
  248. */
  249. public Map<Object, Object> getHashEntries(String key) {
  250. return redisTemplate.opsForHash().entries(key);
  251. }
  252. /**
  253. * 验证指定 key 下 有没有指定的 hashkey
  254. *
  255. * @param key
  256. * @param hashKey
  257. * @return
  258. */
  259. public boolean hashKey(String key, String hashKey) {
  260. return redisTemplate.opsForHash().hasKey(key, hashKey);
  261. }
  262. /**
  263. * 获取指定key的值string
  264. *
  265. * @param key 键
  266. * @param key2 键
  267. * @return
  268. */
  269. public String getMapString(String key, String key2) {
  270. return redisTemplate.opsForHash().get("map1", "key1").toString();
  271. }
  272. /**
  273. * 获取指定的值Int
  274. *
  275. * @param key 键
  276. * @param key2 键
  277. * @return
  278. */
  279. public Integer getMapInt(String key, String key2) {
  280. return (Integer) redisTemplate.opsForHash().get("map1", "key1");
  281. }
  282. /**
  283. * 弹出元素并删除
  284. *
  285. * @param key 键
  286. * @return
  287. */
  288. public String popValue(String key) {
  289. return redisTemplate.opsForSet().pop(key).toString();
  290. }
  291. /**
  292. * 删除指定 hash 的 HashKey
  293. *
  294. * @param key
  295. * @param hashKeys
  296. * @return 删除成功的 数量
  297. */
  298. public Long delete(String key, String... hashKeys) {
  299. return redisTemplate.opsForHash().delete(key, hashKeys);
  300. }
  301. /**
  302. * 给指定 hash 的 hashkey 做增减操作
  303. *
  304. * @param key
  305. * @param hashKey
  306. * @param number
  307. * @return
  308. */
  309. public Long increment(String key, String hashKey, long number) {
  310. return redisTemplate.opsForHash().increment(key, hashKey, number);
  311. }
  312. /**
  313. * 给指定 hash 的 hashkey 做增减操作
  314. *
  315. * @param key
  316. * @param hashKey
  317. * @param number
  318. * @return
  319. */
  320. public Double increment(String key, String hashKey, Double number) {
  321. return redisTemplate.opsForHash().increment(key, hashKey, number);
  322. }
  323. /**
  324. * 获取 key 下的 所有 hashkey 字段
  325. *
  326. * @param key
  327. * @return
  328. */
  329. public Set<Object> hashKeys(String key) {
  330. return redisTemplate.opsForHash().keys(key);
  331. }
  332. /**
  333. * 获取指定 hash 下面的 键值对 数量
  334. *
  335. * @param key
  336. * @return
  337. */
  338. public Long hashSize(String key) {
  339. return redisTemplate.opsForHash().size(key);
  340. }
  341. //- - - - - - - - - - - - - - - - - - - - - list类型 - - - - - - - - - - - - - - - - - - - -
  342. /**
  343. * 在变量左边添加元素值
  344. *
  345. * @param key
  346. * @param value
  347. * @return
  348. */
  349. public void leftPush(String key, Object value) {
  350. redisTemplate.opsForList().leftPush(key, value);
  351. }
  352. /**
  353. * 获取集合指定位置的值。
  354. *
  355. * @param key
  356. * @param index
  357. * @return
  358. */
  359. public Object index(String key, long index) {
  360. return redisTemplate.opsForList().index("list", 1);
  361. }
  362. /**
  363. * 获取指定区间的值。
  364. *
  365. * @param key
  366. * @param start
  367. * @param end
  368. * @return
  369. */
  370. public List<Object> range(String key, long start, long end) {
  371. return redisTemplate.opsForList().range(key, start, end);
  372. }
  373. /**
  374. * 把最后一个参数值放到指定集合的第一个出现中间参数的前面,
  375. * 如果中间参数值存在的话。
  376. *
  377. * @param key
  378. * @param pivot
  379. * @param value
  380. * @return
  381. */
  382. public void leftPush(String key, String pivot, String value) {
  383. redisTemplate.opsForList().leftPush(key, pivot, value);
  384. }
  385. /**
  386. * 向左边批量添加参数元素。
  387. *
  388. * @param key
  389. * @param values
  390. * @return
  391. */
  392. public void leftPushAll(String key, String... values) {
  393. // redisTemplate.opsForList().leftPushAll(key,"w","x","y");
  394. redisTemplate.opsForList().leftPushAll(key, values);
  395. }
  396. /**
  397. * 向集合最右边添加元素。
  398. *
  399. * @param key
  400. * @param value
  401. * @return
  402. */
  403. public void leftPushAll(String key, String value) {
  404. redisTemplate.opsForList().rightPush(key, value);
  405. }
  406. /**
  407. * 向左边批量添加参数元素。
  408. *
  409. * @param key
  410. * @param values
  411. * @return
  412. */
  413. public void rightPushAll(String key, String... values) {
  414. //redisTemplate.opsForList().leftPushAll(key,"w","x","y");
  415. redisTemplate.opsForList().rightPushAll(key, values);
  416. }
  417. /**
  418. * 向已存在的集合中添加元素。
  419. *
  420. * @param key
  421. * @param value
  422. * @return
  423. */
  424. public void rightPushIfPresent(String key, Object value) {
  425. redisTemplate.opsForList().rightPushIfPresent(key, value);
  426. }
  427. /**
  428. * 向已存在的集合中添加元素。
  429. *
  430. * @param key
  431. * @return
  432. */
  433. public long listLength(String key) {
  434. return redisTemplate.opsForList().size(key);
  435. }
  436. /**
  437. * 移除集合中的左边第一个元素。
  438. *
  439. * @param key
  440. * @return
  441. */
  442. public void leftPop(String key) {
  443. redisTemplate.opsForList().leftPop(key);
  444. }
  445. /**
  446. * 移除集合中左边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。
  447. *
  448. * @param key
  449. * @return
  450. */
  451. public void leftPop(String key, long timeout, TimeUnit unit) {
  452. redisTemplate.opsForList().leftPop(key, timeout, unit);
  453. }
  454. /**
  455. * 移除集合中右边的元素。
  456. *
  457. * @param key
  458. * @return
  459. */
  460. public void rightPop(String key) {
  461. redisTemplate.opsForList().rightPop(key);
  462. }
  463. /**
  464. * 移除集合中右边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。
  465. *
  466. * @param key
  467. * @return
  468. */
  469. public void rightPop(String key, long timeout, TimeUnit unit) {
  470. redisTemplate.opsForList().rightPop(key, timeout, unit);
  471. }
  472. }

测试

  1. @RequestMapping("/test")
  2. public String test(String key, String value) {
  3. // 设置键值对
  4. redisUtil.set("key", "value");
  5. // 获取值
  6. String valu2 = (String) redisUtil.get("key");
  7. return valu2;
  8. }

发表评论

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

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

相关阅读