首先安装redis:Mac下安装Redis

java客户端选用Jedis,加入pom文件依赖

pom.xml
1
2
3
4
5
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>

下面是一个简化的Template Demo,用来实现redis各种命令的映射:
com.climbran.redis.JedisTemplate
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.climbran.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPool;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.exceptions.JedisException;

/**
* JedisTemplate 提供了一个template方法,负责对Jedis连接的获取与归还。
* JedisAction<T> 和 JedisActionNoResult两种回调接口,适用于有无返回值两种情况。
*/

public class JedisTemplate {

private static Logger logger = LoggerFactory.getLogger(JedisTemplate.class);

private JedisPool jedisPool;

public JedisTemplate(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}

/**
* Callback interface for template.
*/

public interface JedisAction<T> {
T action(Jedis jedis);
}

/**
* Callback interface for template without result.
*/

public interface JedisActionNoResult {
void action(Jedis jedis);
}

/**
* Execute with a call back action with result.
*/

public <T> T execute(JedisAction<T> jedisAction) throws JedisException {
Jedis jedis = null;
boolean broken = false;
try {
jedis = jedisPool.getResource();
return jedisAction.action(jedis);
} catch (JedisException e) {
broken = handleJedisException(e);
throw e;
} finally {
closeResource(jedis, broken);
}
}

/**
* Execute with a call back action without result.
*/

public void execute(JedisActionNoResult jedisAction) throws JedisException {
Jedis jedis = null;
boolean broken = false;
try {
jedis = jedisPool.getResource();
jedisAction.action(jedis);
} catch (JedisException e) {
broken = handleJedisException(e);
throw e;
} finally {
closeResource(jedis, broken);
}
}


/**
* Handle jedisException, write log and return whether the connection is broken.
*/

protected boolean handleJedisException(JedisException jedisException) {
if (jedisException instanceof JedisConnectionException) {
logger.error("Redis connection " + jedisPool + " lost.", jedisException);
} else if (jedisException instanceof JedisDataException) {
if ((jedisException.getMessage() != null) && (jedisException.getMessage().indexOf("READONLY") != -1)) {
logger.error("Redis connection " + jedisPool + " are read-only slave.", jedisException);
} else {
// dataException, isBroken=false
return false;
}
} else {
logger.error("Jedis exception happen.", jedisException);
}
return true;
}

/**
* Return jedis connection to the pool, call different return methods depends on the conectionBroken status.
*/

protected void closeResource(Jedis jedis, boolean conectionBroken) {
try {
if (conectionBroken) {
jedisPool.returnBrokenResource(jedis);
} else {
jedisPool.returnResource(jedis);
}
} catch (Exception e) {
logger.error("return back jedis failed, will fore close the jedis.", e);
JedisUtils.destroyJedis(jedis);
}

}

// / Common Actions ///
public Boolean del(final String... keys) {
return execute(new JedisAction<Boolean>() {

@Override
public Boolean action(Jedis jedis) {
return jedis.del(keys) == keys.length ? true : false;
}
});
}

public void flushDB() {
execute(new JedisActionNoResult() {

@Override
public void action(Jedis jedis) {
jedis.flushDB();
}
});
}

// / String Actions ///
public String get(final String key) {
return execute(new JedisAction<String>() {

@Override
public String action(Jedis jedis) {
return jedis.get(key);
}
});
}

public void set(final String key, final String value) {
execute(new JedisActionNoResult() {

@Override
public void action(Jedis jedis) {
jedis.set(key, value);
}
});
}

//Other Actions

......
}

com.climbran.redis.JedisUtils
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
package com.climbran.redis;

import com.climbran.redis.JedisTemplate.JedisAction;
//import com.climbran.redis.pool.JedisPool;
import redis.clients.jedis.JedisPool;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisException;

public class JedisUtils {

private static final String OK_CODE = "OK";
private static final String OK_MULTI_CODE = "+OK";

/**
* 判断 返回值是否ok.
*/

public static boolean isStatusOk(String status) {
return (status != null) && (OK_CODE.equals(status) || OK_MULTI_CODE.equals(status));
}

/**
* 在Pool以外强行销毁Jedis.
*/

public static void destroyJedis(Jedis jedis) {
if ((jedis != null) && jedis.isConnected()) {
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
} catch (Exception e) {
}
}
}
}

启动redis服务,执行main函数:

com.climbran.redis.JedisUtils
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import com.climbran.redis.JedisTemplate;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisTest {

public static void main(String[] args){
JedisPool pool = new JedisPool(new JedisPoolConfig(),"localhost",6379);
JedisTemplate template = new JedisTemplate(pool);
template.set("foo","bar");
System.out.println(template.get("foo"));
}
}

如果将JedisTemplate注入dao层调用方法则可对entity层与redis进行映射。

文章目录