Skip to content

Commit df95fb7

Browse files
committed
fixed:添加 SpringBoot 集成 memcached
1 parent 4ff46d5 commit df95fb7

File tree

10 files changed

+471
-2
lines changed

10 files changed

+471
-2
lines changed

SpringBoot-Memcached/.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.gradle
2+
/build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
/out/
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/

SpringBoot-Memcached/build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dependencies {
2+
compile project(':SpringBoot-Utils')
3+
compile group: 'net.spy', name: 'spymemcached', version: '2.12.3'
4+
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.4.RELEASE'
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.dashuai.learning.memcached;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Memcached application
8+
* <p/>
9+
* Created in 2018.11.15
10+
* <p/>
11+
*
12+
* @author Liaozihong
13+
*/
14+
@SpringBootApplication
15+
public class MemcachedApplication {
16+
17+
/**
18+
* The entry point of application.
19+
*
20+
* @param args the input arguments
21+
*/
22+
public static void main(String[] args) {
23+
SpringApplication.run(MemcachedApplication.class, args);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.dashuai.learning.memcached.config;
2+
3+
import com.dashuai.learning.memcached.manager.OpeartionMemcachedManager;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
/**
8+
* Memcached configuration
9+
* <p/>
10+
* Created in 2018.11.15
11+
* <p/>
12+
*
13+
* @author Liaozihong
14+
*/
15+
@Configuration
16+
public class MemcachedConfiguration {
17+
/**
18+
* Opeartion memcached manager opeartion memcached manager.
19+
*
20+
* @return the opeartion memcached manager
21+
*/
22+
@Bean(initMethod = "init", destroyMethod = "disConnect")
23+
public OpeartionMemcachedManager opeartionMemcachedManager() {
24+
return new OpeartionMemcachedManager();
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
package com.dashuai.learning.memcached.manager;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import net.spy.memcached.MemcachedClient;
5+
import net.spy.memcached.internal.OperationFuture;
6+
import org.springframework.beans.factory.annotation.Value;
7+
8+
import java.io.IOException;
9+
import java.net.InetSocketAddress;
10+
import java.util.Collection;
11+
import java.util.Map;
12+
import java.util.concurrent.Future;
13+
import java.util.concurrent.TimeUnit;
14+
15+
/**
16+
* Opeartion memcached manager
17+
* <p/>
18+
* Created in 2018.11.15
19+
* <p/>
20+
*
21+
* @author Liaozihong
22+
*/
23+
@Slf4j
24+
public class OpeartionMemcachedManager {
25+
/**
26+
* The Memcached ip.
27+
*/
28+
@Value("${memcached.ip}")
29+
String memcachedIp;
30+
/**
31+
* The Memcached port.
32+
*/
33+
@Value("${memcached.port}")
34+
Integer memcachedPort;
35+
36+
/**
37+
* The constant DEFAULT_TIMEOUT.
38+
*/
39+
public final static int DEFAULT_TIMEOUT = 5;
40+
/**
41+
* The constant timeUnitSeconds.
42+
*/
43+
public final static TimeUnit timeUnitSeconds = TimeUnit.SECONDS;
44+
45+
private MemcachedClient memcachedClient;
46+
47+
/**
48+
* 初始化
49+
*/
50+
public void init() {
51+
try {
52+
//只采用单机模式,如果需要配置集群模式可用AddrUtil.getAddresses(servers),
53+
//可参考:https://blog.csdn.net/gtuu0123/article/details/4849905
54+
memcachedClient = new MemcachedClient(new InetSocketAddress(memcachedIp, memcachedPort));
55+
log.info("++++++++++++++++++++ Memcached 连接成功,Address:{}:{} ++++++++++++++++++++++", memcachedIp, memcachedPort);
56+
} catch (IOException e) {
57+
log.info("++++++++++++++++++++ Memcached 连接异常,Address:{}:{} ++++++++++++++++++++++{}", memcachedIp, memcachedPort, e);
58+
}
59+
}
60+
61+
/**
62+
* 设置键值
63+
*
64+
* @param key 键
65+
* @param expire 有效时间
66+
* @param value 值
67+
* @return boolean boolean
68+
*/
69+
public Boolean set(String key, int expire, Object value) {
70+
OperationFuture<Boolean> result = memcachedClient.set(key, expire, value);
71+
return getResult(result);
72+
}
73+
74+
/**
75+
* 根据键获取值
76+
*
77+
* @param key the key
78+
* @return the object
79+
*/
80+
public Object get(String key) {
81+
return memcachedClient.get(key);
82+
}
83+
84+
/**
85+
* 以异步的方式获取值
86+
*
87+
* @param key the key
88+
* @return the object
89+
*/
90+
public Object ascynGet(String key) {
91+
Future<Object> objectFuture = memcachedClient.asyncGet(key);
92+
return getResult(objectFuture);
93+
}
94+
95+
/**
96+
* 将对象添加到缓存
97+
*
98+
* @param key the key
99+
* @param value the value
100+
* @param expire the expire
101+
* @return the boolean
102+
*/
103+
public Boolean add(String key, Object value, int expire) {
104+
Future<Boolean> f = memcachedClient.add(key, expire, value);
105+
return getResult(f);
106+
}
107+
108+
/**
109+
* 替换某个键值
110+
*
111+
* @param key the 键
112+
* @param value the 值
113+
* @param expire the 过期时间
114+
* @return the boolean
115+
*/
116+
public Boolean replace(String key, Object value, int expire) {
117+
Future<Boolean> f = memcachedClient.replace(key, expire, value);
118+
return getResult(f);
119+
}
120+
121+
/**
122+
* 删除某个特定键
123+
*
124+
* @param key the key
125+
* @return the boolean
126+
*/
127+
public Boolean delete(String key) {
128+
Future<Boolean> f = memcachedClient.delete(key);
129+
return getResult(f);
130+
}
131+
132+
/**
133+
* 立即从所有服务器清除所有缓存,慎用。
134+
*
135+
* @return the boolean
136+
*/
137+
@Deprecated
138+
public Boolean flush() {
139+
Future<Boolean> f = memcachedClient.flush();
140+
return getResult(f);
141+
}
142+
143+
/**
144+
* 从缓存中获取多个键值。
145+
*
146+
* @param keys the 键集合
147+
* @return the multi
148+
*/
149+
public Map<String, Object> getMulti(Collection<String> keys) {
150+
return memcachedClient.getBulk(keys);
151+
}
152+
153+
/**
154+
* 从缓存中获取多个键值
155+
*
156+
* @param keys the 键数组
157+
* @return the multi
158+
*/
159+
public Map<String, Object> getMulti(String[] keys) {
160+
return memcachedClient.getBulk(keys);
161+
}
162+
163+
/**
164+
* 异步地从缓存中获取一组对象并使用它们进行解码
165+
*
166+
* @param keys the 键集合
167+
* @return the map
168+
*/
169+
public Map<String, Object> asyncGetMulti(Collection<String> keys) {
170+
Map<String, Object> map = null;
171+
Future<Map<String, Object>> f = memcachedClient.asyncGetBulk(keys);
172+
try {
173+
map = getResult(f);
174+
} catch (Exception e) {
175+
f.cancel(false);
176+
}
177+
return map;
178+
}
179+
180+
/**
181+
* 增加给定的计数器,返回新值。
182+
*
183+
* @param key the key
184+
* @param by the 增值
185+
* @param defaultValue the 默认值(如计时器不存在),如该key没值,则取默认值
186+
* @param expire the 过期时间
187+
* @return the long
188+
*/
189+
public long increment(String key, int by, long defaultValue, int expire) {
190+
return memcachedClient.incr(key, by, defaultValue, expire);
191+
}
192+
193+
/**
194+
* 以给定的数量增加给定的键。
195+
*
196+
* @param key the key
197+
* @param by the 增值
198+
* @return the long
199+
*/
200+
public long increment(String key, int by) {
201+
return memcachedClient.incr(key, by);
202+
}
203+
204+
/**
205+
* 减量.
206+
*
207+
* @param key the key
208+
* @param by the 减量
209+
* @param defaultValue the 默认值(如果计数器不存在)
210+
* @param expire the 过期时间
211+
* @return the long
212+
*/
213+
public long decrement(String key, int by, long defaultValue, int expire) {
214+
return memcachedClient.decr(key, by, defaultValue, expire);
215+
}
216+
217+
/**
218+
* 减量
219+
*
220+
* @param key the key
221+
* @param by the 要减的值
222+
* @return the long
223+
*/
224+
public long decrement(String key, int by) {
225+
return memcachedClient.decr(key, by);
226+
}
227+
228+
/**
229+
* 异步增量,并返回当前值.
230+
*
231+
* @param key the key
232+
* @param by the 要增加的值
233+
* @return the long
234+
*/
235+
public Long asyncIncrement(String key, int by) {
236+
Future<Long> f = memcachedClient.asyncIncr(key, by);
237+
return getResult(f);
238+
}
239+
240+
/**
241+
* Async decrement long.
242+
* 异步减量,并返回当前值
243+
*
244+
* @param key the key
245+
* @param by the 要减少的值
246+
* @return the long
247+
*/
248+
public Long asyncDecrement(String key, int by) {
249+
Future<Long> f = memcachedClient.asyncDecr(key, by);
250+
return getResult(f);
251+
}
252+
253+
/**
254+
* Gets result.
255+
* 获取返回结果
256+
*
257+
* @param <T> the type parameter
258+
* @param future the future
259+
* @return the result
260+
*/
261+
public <T> T getResult(Future<T> future) {
262+
try {
263+
return future.get(DEFAULT_TIMEOUT,
264+
timeUnitSeconds);
265+
} catch (Exception e) {
266+
log.warn("获取返回结果失败!{}", e);
267+
}
268+
return null;
269+
}
270+
271+
/**
272+
* 关闭连接
273+
*/
274+
public void disConnect() {
275+
if (memcachedClient == null) {
276+
return;
277+
}
278+
memcachedClient.shutdown();
279+
}
280+
281+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
memcached.ip=127.0.0.1
2+
memcached.port=11211

0 commit comments

Comments
 (0)