java nio-based memcached client - xmemcached

JavamemcachedGoogle Programming Gmail
1. What is xmemcached?
xmemcached is a memcached client API implemented based on java nio.
In fact, it is based on a simple nio framework http://code.google.com/p/yanf4j/ I implemented (currently based on yanf4j 0.52), the core code does not exceed 1000 lines, and the serialization mechanism is directly used Transcoder for spymemcached.
In terms of performance, there is still a gap between spymemcached in reading and writing simple types, and it has an efficient advantage in reading and writing relatively large objects (such as collections).
The current 0.50-beta version only supports a single memcached server, and will be considered for expansion in the future. Currently, the protocols get, set, add, replace, delete, incr, decr, and version are supported. The API is a blocking model, not the asynchronous mode of spymemcached. The asynchronous model has advantages in batch processing, but the blocking mode is much easier to program and use.
2. Why is it called xmemcached?
Because I am in Xiamen (XM)...

3. Download and use of xmemcached
Project home page: http://code.google.com/p/xmemcached/Download
address: http: //code.google.com/p/xmemcached/downloads/list
The downloaded compressed package includes the dependent library, source code and packaged jar, which can be used in the lib directory of the project.
Example reference:
<!---->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();
        }

    }
}
4. The plan of xmemcached?
1), add multi-server and cluster support
2), performance optimization, refactoring
3), add cas atomic operations and more protocol support
   
    . Interested to see and make suggestions.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327003611&siteId=291194637