Memcached client based on Java NIO

Source: Internet
Author: User
Tags memcached advantage

1, what is xmemcached?

Xmemcached is a memcached client API based on the Java NIO implementation.

It's actually based on a simple NIO framework I've implemented (currently based on YANF4J 0.52), and the core code is no more than 1000 lines, http://code.google.com/p/yanf4j/. The serialization mechanism directly diverts spymemcached transcoder.

In terms of performance, there is still a gap between reading and writing simple types, and there is an efficient advantage in reading and writing larger objects (such as collections). spymemcached

The current 0.50-beta version only supports a single memcached server and is later considered for expansion. Several protocols for GET, set, add, replace, delete, incr, DECR, version are currently supported. The API is a blocking model, not a spymemcached asynchronous model, and asynchronous models have an advantage in batch processing, but blocking patterns can be much easier to program and use.

2, why call xmemcached?

Because I have a bread and bread in Xiamen (XM) ...

3, download and use of xmemcached

Project home: http://code.google.com/p/xmemcached/

Download Address: Http://code.google.com/p/xmemcached/downloads/list

The downloaded package includes a library, source code, and packaged jar, which can be used in the project's Lib directory.

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);


//Storage Operations


if (!client.set ("Hello", 0, "Dennis")) {


System.err.println ("set error");


             }


client.add ("Hello", 0, "Dennis");


client.replace ("Hello", 0, "Dennis");





//Get Operations


String name = (string) client.get ("Hello");


System.out.println (name);





//Bulk Access


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 operation


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);


//additions and deletions to the custom object


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 xmemcached plan?

1), add multiple servers and cluster support

2), performance optimization, refactoring

3), add CAs atomic operations, and more protocol support

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.