自己整理了 spring boot 結合 redis 的工具類
引入依賴
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency> |
加入配置
1
2
3
4
5
6
|
# redis數據庫索引(默認為 0 ) spring.redis.database= 0 # redis服務器地址 spring.redis.host=localhost # redis服務器連接端口 spring.redis.port= 6379 |
實現代碼
這里用到了 靜態類工具類中 如何使用 @autowired
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
package com.lmxdawn.api.common.utils; import java.util.collection; import java.util.set; import java.util.concurrent.timeunit; import javax.annotation.postconstruct; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.redis.core.redistemplate; import org.springframework.stereotype.component; /** * 緩存操作類 */ @component public class cacheutils { @autowired private redistemplate<string, string> redistemplate; // 維護一個本類的靜態變量 private static cacheutils cacheutils; @postconstruct public void init() { cacheutils = this ; cacheutils.redistemplate = this .redistemplate; } /** * 將參數中的字符串值設置為鍵的值,不設置過期時間 * @param key * @param value 必須要實現 serializable 接口 */ public static void set(string key, string value) { cacheutils.redistemplate.opsforvalue().set(key, value); } /** * 將參數中的字符串值設置為鍵的值,設置過期時間 * @param key * @param value 必須要實現 serializable 接口 * @param timeout */ public static void set(string key, string value, long timeout) { cacheutils.redistemplate.opsforvalue().set(key, value, timeout, timeunit.seconds); } /** * 獲取與指定鍵相關的值 * @param key * @return */ public static object get(string key) { return cacheutils.redistemplate.opsforvalue().get(key); } /** * 設置某個鍵的過期時間 * @param key 鍵值 * @param ttl 過期秒數 */ public static boolean expire(string key, long ttl) { return cacheutils.redistemplate.expire(key, ttl, timeunit.seconds); } /** * 判斷某個鍵是否存在 * @param key 鍵值 */ public static boolean haskey(string key) { return cacheutils.redistemplate.haskey(key); } /** * 向集合添加元素 * @param key * @param value * @return 返回值為設置成功的value數 */ public static long sadd(string key, string... value) { return cacheutils.redistemplate.opsforset().add(key, value); } /** * 獲取集合中的某個元素 * @param key * @return 返回值為redis中鍵值為key的value的set集合 */ public static set<string> sgetmembers(string key) { return cacheutils.redistemplate.opsforset().members(key); } /** * 將給定分數的指定成員添加到鍵中存儲的排序集合中 * @param key * @param value * @param score * @return */ public static boolean zadd(string key, string value, double score) { return cacheutils.redistemplate.opsforzset().add(key, value, score); } /** * 返回指定排序集中給定成員的分數 * @param key * @param value * @return */ public static double zscore(string key, string value) { return cacheutils.redistemplate.opsforzset().score(key, value); } /** * 刪除指定的鍵 * @param key * @return */ public static boolean delete(string key) { return cacheutils.redistemplate.delete(key); } /** * 刪除多個鍵 * @param keys * @return */ public static long delete(collection<string> keys) { return cacheutils.redistemplate.delete(keys); } } |
相關地址
github 地址:https://github.com/lmxdawn/vue-admin-java
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://segmentfault.com/a/1190000017131552