Java spymemcached 简单使用
下载并安装memcached 百度一大把
添加依赖 sbt 或mvn
sbt
1
libraryDependencies += "net.spy" % "spymemcached" % "2.12
maven
1
2
3
4
5<dependency>
<groupId>net.spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.12.3</version>
</dependency>上代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class MemcachedUtil {
// 日志对象
private static final play.Logger.ALogger logger = play.Logger.of(MemcachedUtil.class);
static final String servers = "127.0.0.1:11211";
public static MemcachedClient mc = null;
public static MemcachedClient getMemcachedClient(){
if (mc != null) {
return mc;
}
try{
mc = new MemcachedClient(new ConnectionFactoryBuilder()
.setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).build(), AddrUtil.getAddresses(servers));
}catch(Exception ex){
logger.error(ex.getMessage());
}
return mc;
}设值
1
2MemcachedUtil.getMemcachedClient().set(key, 秒, value);
MemcachedUtil.getMemcachedClient().get(key)
Java spymemcached 简单使用
http://yoursite.com/post/7af52029.html/