memcached循序渐进(二) - Java下访问memcached简单代码
日期: 2019-06-27 分类: 个人收藏 377次阅读
1. 下载客户端jar
https://github.com/gwhalin/Memcached-Java-Client
2. 在Eclipse下新建一个小项目
代码如下:
package memcached.java.client;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemCacheInvoke {
protected static MemCachedClient mcc = new MemCachedClient();
static{
/*
* 设置缓存服务器列表,当使用分布式缓存的时
* 可以指定多个缓存服务器,这里将两个服务设置为一样
*/
String[] servers =
{
"127.0.0.1:11211",
"127.0.0.1:11211"
// "server3.mydomain.com:1624"
};
// 设置服务器权重
Integer[] weights = {3, 2};
// 创建一个Socked连接池实例
SockIOPool pool = SockIOPool.getInstance();
// 向连接池设置服务器和权重
pool.setServers(servers);
pool.setWeights(weights);
// set some TCP settings
// disable nagle
// set the read timeout to 3 secs
// and don't set a connect timeout
pool.setNagle( false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// initialize the connection pool
pool.initialize();
}
public static void main(String[] args) {
mcc.set("foo", "This is in memcached");
String bar = mcc.get("foo").toString();
System.out.println(">>> " + bar);
}
}
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemCacheInvoke {
protected static MemCachedClient mcc = new MemCachedClient();
static{
/*
* 设置缓存服务器列表,当使用分布式缓存的时
* 可以指定多个缓存服务器,这里将两个服务设置为一样
*/
String[] servers =
{
"127.0.0.1:11211",
"127.0.0.1:11211"
// "server3.mydomain.com:1624"
};
// 设置服务器权重
Integer[] weights = {3, 2};
// 创建一个Socked连接池实例
SockIOPool pool = SockIOPool.getInstance();
// 向连接池设置服务器和权重
pool.setServers(servers);
pool.setWeights(weights);
// set some TCP settings
// disable nagle
// set the read timeout to 3 secs
// and don't set a connect timeout
pool.setNagle( false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// initialize the connection pool
pool.initialize();
}
public static void main(String[] args) {
mcc.set("foo", "This is in memcached");
String bar = mcc.get("foo").toString();
System.out.println(">>> " + bar);
}
}
3. 运行结果
>>> This is in memcached
转载于:https://www.cnblogs.com/davidgu/archive/2012/06/14/2549041.html
相关资源:韩顺平全套PHP+Java
除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog
精华推荐