假设我们有3台memcached 服务器,server1 和server2 有3GB 的内存空间,server3 有2GB 的内存空间.下面程序说明怎么来创建客户端. import com.danga.MemCached.*;public class MyClass {// 创建一个 memcached 客户端对象protected static MemCachedClient mcc = new MemCachedClient();// 创建 memcached连接池static{ // 指定memcached服务地址String[] servers ={ "server1.mydomain.com:1121","server2.mydomain.com:1121", "server3.mydomain.com:1121" };// 指定memcached服务器负载量 Integer[] weights ={ 3, 3, 2 };// 从连接池获取一个连接实例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 * 6 );// 设置主线程睡眠时间// 每隔30秒醒来 然后// 开始维护 连接数大小pool.setMaintSleep( 30 );// 设置tcp 相关的树形// 关闭nagle算法// 设置 读取 超时3秒钟 set the read timeout to 3 secs// 不设置连接超时pool.setNagle( false );pool.setSocketTO( 3000 );pool.setSocketConnectTO( 0 );// 开始初始化 连接池pool.initialize();// 设置压缩模式//如果超过64k压缩数据mcc.setCompressEnable( true );mcc.setCompressThreshold( 64 * 1024 );}public static void examples() {mcc.set( "foo", "This is a test String" );String bar = mcc.get( "foo" );}}
MemCachedClient 类 常用的方法说明
创建 client对象 设置参数:
MemCachedClient mc = new MemCachedClient();
//压缩模式
mc.setCompressEnable(true);
// 如果 cache数据 大于4 KB 就启用压缩
mc.setCompressThreshold(4096);
// 基本类型tostring方法
// 通常不需要设置
mc.setPrimitiveAsString(true);
存储一个对象:
MemCachedClient mc = new MemCachedClient();String key = "cacheKey1";Object value = SomeClass.getObject();mc.set(key, value);
用客户端hashcode 存储一个对象:
MemCachedClient mc = new MemCachedClient();String key = "cacheKey1";Object value = SomeClass.getObject();Integer hash = new Integer(45);mc.set(key, value, hash);set方法:在cache中存储一个指定对象 add 和replace 方法功能差不多 add -- 如果不存在 这个key的对象,将会存储一个对象到cache中 replace --只有当存在指定key对象的时候 会覆盖已有对象
删除一个对象:
MemCachedClient mc = new MemCachedClient();String key = "cacheKey1";mc.delete(key);
结合hashcode 删除一个对象:
MemCachedClient mc = new MemCachedClient();String key = "cacheKey1";Integer hash = new Integer(45);mc.delete(key, hashCode);
怎么cache计数,增 减计数:
MemCachedClient mc = new MemCachedClient();String key = "counterKey";mc.storeCounter(key, new Integer(100));System.out.println("counter after adding 1: " mc.incr(key));System.out.println("counter after adding 5: " mc.incr(key, 5));System.out.println("counter after subtracting 4: " mc.decr(key, 4));System.out.println("counter after subtracting 1: " mc.decr(key));
利用客户端的hashcode存储计数 增减 计数:
MemCachedClient mc = new MemCachedClient();String key = "counterKey";Integer hash = new Integer(45);mc.storeCounter(key, new Integer(100), hash);System.out.println("counter after adding 1: " mc.incr(key, 1, hash));System.out.println("counter after adding 5: " mc.incr(key, 5, hash));System.out.println("counter after subtracting 4: " mc.decr(key, 4, hash));System.out.println("counter after subtracting 1: " mc.decr(key, 1, hash));
获取一个对象:
MemCachedClient mc = new MemCachedClient();String key = "key";Object value = mc.get(key);
用客户端hashcode获取一个对象:
MemCachedClient mc = new MemCachedClient();String key = "key";Integer hash = new Integer(45);Object value = mc.get(key, hash);
从cache 中获取多个对象
MemCachedClient mc = new MemCachedClient();String[] keys ={ "key", "key1", "key2" };Mapvalues = mc.getMulti(keys);
用客户端hashcode() 从cache中获取多个对象
MemCachedClient mc = new MemCachedClient();String[] keys = { "key", "key1", "key2" };Integer[] hashes ={ new Integer(45), new Integer(32), new Integer(44) };Map<Object> values = mc.getMulti(keys, hashes);
清空所有的对象
MemCachedClient mc = new MemCachedClient();mc.flushAll();
得到服务器memcached的状态信息
MemCachedClient mc = new MemCachedClient();Map stats = mc.stats();
注意点
1:Failover/Failback
当一个memcached服务器失效的时候客户端默认会failover另一个服务去.
如果失效的服务器 恢复运行,客户端会返回到原来连接的服务器.
如果你不想用这个功能 设置下面的参数
pool.setFailover( false );
pool.setFailback( false );
2:序列化
Boolean
Byte
String
Character
StringBuffer
StringBuilder
Short
Long
Double
Float
Date
java默认的类型没有实现序列化 可以设置
mcc.setPrimitiveAsString( true )替代.
Meetup.com实践过程中得出的一个经验 ,项目中model 对象implement
Externalizable 实现序列化,可以节省cache 对象的大小。从而节省网络带宽和内存空间。
[color=red]from:http://hi.baidu.com/asheng99012/blog/item/4c613816676c7756f2de327d.html[/color]
MemCachedClient 类 常用的方法说明
创建 client对象 设置参数:
MemCachedClient mc = new MemCachedClient();
//压缩模式
mc.setCompressEnable(true);
// 如果 cache数据 大于4 KB 就启用压缩
mc.setCompressThreshold(4096);
// 基本类型tostring方法
// 通常不需要设置
mc.setPrimitiveAsString(true);
存储一个对象:
MemCachedClient mc = new MemCachedClient();String key = "cacheKey1";Object value = SomeClass.getObject();mc.set(key, value);
用客户端hashcode 存储一个对象:
MemCachedClient mc = new MemCachedClient();String key = "cacheKey1";Object value = SomeClass.getObject();Integer hash = new Integer(45);mc.set(key, value, hash);set方法:在cache中存储一个指定对象 add 和replace 方法功能差不多 add -- 如果不存在 这个key的对象,将会存储一个对象到cache中 replace --只有当存在指定key对象的时候 会覆盖已有对象
删除一个对象:
MemCachedClient mc = new MemCachedClient();String key = "cacheKey1";mc.delete(key);
结合hashcode 删除一个对象:
MemCachedClient mc = new MemCachedClient();String key = "cacheKey1";Integer hash = new Integer(45);mc.delete(key, hashCode);
怎么cache计数,增 减计数:
MemCachedClient mc = new MemCachedClient();String key = "counterKey";mc.storeCounter(key, new Integer(100));System.out.println("counter after adding 1: " mc.incr(key));System.out.println("counter after adding 5: " mc.incr(key, 5));System.out.println("counter after subtracting 4: " mc.decr(key, 4));System.out.println("counter after subtracting 1: " mc.decr(key));
利用客户端的hashcode存储计数 增减 计数:
MemCachedClient mc = new MemCachedClient();String key = "counterKey";Integer hash = new Integer(45);mc.storeCounter(key, new Integer(100), hash);System.out.println("counter after adding 1: " mc.incr(key, 1, hash));System.out.println("counter after adding 5: " mc.incr(key, 5, hash));System.out.println("counter after subtracting 4: " mc.decr(key, 4, hash));System.out.println("counter after subtracting 1: " mc.decr(key, 1, hash));
获取一个对象:
MemCachedClient mc = new MemCachedClient();String key = "key";Object value = mc.get(key);
用客户端hashcode获取一个对象:
MemCachedClient mc = new MemCachedClient();String key = "key";Integer hash = new Integer(45);Object value = mc.get(key, hash);
从cache 中获取多个对象
MemCachedClient mc = new MemCachedClient();String[] keys ={ "key", "key1", "key2" };Mapvalues = mc.getMulti(keys);
用客户端hashcode() 从cache中获取多个对象
MemCachedClient mc = new MemCachedClient();String[] keys = { "key", "key1", "key2" };Integer[] hashes ={ new Integer(45), new Integer(32), new Integer(44) };Map<Object> values = mc.getMulti(keys, hashes);
清空所有的对象
MemCachedClient mc = new MemCachedClient();mc.flushAll();
得到服务器memcached的状态信息
MemCachedClient mc = new MemCachedClient();Map stats = mc.stats();
注意点
1:Failover/Failback
当一个memcached服务器失效的时候客户端默认会failover另一个服务去.
如果失效的服务器 恢复运行,客户端会返回到原来连接的服务器.
如果你不想用这个功能 设置下面的参数
pool.setFailover( false );
pool.setFailback( false );
2:序列化
Boolean
Byte
String
Character
StringBuffer
StringBuilder
Short
Long
Double
Float
Date
java默认的类型没有实现序列化 可以设置
mcc.setPrimitiveAsString( true )替代.
Meetup.com实践过程中得出的一个经验 ,项目中model 对象implement
Externalizable 实现序列化,可以节省cache 对象的大小。从而节省网络带宽和内存空间。
[color=red]from:http://hi.baidu.com/asheng99012/blog/item/4c613816676c7756f2de327d.html[/color]