memcache Java客户端调用小例子
2013-04-01 14:47
302 查看
需要安装memcache
执行依赖于memcached-2.6.6.jar, slf4j-api-1.6.1.jar, commons-pool-1.3.jar
package memcached;
import java.util.ArrayList;
import java.util.List;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
/**
* Created by IntelliJ IDEA. User: liuzhy Date: 2010-12-3 Time: 16:43:36
*/
public class MemCacheInvoke {
protected static MemCachedClient mcc = new MemCachedClient();
static {
// 设置缓存服务器列表,当使用分布式缓存的时,可以指定多个缓存服务器。这里应该设置为多个不同的服务,我这里将两个服务设置为一样的,大家不要向我学习,呵呵。
String[] servers = {
// "10.15.0.215:46697",
// "10.15.0.215:46697",
"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 a test String");
String foo = mcc.get("foo").toString();
System.out.println("===foo===" + foo);
List list = new ArrayList();
list.add(0, "first");
list.add(1, "second");
list.add(2, "third");
mcc.set("list", list);
List resutList = (ArrayList) mcc.get("list");
if(resutList!=null){
System.out.println("===resutList.size()===" + resutList.size());
if(resutList.size()>0){
for (int i = 0; i < resutList.size(); i++) {
System.out.println("===resutList==="+i+"===" + resutList.get(i));
}
}
}
}
}
执行依赖于memcached-2.6.6.jar, slf4j-api-1.6.1.jar, commons-pool-1.3.jar
package memcached;
import java.util.ArrayList;
import java.util.List;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
/**
* Created by IntelliJ IDEA. User: liuzhy Date: 2010-12-3 Time: 16:43:36
*/
public class MemCacheInvoke {
protected static MemCachedClient mcc = new MemCachedClient();
static {
// 设置缓存服务器列表,当使用分布式缓存的时,可以指定多个缓存服务器。这里应该设置为多个不同的服务,我这里将两个服务设置为一样的,大家不要向我学习,呵呵。
String[] servers = {
// "10.15.0.215:46697",
// "10.15.0.215:46697",
"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 a test String");
String foo = mcc.get("foo").toString();
System.out.println("===foo===" + foo);
List list = new ArrayList();
list.add(0, "first");
list.add(1, "second");
list.add(2, "third");
mcc.set("list", list);
List resutList = (ArrayList) mcc.get("list");
if(resutList!=null){
System.out.println("===resutList.size()===" + resutList.size());
if(resutList.size()>0){
for (int i = 0; i < resutList.size(); i++) {
System.out.println("===resutList==="+i+"===" + resutList.get(i));
}
}
}
}
}
相关文章推荐
- memcache Java客户端调用小例子
- android binder - 客户端(java层) 调用 服务端(c++层) 例子
- java调用基于SOAP的CXF 框架的WebService客户端和服务器段helloworld例子
- java客户端调用webservice的例子
- windows下安装memcache及java客户端调用
- JAVA调用Tuxedo服务——使用JNI封装Tuxedo客户端的一个例子
- android binder - 客户端(java层) 调用 服务端(c++层) 例子
- JAVA调用Tuxedo服务——使用JNI封装Tuxedo客户端的一个例子
- 程序调用飞信API发送免费短信(JAVA例子,其他语言一样用)
- Linux平台Java调用so库-JNI使用例子
- netty学习九:(window7上)python客户端通过thrift调用java服务端
- 一个 Java 的 Socket 服务器和客户端通信的例子
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍
- Java 的 Socket 服务器和客户端通信的例子
- 关于Webservice接口的Java客户端调用
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式等)介绍
- memcache原生,php客户端,java客户端的性能对比
- Oracle调用java程序例子
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)
- ActiveMQ完整的java客户端例子(源码)