基于java nio的memcached客户端——xmemcached
程序员文章站
2022-03-01 13:46:10
...
1、xmemcached是什么?
xmemcached是基于java nio实现的memcached客户端API。
实际上是基于我实现的一个简单nio框架 http://code.google.com/p/yanf4j/ 的基础上实现的(目前是基于yanf4j 0.52),核心代码不超过1000行,序列化机制直接挪用spymemcached的Transcoder。
性能方面,在读写简单类型上比之spymemcached还是有差距,在读写比较大的对象(如集合)有效率优势。
当 前0.50-beta版本,仅支持单个memcached服务器,以后考虑扩展。目前已经支持get、set、add、replace、delete、 incr、decr、version 这几个协议。API为阻塞模型,而非spymemcached的异步模式,异步模型在批处理的时候有优势,但是阻塞模 式在编程难度和使用上会容易很多。
2、为什么叫xmemcached?
因为我在厦门(XM)混饭......
3、xmemcached的下载和使用
项目主页:http://code.google.com/p/xmemcached/
下载地址:http://code.google.com/p/xmemcached/downloads/list
下载的压缩包中包括了依赖库、源码和打包后的jar,放到项目的lib目录下即可使用。
示例参考:
<!---->package
net.rubyeye.xmemcached.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.Serializable;
import net.rubyeye.xmemcached.XMemcachedClient;
class Name implements Serializable {
String firstName;
String lastName;
int age;
int money;
public Name(String firstName, String lastName, int age, int money) {
super ();
this .firstName = firstName;
this .lastName = lastName;
this .age = age;
this .money = money;
}
public String toString() {
return " [ " + firstName + " " + lastName + " ,age= " + age + " ,money= "
+ money + " ] " ;
}
}
public class Example {
public static void main(String[] args) {
try {
String ip = " 192.168.222.100 " ;
int port = 11211 ;
XMemcachedClient client = new XMemcachedClient(ip, port);
// 存储操作
if ( ! client.set( " hello " , 0 , " dennis " )) {
System.err.println( " set error " );
}
client.add( " hello " , 0 , " dennis " );
client.replace( " hello " , 0 , " dennis " );
// get操作
String name = (String) client.get( " hello " );
System.out.println(name);
// 批量获取
List < String > keys = new ArrayList < String > ();
keys.add( " hello " );
keys.add( " test " );
Map < String, Object > map = client.get(keys);
System.out.println( " map size: " + map.size());
// delete操作
if ( ! client.delete( " hello " , 1000 )) {
System.err.println( " delete error " );
}
// incr,decr
client.incr( " a " , 4 );
client.decr( " a " , 4 );
// version
String version = client.version();
System.out.println(version);
// 增删改查自定义对象
Name dennis = new Name( " dennis " , " zhuang " , 26 , - 1 );
System.out.println( " dennis: " + dennis);
client.set( " dennis " , 0 , dennis);
Name cachedPerson = (Name) client.get( " dennis " );
System.out.println( " cachedPerson: " + cachedPerson);
cachedPerson.money = - 10000 ;
client.replace( " dennis " , 0 , cachedPerson);
Name cachedPerson2 = (Name) client.get( " dennis " );
System.out.println( " cachedPerson2: " + cachedPerson2);
// delete
client.delete( " dennis " );
System.out.println( " after delete: " + client.get( " dennis " ));
client.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.Serializable;
import net.rubyeye.xmemcached.XMemcachedClient;
class Name implements Serializable {
String firstName;
String lastName;
int age;
int money;
public Name(String firstName, String lastName, int age, int money) {
super ();
this .firstName = firstName;
this .lastName = lastName;
this .age = age;
this .money = money;
}
public String toString() {
return " [ " + firstName + " " + lastName + " ,age= " + age + " ,money= "
+ money + " ] " ;
}
}
public class Example {
public static void main(String[] args) {
try {
String ip = " 192.168.222.100 " ;
int port = 11211 ;
XMemcachedClient client = new XMemcachedClient(ip, port);
// 存储操作
if ( ! client.set( " hello " , 0 , " dennis " )) {
System.err.println( " set error " );
}
client.add( " hello " , 0 , " dennis " );
client.replace( " hello " , 0 , " dennis " );
// get操作
String name = (String) client.get( " hello " );
System.out.println(name);
// 批量获取
List < String > keys = new ArrayList < String > ();
keys.add( " hello " );
keys.add( " test " );
Map < String, Object > map = client.get(keys);
System.out.println( " map size: " + map.size());
// delete操作
if ( ! client.delete( " hello " , 1000 )) {
System.err.println( " delete error " );
}
// incr,decr
client.incr( " a " , 4 );
client.decr( " a " , 4 );
// version
String version = client.version();
System.out.println(version);
// 增删改查自定义对象
Name dennis = new Name( " dennis " , " zhuang " , 26 , - 1 );
System.out.println( " dennis: " + dennis);
client.set( " dennis " , 0 , dennis);
Name cachedPerson = (Name) client.get( " dennis " );
System.out.println( " cachedPerson: " + cachedPerson);
cachedPerson.money = - 10000 ;
client.replace( " dennis " , 0 , cachedPerson);
Name cachedPerson2 = (Name) client.get( " dennis " );
System.out.println( " cachedPerson2: " + cachedPerson2);
// delete
client.delete( " dennis " );
System.out.println( " after delete: " + client.get( " dennis " ));
client.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、xmemcached的计划?
1)、添加多服务器和集群支持
2)、性能优化、重构
3)、添加cas原子操作以及更多协议支持
有兴趣的瞧瞧,提提建议。
推荐阅读
-
基于memcached client for java的cache封装
-
基于memcached client for java的cache封装
-
JAVA(多人聊天室) 最简单的控制台输出版,NIO单线程服务器,双线程客户端,妈妈再也不担心我不会写聊天室了
-
基于openssl、tomcat 7、Axis2的双向CA认证方案以及Java客户端调用
-
【JAVA】基于Socket协议传输的服务端与客户端实例!(可直接运行)
-
java:(一)基于UDP的单客户端连接
-
xmemcached 1.4.2发布,高性能Java Memcached客户端
-
实例讲解分布式缓存软件Memcached的Java客户端使用
-
一个用java的NIO实现的socket的客户端和服务端的demo
-
实例讲解分布式缓存软件Memcached的Java客户端使用