package com.memcached;
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
/**
* 创建 memcached连接池
* @author peidw
*
*/
public class MemcachedServer {
/**
* 客户端API:memcached client for ja
*/
public void mctest(){
String[] servers=new String[]{"192.168.75.132:11211"};
Integer[] weights={1};
// 从连接池获取一个连接实例
SockIOPool pool = 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 * 2 );
// 设置主线程睡眠时间 每隔30秒醒来 然后 开始维护 连接数大小
pool.setMaintSleep( 30 );
// 关闭nagle算法
// 设置 读取 超时3秒钟 set the read timeout to 3 secs
// 不设置连接超时
pool.setNagle( false );
pool.setSocketTO( 3000 );
pool.setSocketConnectTO( 0 );
pool.initialize();
MemCachedClient memCachedClient = new MemCachedClient();
for(int i = 0; i < 10; i++) {
/*将对象加入到memcached缓存*/
boolean success = memCachedClient.set("" + i, "Hello!");
/*从memcached缓存中按key值取对象*/
String result = (String) memCachedClient.get("" + i);
System.out.println(String.format("set( %d ): %s", i, success));
System.out.println(String.format("get( %d ): %s", i, result));
}
}
public void testSpyMem(){
try{
/*建立MemcachedClient 实例,并指定memcached服务的IP地址和端口号*/
MemcachedClient mc = new MemcachedClient(new InetSocketAddress("192.168.75.132", 11211));
Future<Boolean> b = null;
/*将key值,过期时间(秒)和要缓存的对象set到memcached中*/
b = mc.set("neea:testDaF:ksIdno", 900, "someObject");
System.out.println(mc.get("neea:testDaF:ksIdno")+"\n");
if(b.get().booleanValue()==true){
mc.shutdown();
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args) {
MemcachedServer ms=new MemcachedServer();
ms.mctest();
ms.testSpyMem();
}
}
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import net.spy.memcached.MemcachedClient;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
/**
* 创建 memcached连接池
* @author peidw
*
*/
public class MemcachedServer {
/**
* 客户端API:memcached client for ja
*/
public void mctest(){
String[] servers=new String[]{"192.168.75.132:11211"};
Integer[] weights={1};
// 从连接池获取一个连接实例
SockIOPool pool = 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 * 2 );
// 设置主线程睡眠时间 每隔30秒醒来 然后 开始维护 连接数大小
pool.setMaintSleep( 30 );
// 关闭nagle算法
// 设置 读取 超时3秒钟 set the read timeout to 3 secs
// 不设置连接超时
pool.setNagle( false );
pool.setSocketTO( 3000 );
pool.setSocketConnectTO( 0 );
pool.initialize();
MemCachedClient memCachedClient = new MemCachedClient();
for(int i = 0; i < 10; i++) {
/*将对象加入到memcached缓存*/
boolean success = memCachedClient.set("" + i, "Hello!");
/*从memcached缓存中按key值取对象*/
String result = (String) memCachedClient.get("" + i);
System.out.println(String.format("set( %d ): %s", i, success));
System.out.println(String.format("get( %d ): %s", i, result));
}
}
public void testSpyMem(){
try{
/*建立MemcachedClient 实例,并指定memcached服务的IP地址和端口号*/
MemcachedClient mc = new MemcachedClient(new InetSocketAddress("192.168.75.132", 11211));
Future<Boolean> b = null;
/*将key值,过期时间(秒)和要缓存的对象set到memcached中*/
b = mc.set("neea:testDaF:ksIdno", 900, "someObject");
System.out.println(mc.get("neea:testDaF:ksIdno")+"\n");
if(b.get().booleanValue()==true){
mc.shutdown();
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args) {
MemcachedServer ms=new MemcachedServer();
ms.mctest();
ms.testSpyMem();
}
}