1、参考:https://cloud.tencent.com/developer/article/1377455
2、首先创建一个maven项目。然后加入依赖的jar包就行了。我加入的jar包很多,反正加入了也没啥坏的影响。
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4 <groupId>com.charts</groupId>
5 <artifactId>com.fline.aic.charts</artifactId>
6 <packaging>war</packaging>
7 <version>0.0.1-SNAPSHOT</version>
8 <name>com.fline.aic.charts Maven Webapp</name>
9 <url>http://maven.apache.org</url>
10
11 <dependencies>
12 <dependency>
13 <groupId>junit</groupId>
14 <artifactId>junit</artifactId>
15 <version>3.8.1</version>
16 <scope>test</scope>
17 </dependency>
18 <!-- https://mvnrepository.com/artifact/log4j/log4j -->
19 <dependency>
20 <groupId>log4j</groupId>
21 <artifactId>log4j</artifactId>
22 <version>1.2.17</version>
23 </dependency>
24 <!-- https://mvnrepository.com/artifact/com.github.kstyrc/embedded-redis -->
25 <dependency>
26 <groupId>com.github.kstyrc</groupId>
27 <artifactId>embedded-redis</artifactId>
28 <version>0.6</version>
29 <scope>test</scope>
30 </dependency>
31 <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
32 <dependency>
33 <groupId>redis.clients</groupId>
34 <artifactId>jedis</artifactId>
35 <version>2.9.0</version>
36 </dependency>
37 <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
38 <dependency>
39 <groupId>org.springframework.data</groupId>
40 <artifactId>spring-data-redis</artifactId>
41 <version>1.7.2.RELEASE</version>
42 </dependency>
43 <!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
44 <dependency>
45 <groupId>commons-pool</groupId>
46 <artifactId>commons-pool</artifactId>
47 <version>1.5.4</version>
48 </dependency>
49 <dependency>
50 <groupId>com.fasterxml.jackson.core</groupId>
51 <artifactId>jackson-core</artifactId>
52 <version>2.1.0</version>
53 </dependency>
54 <dependency>
55 <groupId>com.fasterxml.jackson.core</groupId>
56 <artifactId>jackson-databind</artifactId>
57 <version>2.1.0</version>
58 </dependency>
59 <dependency>
60 <groupId>com.fasterxml.jackson.core</groupId>
61 <artifactId>jackson-annotations</artifactId>
62 <version>2.1.0</version>
63 </dependency>
64
65 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
66 <dependency>
67 <groupId>org.springframework</groupId>
68 <artifactId>spring-core</artifactId>
69 <version>4.3.18.RELEASE</version>
70 </dependency>
71 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
72 <dependency>
73 <groupId>org.springframework</groupId>
74 <artifactId>spring-context</artifactId>
75 <version>4.3.18.RELEASE</version>
76 </dependency>
77 <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
78 <dependency>
79 <groupId>org.springframework</groupId>
80 <artifactId>spring-web</artifactId>
81 <version>4.3.18.RELEASE</version>
82 </dependency>
83 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
84 <dependency>
85 <groupId>org.springframework</groupId>
86 <artifactId>spring-aop</artifactId>
87 <version>4.3.18.RELEASE</version>
88 </dependency>
89 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
90 <dependency>
91 <groupId>org.springframework</groupId>
92 <artifactId>spring-webmvc</artifactId>
93 <version>4.3.18.RELEASE</version>
94 </dependency>
95 <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
96 <dependency>
97 <groupId>org.springframework</groupId>
98 <artifactId>spring-beans</artifactId>
99 <version>4.3.18.RELEASE</version>
100 </dependency>
101 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
102 <dependency>
103 <groupId>org.springframework</groupId>
104 <artifactId>spring-jdbc</artifactId>
105 <version>4.3.18.RELEASE</version>
106 </dependency>
107 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
108 <dependency>
109 <groupId>org.springframework</groupId>
110 <artifactId>spring-webmvc</artifactId>
111 <version>4.3.18.RELEASE</version>
112 </dependency>
113 <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
114 <dependency>
115 <groupId>org.springframework</groupId>
116 <artifactId>spring-test</artifactId>
117 <version>4.3.18.RELEASE</version>
118 <scope>test</scope>
119 </dependency>
120 <dependency>
121 <groupId>org.aspectj</groupId>
122 <artifactId>aspectjweaver</artifactId>
123 <version>1.8.13</version>
124 </dependency>
125
126 </dependencies>
127 <build>
128 <finalName>com.fline.aic.charts</finalName>
129 </build>
130
131
132
133 </project>
3、然后在web.xml里面配置一下Spring框架listener配置。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
3 <display-name>Spring_Struts2_20170313</display-name>
4 <welcome-file-list>
5 <welcome-file>index.html</welcome-file>
6 <welcome-file>index.htm</welcome-file>
7 <welcome-file>index.jsp</welcome-file>
8 <welcome-file>default.html</welcome-file>
9 <welcome-file>default.htm</welcome-file>
10 <welcome-file>default.jsp</welcome-file>
11 </welcome-file-list>
12
13
14 <!-- 1:spring配置 ,在spring-framework-3.2.5.RELEASE\docs\spring-framework-reference\htmlsingle
15 搜索context-param找到下面这段话即可。记得就该param-value的值,如下所示;
16 2:param-value的值最好使用bean,这样方便引用如/WEB-INF/classes/bean-*.xml
17 -->
18 <context-param>
19 <param-name>contextConfigLocation</param-name>
20 <!-- <param-value>classpath:applicationContext.xml</param-value> -->
21 <param-value>/WEB-INF/applicationContext.xml</param-value>
22 </context-param>
23 <listener>
24 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
25 </listener>
26
27
28 </web-app>
4、然后配置一下Spring的配置文件。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:p="http://www.springframework.org/schema/p"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="
7 http://www.springframework.org/schema/beans
8 http://www.springframework.org/schema/beans/spring-beans.xsd
9 http://www.springframework.org/schema/context
10 http://www.springframework.org/schema/context/spring-context.xsd">
11
12 <!-- 开启注解扫描 -->
13 <context:annotation-config/>
14 <context:component-scan base-package="com.fline.*"></context:component-scan>
15
16 <!-- <import resource="classpath:applicationContext-redis.xml" /> -->
17
18 </beans>
5、然后配置一下Redis的配置文件redis.properties和applicationContext-redis.xml。
1 redis.host=127.0.0.1
2 redis.port=6379
3 redis.password=123456
4
5 redis.maxIdle=300
6 redis.maxActive=600
7 redis.maxWait=1000
8 redis.testOnBorrow=true
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
6
7 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" >
8 <!--最大空闲数-->
9 <property name="maxIdle" value="10" />
10 <!--连接池的最大数据库连接数 -->
11 <property name="maxTotal" value="50" />
12 <!--最大建立连接等待时间-->
13 <property name="maxWaitMillis" value="1500" />
14 <!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)-->
15 <property name="minEvictableIdleTimeMillis" value="1800000" />
16 <!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3-->
17 <property name="numTestsPerEvictionRun" value="1024" />
18 <!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1-->
19 <property name="timeBetweenEvictionRunsMillis" value="30000" />
20 <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
21 <property name="testOnBorrow" value="true" />
22 <!--在空闲时检查有效性, 默认false -->
23 <property name="testWhileIdle" value="true" />
24 </bean >
25
26 <!--redis连接工厂 -->
27 <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
28 <property name="poolConfig" ref="jedisPoolConfig"></property>
29 <!--IP地址 -->
30 <property name="hostName" value="127.0.0.1"></property>
31 <!--端口号 -->
32 <property name="port" value="6379"></property>
33 <!--如果Redis设置有密码 -->
34 <property name="password" value="123456" />
35 <!--客户端超时时间单位是毫秒 -->
36 <property name="timeout" value="1000"></property>
37 </bean>
38
39 <!--redis操作模版,使用该对象可以操作redis -->
40 <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
41 <property name="connectionFactory" ref="jedisConnectionFactory" />
42 <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!! -->
43 <property name="keySerializer" >
44 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
45 </property>
46 <property name="valueSerializer" >
47 <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
48 </property>
49 <property name="hashKeySerializer">
50 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
51 </property>
52 <property name="hashValueSerializer">
53 <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
54 </property>
55 <!--开启事务 -->
56 <property name="enableTransactionSupport" value="true"></property>
57 </bean >
58
59 <bean id="redisUtil" class="com.fline.aic.utils.RedisUtil">
60 <property name="redisTemplate" ref="redisTemplate" />
61 </bean>
62
63 </beans>
6、redis的工具类,可以直接拿到项目中使用的。
1 package com.fline.aic.utils;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.Set;
6 import java.util.concurrent.TimeUnit;
7
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.data.redis.core.RedisTemplate;
10 import org.springframework.stereotype.Component;
11 import org.springframework.util.CollectionUtils;
12
13 /**
14 *
15 * @author QLQ 基于spring和redis的redisTemplate工具类 针对所有的hash 都是以h开头的方法 针对所有的Set
16 * 都是以s开头的方法 不含通用方法 针对所有的List 都是以l开头的方法
17 */
18 @Component // 交给Spring管理(在需要缓存的地方自动注入即可使用)
19 public class RedisUtil {
20
21 @Autowired // (自动注入redisTemplet)
22 private RedisTemplate<String, Object> redisTemplate;
23
24 public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
25 this.redisTemplate = redisTemplate;
26 }
27 // =============================common============================
28
29 /**
30 * 指定缓存失效时间
31 *
32 * @param key
33 * 键
34 * @param time
35 * 时间(秒)
36 * @return
37 */
38 public boolean expire(String key, long time) {
39 try {
40 if (time > 0) {
41 redisTemplate.expire(key, time, TimeUnit.SECONDS);
42 }
43 return true;
44 } catch (Exception e) {
45 e.printStackTrace();
46 return false;
47 }
48 }
49
50 /**
51 * 根据key 获取过期时间
52 *
53 * @param key
54 * 键 不能为null
55 * @return 时间(秒) 返回0代表为永久有效
56 */
57 public long getExpire(String key) {
58 return redisTemplate.getExpire(key, TimeUnit.SECONDS);
59 }
60
61 /**
62 * 判断key是否存在
63 *
64 * @param key
65 * 键
66 * @return true 存在 false不存在
67 */
68 public boolean hasKey(String key) {
69 try {
70 return redisTemplate.hasKey(key);
71 } catch (Exception e) {
72 e.printStackTrace();
73 return false;
74 }
75 }
76
77 /**
78 * 删除缓存
79 *
80 * @param key
81 * 可以传一个值 或多个
82 */
83 @SuppressWarnings("unchecked")
84 public void del(String... key) {
85 if (key != null && key.length > 0) {
86 if (key.length == 1) {
87 redisTemplate.delete(key[0]);
88 } else {
89 redisTemplate.delete(CollectionUtils.arrayToList(key));
90 }
91 }
92 }
93
94 // ============================String=============================
95 /**
96 * 普通缓存获取
97 *
98 * @param key
99 * 键
100 * @return 值
101 */
102 public Object get(String key) {
103 return key == null ? null : redisTemplate.opsForValue().get(key);
104 }
105
106 /**
107 * 普通缓存放入
108 *
109 * @param key
110 * 键
111 * @param value
112 * 值
113 * @return true成功 false失败
114 */
115 public boolean set(String key, Object value) {
116 try {
117 redisTemplate.opsForValue().set(key, value);
118 return true;
119 } catch (Exception e) {
120 e.printStackTrace();
121 return false;
122 }
123
124 }
125
126 /**
127 * 普通缓存放入并设置时间
128 *
129 * @param key
130 * 键
131 * @param value
132 * 值
133 * @param time
134 * 时间(秒) time要大于0 如果time小于等于0 将设置无限期
135 * @return true成功 false 失败
136 */
137 public boolean set(String key, Object value, long time) {
138 try {
139 if (time > 0) {
140 redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
141 } else {
142 set(key, value);
143 }
144 return true;
145 } catch (Exception e) {
146 e.printStackTrace();
147 return false;
148 }
149 }
150
151 /**
152 * 递增
153 *
154 * @param key
155 * 键
156 * @param by
157 * 要增加几(大于0)
158 * @return
159 */
160 public long incr(String key, long delta) {
161 if (delta < 0) {
162 throw new RuntimeException("递增因子必须大于0");
163 }
164 return redisTemplate.opsForValue().increment(key, delta);
165 }
166
167 /**
168 * 递减
169 *
170 * @param key
171 * 键
172 * @param by
173 * 要减少几(小于0)
174 * @return
175 */
176 public long decr(String key, long delta) {
177 if (delta < 0) {
178 throw new RuntimeException("递减因子必须大于0");
179 }
180 return redisTemplate.opsForValue().increment(key, -delta);
181 }
182
183 // ================================Map=================================
184 /**
185 * HashGet
186 *
187 * @param key
188 * 键 不能为null
189 * @param item
190 * 项 不能为null
191 * @return 值
192 */
193 public Object hget(String key, String item) {
194 return redisTemplate.opsForHash().get(key, item);
195 }
196
197 /**
198 * 获取hashKey对应的所有键值
199 *
200 * @param key
201 * 键
202 * @return 对应的多个键值
203 */
204 public Map<Object, Object> hmget(String key) {
205 return redisTemplate.opsForHash().entries(key);
206 }
207
208 /**
209 * HashSet
210 *
211 * @param key
212 * 键
213 * @param map
214 * 对应多个键值
215 * @return true 成功 false 失败
216 */
217 public boolean hmset(String key, Map<String, Object> map) {
218 try {
219 redisTemplate.opsForHash().putAll(key, map);
220 return true;
221 } catch (Exception e) {
222 e.printStackTrace();
223 return false;
224 }
225 }
226
227 /**
228 * HashSet 并设置时间
229 *
230 * @param key
231 * 键
232 * @param map
233 * 对应多个键值
234 * @param time
235 * 时间(秒)
236 * @return true成功 false失败
237 */
238 public boolean hmset(String key, Map<String, Object> map, long time) {
239 try {
240 redisTemplate.opsForHash().putAll(key, map);
241 if (time > 0) {
242 expire(key, time);
243 }
244 return true;
245 } catch (Exception e) {
246 e.printStackTrace();
247 return false;
248 }
249 }
250
251 /**
252 * 向一张hash表中放入数据,如果不存在将创建
253 *
254 * @param key
255 * 键
256 * @param item
257 * 项
258 * @param value
259 * 值
260 * @return true 成功 false失败
261 */
262 public boolean hset(String key, String item, Object value) {
263 try {
264 redisTemplate.opsForHash().put(key, item, value);
265 return true;
266 } catch (Exception e) {
267 e.printStackTrace();
268 return false;
269 }
270 }
271
272 /**
273 * 向一张hash表中放入数据,如果不存在将创建
274 *
275 * @param key
276 * 键
277 * @param item
278 * 项
279 * @param value
280 * 值
281 * @param time
282 * 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
283 * @return true 成功 false失败
284 */
285 public boolean hset(String key, String item, Object value, long time) {
286 try {
287 redisTemplate.opsForHash().put(key, item, value);
288 if (time > 0) {
289 expire(key, time);
290 }
291 return true;
292 } catch (Exception e) {
293 e.printStackTrace();
294 return false;
295 }
296 }
297
298 /**
299 * 删除hash表中的值
300 *
301 * @param key
302 * 键 不能为null
303 * @param item
304 * 项 可以使多个 不能为null
305 */
306 public void hdel(String key, Object... item) {
307 redisTemplate.opsForHash().delete(key, item);
308 }
309
310 /**
311 * 判断hash表中是否有该项的值
312 *
313 * @param key
314 * 键 不能为null
315 * @param item
316 * 项 不能为null
317 * @return true 存在 false不存在
318 */
319 public boolean hHasKey(String key, String item) {
320 return redisTemplate.opsForHash().hasKey(key, item);
321 }
322
323 /**
324 * hash递增 如果不存在,就会创建一个 并把新增后的值返回
325 *
326 * @param key
327 * 键
328 * @param item
329 * 项
330 * @param by
331 * 要增加几(大于0)
332 * @return
333 */
334 public double hincr(String key, String item, double by) {
335 return redisTemplate.opsForHash().increment(key, item, by);
336 }
337
338 /**
339 * hash递减
340 *
341 * @param key
342 * 键
343 * @param item
344 * 项
345 * @param by
346 * 要减少记(小于0)
347 * @return
348 */
349 public double hdecr(String key, String item, double by) {
350 return redisTemplate.opsForHash().increment(key, item, -by);
351 }
352
353 // ============================set=============================
354 /**
355 * 根据key获取Set中的所有值
356 *
357 * @param key
358 * 键
359 * @return
360 */
361 public Set<Object> sGet(String key) {
362 try {
363 return redisTemplate.opsForSet().members(key);
364 } catch (Exception e) {
365 e.printStackTrace();
366 return null;
367 }
368 }
369
370 /**
371 * 根据value从一个set中查询,是否存在
372 *
373 * @param key
374 * 键
375 * @param value
376 * 值
377 * @return true 存在 false不存在
378 */
379 public boolean sHasKey(String key, Object value) {
380 try {
381 return redisTemplate.opsForSet().isMember(key, value);
382 } catch (Exception e) {
383 e.printStackTrace();
384 return false;
385 }
386 }
387
388 /**
389 * 将数据放入set缓存
390 *
391 * @param key
392 * 键
393 * @param values
394 * 值 可以是多个
395 * @return 成功个数
396 */
397 public long sSet(String key, Object... values) {
398 try {
399 return redisTemplate.opsForSet().add(key, values);
400 } catch (Exception e) {
401 e.printStackTrace();
402 return 0;
403 }
404 }
405
406 /**
407 * 将set数据放入缓存
408 *
409 * @param key
410 * 键
411 * @param time
412 * 时间(秒)
413 * @param values
414 * 值 可以是多个
415 * @return 成功个数
416 */
417 public long sSetAndTime(String key, long time, Object... values) {
418 try {
419 Long count = redisTemplate.opsForSet().add(key, values);
420 if (time > 0)
421 expire(key, time);
422 return count;
423 } catch (Exception e) {
424 e.printStackTrace();
425 return 0;
426 }
427 }
428
429 /**
430 * 获取set缓存的长度
431 *
432 * @param key
433 * 键
434 * @return
435 */
436 public long sGetSetSize(String key) {
437 try {
438 return redisTemplate.opsForSet().size(key);
439 } catch (Exception e) {
440 e.printStackTrace();
441 return 0;
442 }
443 }
444
445 /**
446 * 移除值为value的
447 *
448 * @param key
449 * 键
450 * @param values
451 * 值 可以是多个
452 * @return 移除的个数
453 */
454 public long setRemove(String key, Object... values) {
455 try {
456 Long count = redisTemplate.opsForSet().remove(key, values);
457 return count;
458 } catch (Exception e) {
459 e.printStackTrace();
460 return 0;
461 }
462 }
463 // ===============================list=================================
464
465 /**
466 * 获取list缓存的内容
467 *
468 * @param key
469 * 键
470 * @param start
471 * 开始
472 * @param end
473 * 结束 0 到 -1代表所有值
474 * @return
475 */
476 public List<Object> lGet(String key, long start, long end) {
477 try {
478 return redisTemplate.opsForList().range(key, start, end);
479 } catch (Exception e) {
480 e.printStackTrace();
481 return null;
482 }
483 }
484
485 /**
486 * 获取list缓存的长度
487 *
488 * @param key
489 * 键
490 * @return
491 */
492 public long lGetListSize(String key) {
493 try {
494 return redisTemplate.opsForList().size(key);
495 } catch (Exception e) {
496 e.printStackTrace();
497 return 0;
498 }
499 }
500
501 /**
502 * 通过索引 获取list中的值
503 *
504 * @param key
505 * 键
506 * @param index
507 * 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
508 * @return
509 */
510 public Object lGetIndex(String key, long index) {
511 try {
512 return redisTemplate.opsForList().index(key, index);
513 } catch (Exception e) {
514 e.printStackTrace();
515 return null;
516 }
517 }
518
519 /**
520 * 将list放入缓存
521 *
522 * @param key
523 * 键
524 * @param value
525 * 值
526 * @param time
527 * 时间(秒)
528 * @return
529 */
530 public boolean lSet(String key, Object value) {
531 try {
532 redisTemplate.opsForList().rightPush(key, value);
533 return true;
534 } catch (Exception e) {
535 e.printStackTrace();
536 return false;
537 }
538 }
539
540 /**
541 * 将list放入缓存
542 *
543 * @param key
544 * 键
545 * @param value
546 * 值
547 * @param time
548 * 时间(秒)
549 * @return
550 */
551 public boolean rSet(String key, Object value) {
552 try {
553 redisTemplate.opsForList().rightPush(key, value);
554 return true;
555 } catch (Exception e) {
556 e.printStackTrace();
557 return false;
558 }
559 }
560
561 /**
562 * 将list放入缓存
563 *
564 * @param key
565 * 键
566 * @param value
567 * 值
568 * @param time
569 * 时间(秒)
570 * @return
571 */
572 public boolean lSet(String key, Object value, long time) {
573 try {
574 redisTemplate.opsForList().rightPush(key, value);
575 if (time > 0)
576 expire(key, time);
577 return true;
578 } catch (Exception e) {
579 e.printStackTrace();
580 return false;
581 }
582 }
583
584 /**
585 * 将list放入缓存
586 *
587 * @param key
588 * 键
589 * @param value
590 * 值
591 * @param time
592 * 时间(秒)
593 * @return
594 */
595 public boolean lSet(String key, List<Object> value) {
596 try {
597 redisTemplate.opsForList().rightPushAll(key, value);
598 return true;
599 } catch (Exception e) {
600 e.printStackTrace();
601 return false;
602 }
603 }
604
605 /**
606 * 将list放入缓存
607 *
608 * @param key
609 * 键
610 * @param value
611 * 值
612 * @param time
613 * 时间(秒)
614 * @return
615 */
616 public boolean lSet(String key, List<Object> value, long time) {
617 try {
618 redisTemplate.opsForList().rightPushAll(key, value);
619 if (time > 0)
620 expire(key, time);
621 return true;
622 } catch (Exception e) {
623 e.printStackTrace();
624 return false;
625 }
626 }
627
628 /**
629 * 根据索引修改list中的某条数据
630 *
631 * @param key
632 * 键
633 * @param index
634 * 索引
635 * @param value
636 * 值
637 * @return
638 */
639 public boolean lUpdateIndex(String key, long index, Object value) {
640 try {
641 redisTemplate.opsForList().set(key, index, value);
642 return true;
643 } catch (Exception e) {
644 e.printStackTrace();
645 return false;
646 }
647 }
648
649 /**
650 * 移除N个值为value
651 *
652 * @param key
653 * 键
654 * @param count
655 * 移除多少个
656 * @param value
657 * 值
658 * @return 移除的个数
659 */
660 public long lRemove(String key, long count, Object value) {
661 try {
662 Long remove = redisTemplate.opsForList().remove(key, count, value);
663 return remove;
664 } catch (Exception e) {
665 e.printStackTrace();
666 return 0;
667 }
668 }
669
670 }
7、最后是Redis的测试类,方便自己进行测试使用。
1 package com.fline.aic.utils;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.Map.Entry;
6 import java.util.Set;
7
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.springframework.context.ApplicationContext;
11 import org.springframework.context.support.ClassPathXmlApplicationContext;
12
13 /**
14 *
15 * @Description TODO
16 * @author biehl
17 * @Date 2018年12月7日 下午8:25:34
18 *
19 * 参考:https://www.cnblogs.com/qlqwjy/p/8562703.html
20 */
21 public class RedisTest {
22
23 private RedisUtil redisUtils = null;
24
25 @Before
26 public void before() {
27 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-redis.xml");
28 redisUtils = (RedisUtil) context.getBean("redisUtil");
29 }
30
31 // redis存放String类型的
32 @Test
33 public void redisString() throws InterruptedException {
34 // 从redis数据库中获取到dd的字符串
35 String str = (String) redisUtils.get("dd");
36 // 如果获取到的str字符串不为空
37 if (str != null) {
38 // 就删除这个str字符串
39 redisUtils.del(str);
40 }
41 // 将key为dd和value为dd设置到redis里面
42 redisUtils.set("dd", "dd");
43 // 打印该key为dd的字符串
44 System.out.println(redisUtils.get("dd"));
45 }
46
47 // redis存放List类型的
48 @Test
49 public void redisList() throws InterruptedException {
50 // 删除key为list1
51 redisUtils.del("list1");
52 redisUtils.lSet("list1", "list1");
53 redisUtils.lSet("list1", "list2");
54 redisUtils.lSet("list1", "list3");
55 redisUtils.rSet("list1", "list4");
56 List<Object> lGet = redisUtils.lGet("list1", 0, -1);
57 // redisTemplate.opsForList().range("myList", 0, -1);
58 for (Object s : lGet) {
59 System.out.println(s);
60 }
61 }
62
63 // redis存放Set类型的
64 @Test
65 public void redisSet() throws InterruptedException {
66 redisUtils.del("set1");
67 redisUtils.sSet("set1", "set1");
68 redisUtils.sSet("set1", "set2");
69 redisUtils.sSet("set1", "set3");
70 redisUtils.sSet("set1", "set4");
71 Set<Object> sGet = redisUtils.sGet("set1");
72 for (Object s : sGet) {
73 System.out.println(s);
74 }
75 }
76
77 // redis存放Hash类型的
78 @Test
79 public void redisHash() throws InterruptedException {
80 redisUtils.del("hash1");
81 redisUtils.hset("hash1", "beijing", "北京");
82 redisUtils.hset("hash1", "shanghai", "上海");
83 redisUtils.hset("hash1", "chenzhen", "深圳");
84 redisUtils.hset("hash1", "xinxiang", "新乡");
85 Map<Object, Object> hmget = redisUtils.hmget("hash1");
86 Set<Entry<Object, Object>> entrySet = hmget.entrySet();
87 for (Map.Entry<Object, Object> hash : entrySet) {
88 System.out.println(hash.getKey() + " : " + hash.getValue());
89 }
90 }
91
92 }
效果如下所示:
待续.....
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有