java 连接memcached

1.下载memcached的windows版本,地址在http://code.jellycan.com/memcached/

2.解压到任意目录,然后用管理员身份cd到你的解压目录

3.memcached.exe-dinstall

4.memcached.exe-dstart

memcached安装完成。

https://github.com/gwhalin/Memcached-Java-Client/downloads

java_memcached-release_2.5.0.zip为java的连接客户端

1.解压,将里面的jar包拷贝到项目的lib目录下

使用以下代码调用

Java代码

packageinfo.frady;

importcom.danga.MemCached.MemCachedClient;

importcom.danga.MemCached.SockIOPool;

/**

*Oct15,2011

*/

publicclassMemCachedTest{

privatestaticMemCachedClientmcc=newMemCachedClient();

static{

String[]servers={"127.0.0.1:11211"};

//创建一个连接池

SockIOPoolpool=SockIOPool.getInstance();

//设置缓存服务器

pool.setServers(servers);

//设置初始化连接数,最小连接数,最大连接数以及最大处理时间

pool.setInitConn(50);

pool.setMinConn(50);

pool.setMaxConn(500);

pool.setMaxIdle(1000*60*60);

//设置主线程睡眠时间,每3秒苏醒一次,维持连接池大小

//maintSleep千万不要设置成30,访问量一大就出问题,单位是毫秒,推荐30000毫秒。

pool.setMaintSleep(3000);

//关闭套接字缓存

pool.setNagle(false);

//连接建立后的超时时间

pool.setSocketTO(3000);

//连接建立时的超时时间

pool.setSocketConnectTO(0);

//初始化连接池

pool.initialize();

}

protectedMemCachedTest(){

}

publicstaticMemCachedClientgetInstance(){

returnmcc;

}

publicstaticvoidmain(String[]args){

MemCachedClientcache=MemCachedTest.getInstance();

cache.add("frady","thisisatest");

System.out.println(cache.get("frady"));

}

}

以下是对象的封装

Java代码

packageinfo.frady;

importcom.danga.MemCached.MemCachedClient;

importcom.danga.MemCached.SockIOPool;

/**

*Oct15,2011

*/

publicclassMemTest{

//创建一个memcached客户端对象

protectedstaticMemCachedClientmcc=newMemCachedClient();

//创建memcached连接池

static

{//指定memcached服务地址

String[]servers=

{"127.0.0.1:11211","127.0.0.1:11211","127.0.0.1:11211"};

//指定memcached服务器负载量

Integer[]weights={3,3,2};

//从连接池获取一个连接实例

SockIOPoolpool=SockIOPool.getInstance();

//设置服务器和服务器负载量

pool.setServers(servers);

pool.setWeights(weights);

//设置一些基本的参数

//设置初始连接数5最小连接数5最大连接数250

//设置一个连接最大空闲时间6小时

pool.setInitConn(5);

pool.setMinConn(5);

pool.setMaxConn(250);

pool.setMaxIdle(1000*60*60*6);

//设置主线程睡眠时间

//每隔30秒醒来然后

//开始维护连接数大小

pool.setMaintSleep(30);

//设置tcp相关的树形

//关闭nagle算法

//设置读取超时3秒钟setthereadtimeoutto3secs

//不设置连接超时

pool.setNagle(false);

pool.setSocketTO(3000);

pool.setSocketConnectTO(0);

//开始初始化连接池

pool.initialize();

//设置压缩模式

//如果超过64k压缩数据

mcc.setCompressEnable(true);

mcc.setCompressThreshold(64*1024);

}

publicstaticvoidmain(Stringargs[]){

Useru1=newUser();

u1.setUserName("frady");

u1.setEmail("[email protected]");

mcc.add("fradyt",u1);

Useru2=(User)mcc.get("fradyt");

System.out.println("email="+u2.getEmail());

u2.setEmail("[email protected]");

mcc.replace("fradyt",u2);

u2=(User)mcc.get("fradyt");

System.out.println("email="+u2.getEmail());

}

}

