1、关于spring配置文件中对于redis的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!-- redis配置 -->
<bean id= "jedispoolconfig" class = "redis.clients.jedis.jedispoolconfig" >
<!-- <property name= "maxactive" value= "90" /> -->
<property name= "maxidle" value= "5" />
<!-- <property name= "maxwait" value= "1000" /> -->
<property name= "testonborrow" value= "true" />
</bean>
<!--配置redis数据源-->
<bean id= "jedispool" class = "redis.clients.jedis.jedispool" destroy-method= "destroy" >
<constructor-arg ref= "jedispoolconfig" />
<constructor-arg value= "192.168.21.195" />
<constructor-arg value= "6379" />
</bean>
<!--配置自定义的redisapi工具类-->
<bean id= "redisapi" class = "org.slsale.common.redisapi" >
<property name= "jedispool" ref= "jedispool" />
</bean>
|
2、配置自定义的redisapi,对redis数据库的管理
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
|
package org.slsale.common;
import redis.clients.jedis.jedis;
import redis.clients.jedis.jedispool;
/**
* jedisapi
* @author luzhewu
*
*/
public class redisapi {
public jedispool jedispool; // redis连接池对象
public jedispool getjedispool() {
return jedispool;
}
public void setjedispool(jedispool jedispool) {
this .jedispool = jedispool;
}
/**
* set key and value tp redis
* @param key
* @param value
* @return
*/
public boolean set(string key, string value) {
jedis jedis = null ;
try {
jedis = jedispool.getresource(); // 获取jedis对象
jedis.set(key, value);
return true ;
} catch (exception e) {
e.printstacktrace();
} finally {
// 返还到连接池
returnresource(jedispool, jedis);
}
return false ;
}
/**
* 判断某个key是否存在
* @param key
* @return
*/
public boolean exist(string key) {
jedis jedis = null ;
try {
jedis = jedispool.getresource();
return jedis.exists(key);
} catch (exception e) {
e.printstacktrace();
} finally {
// 返还到连接池
returnresource(jedispool, jedis);
}
return false ;
}
/**
* 通过key获取value
* @param key
* @return
*/
public string get(string key) {
string value = null ;
jedis jedis = null ;
try {
jedis = jedispool.getresource();
value = jedis.get(key);
} catch (exception e) {
e.printstacktrace();
} finally {
// 返还到连接池
returnresource(jedispool, jedis);
}
return value;
}
/**
* 返还到连接池
* @param jedispool
* @param jedis
*/
public static void returnresource(jedispool jedispool, jedis jedis) {
if (jedis != null ) {
jedispool.returnresource(jedis);
}
}
}
|
3、redis相关依赖
1
2
3
4
5
|
<!-- redis相关依赖jedis -->
<dependency>
<groupid>redis.clients</groupid>
<artifactid>jedis</artifactid>
<version> 2.6 . 1 </version>
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/wiseroll/p/7258863.html