SpringBoot已经实现了很多实用的缓存组件;但是由于工作中习惯了试用JedisUtils工具类来进行缓存操作, 这里记录一下SpringBoot中对于JedisUtils的封装

操作

新建一个SpringBoot项目

修改pom.xml增加:

1
2
3
4
5
6
7
8
9
10
11

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>

新建JedisConfiguration.java用于配置JedisPool Bean

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
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableCaching
public class JedisConfiguration {

@Value("${spring.redis.host}")
private String host;

@Value("${spring.redis.port}")
private int port;

@Value("${spring.redis.timeout}")
private String timeouts;

@Value("${spring.redis.jedis.pool.min-idle}")
private int maxIdle;

@Value("${spring.redis.jedis.pool.max-wait}")
private String maxWaitMilliss;

@Value("${spring.redis.password}")
private String password;

@Bean(name = "jedisPool")
public JedisPool redisPoolFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
long maxWaitMillis = Long.parseLong(maxWaitMilliss.replace("ms", ""));
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
int timeout = Integer.parseInt(timeouts.replace("ms", ""));
return new JedisPool(jedisPoolConfig, host, port, timeout);
}

}

创建JedisUtils.java

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
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

public class JedisUtils implements ApplicationContextAware {

private static ApplicationContext applicationContext = null;

private static JedisPool jedisPool = null;

private static volatile Jedis jedis = null;

private static Logger logger = Logger.getLogger("JedisUtils");

public JedisUtils() {
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (JedisUtils.applicationContext == null) {
//初始化 spring applicationContext
JedisUtils.applicationContext = applicationContext;
}
}

public static Jedis getJedis() {
if (jedis == null) {
synchronized (Jedis.class) {
if (jedis == null) {
jedis = getJedisPool().getResource();
}
}
}
return jedis;
}

public static JedisPool getJedisPool() {
if (jedisPool == null) {
synchronized (JedisPool.class) {
if (jedisPool == null) {
jedisPool = applicationContext.getBean("jedisPool", JedisPool.class);
}
}
}
return jedisPool;
}

/**
* 根据key查看是否存在
*
* @param key
* @return
*/
public static boolean hasKey(String key) {
return getJedis().exists(key);
}

/**
* 设置key -value 形式数据
*
* @param key
* @param value
* @return
*/
public static boolean set(String key, String value) {
boolean result = false;
Jedis jedis = null;
try {
jedis = getJedis();
result = "OK".equals(jedis.set(key, value));
} catch (Exception e) {
closeBrokenJedis(jedis);
logger.error("JedisCache.set falid", e);
}
return result;
}

/**
* 设置 一个过期时间
*
* @param key key
* @param value value
* @param timeOut 单位秒
* @return
*/
public static boolean set(String key, String value, int timeOut) {
boolean result = false;
Jedis jedis = null;
try {
jedis = getJedis();
result = "OK".equals(jedis.setex(key, timeOut, value));
} catch (Exception e) {
closeBrokenJedis(jedis);
logger.error("JedisCache.set falid", e);
}
return result;
}

/**
* 根据key获取value
*
* @param key
* @return
*/
public static String get(String key) {
String result = null;
Jedis jedis = null;
try {
jedis = getJedis();
result = jedis.get(key);
closeJedis(jedis);
} catch (Exception e) {
closeBrokenJedis(jedis);
logger.error("JedisCache.get falid", e);
}
return result;
}

/**
* 根据key删除
*
* @param key
*/
public static void del(String key) {
getJedis().del(key);
}
}

其中的getJedisPool()可以从bean获取JedisPool对象, 用于后续的操作, 上面给出了部分代码, 完整版在这.

在springboot的主类上添加注解

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableCaching
@Import({JedisUtils.class, JedisPool.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

结束

至此, 就可以愉快的使用JedisUtils直接操作Redis缓存了.