packageinfo.frady;

importcom.danga.MemCached.MemCachedClient;

importcom.danga.MemCached.SockIOPool;

/**

*Oct15,2011

*/

publicclassMemTest{

//创建一个memcached客户端对象

protectedstaticMemCachedClientmcc=newMemCachedClient();

//创建memcached连接池

static

{//指定memcached服务地址

String[]servers=

{"127.0.0.1:11211","127.0.0.1:11211","127.0.0.1:11211"};

//指定memcached服务器负载量

Integer[]weights={3,3,2};

//从连接池获取一个连接实例

SockIOPoolpool=SockIOPool.getInstance();

//设置服务器和服务器负载量

pool.setServers(servers);

pool.setWeights(weights);

//设置一些基本的参数

//设置初始连接数5最小连接数5最大连接数250

//设置一个连接最大空闲时间6小时

pool.setInitConn(5);

pool.setMinConn(5);

pool.setMaxConn(250);

pool.setMaxIdle(1000*60*60*6);

//设置主线程睡眠时间

//每隔30秒醒来然后

//开始维护连接数大小

pool.setMaintSleep(30);

//设置tcp相关的树形

//关闭nagle算法

//设置读取超时3秒钟setthereadtimeoutto3secs

//不设置连接超时

pool.setNagle(false);

pool.setSocketTO(3000);

pool.setSocketConnectTO(0);

//开始初始化连接池

pool.initialize();

//设置压缩模式

//如果超过64k压缩数据

mcc.setCompressEnable(true);

mcc.setCompressThreshold(64*1024);

}

publicstaticvoidmain(Stringargs[]){

Useru1=newUser();

u1.setUserName("frady");

u1.setEmail("[email protected]");

mcc.add("fradyt",u1);

Useru2=(User)mcc.get("fradyt");

System.out.println("email="+u2.getEmail());

u2.setEmail("[email protected]");

mcc.replace("fradyt",u2);

u2=(User)mcc.get("fradyt");

System.out.println("email="+u2.getEmail());

}

}

此处要注意User得实现序列化

Java代码

packageinfo.frady;

importjava.io.Serializable;

/**

*Oct15,2011

*/

publicclassUserimplementsSerializable{

/**

*

*/

privatestaticfinallongserialVersionUID=1003272426055966138L;

privateStringuserName;

privateStringemail;

publicStringgetUserName(){

returnuserName;

}

publicvoidsetUserName(StringuserName){

this.userName=userName;

}

publicStringgetEmail(){

returnemail;

}

publicvoidsetEmail(Stringemail){

this.email=email;

}

}

packageinfo.frady;

importjava.io.Serializable;

/**

*Oct15,2011

*/

publicclassUserimplementsSerializable{

/**

*

*/

privatestaticfinallongserialVersionUID=1003272426055966138L;

privateStringuserName;

privateStringemail;

publicStringgetUserName(){

returnuserName;

}

publicvoidsetUserName(StringuserName){

this.userName=userName;

}

publicStringgetEmail(){

returnemail;

}

publicvoidsetEmail(Stringemail){

this.email=email;

}

}

查看cache的状态

Java代码

Map<String,Map<String,String>>stats=cache.stats();

for(Map.Entry<String,Map<String,String>>m:stats.entrySet()){

System.out.println(m.getKey());

Map<String,String>values=m.getValue();

for(Map.Entry<String,String>v:values.entrySet()){

System.out.print(v.getKey()+":");

System.out.println(v.getValue());

}

}

Map<String,Map<String,String>>stats=cache.stats();

for(Map.Entry<String,Map<String,String>>m:stats.entrySet()){

System.out.println(m.getKey());

Map<String,String>values=m.getValue();

for(Map.Entry<String,String>v:values.entrySet()){

System.out.print(v.getKey()+":");

System.out.println(v.getValue());

}

}

清空所有的对象

Java代码

MemCachedClientmc=newMemCachedClient();

mc.flushAll();

相关推荐