Principle and Implementation of memcache in Java Development

Source: Internet
Author: User

VII. memcached
Client Program

There are already three types of memcached Java clients:

? Officially provided client maintained by Greg whalin based on traditional blocking Io

? Java NiO-based spymemcached implemented by Dustin sallings

? Xmemcached

1. Comparison of Three APIs

1) memcached client for Java

The memcached Java client API was launched earlier, which is widely used and runs stably.

2) spymemcached

A simple, asynchronous, single-threaded
Memcached client written in Java.
Supports asynchronous and single-threaded memcached clients that use concurrent and NiO of Java. The access speed is higher than the former, but the stability is poor.
Timeout and other related exceptions are reported.

3) xmemcached

Xmemcached is also based on Java NiO client, Java
Compared with traditional block I/O models, NiO has the advantages of high efficiency (especially in high concurrency) and low resource consumption. To improve efficiency, traditional Io blocking requires creating a certain number of connections to form connections.
While NiO only needs one connection (of course, NiO can also be pooled), which reduces the overhead of thread creation and switching, which is particularly significant in high concurrency. Therefore
Xmemcached and spymemcached have excellent performance. In some aspects (when the storage data is relatively small), xmemcached is better
Spymemcached performs better. For details, refer to this Java memcached clients benchmark.

2. Suggestions

Memcached client for Java has released a new version, which improves performance and runs stably. Therefore, we recommend that you use memcached client for Java.

Xmemcached is also widely used and has detailed Chinese API documentation, which has the following features: High Performance
Supports complete protocols, client distribution, node weight setting, dynamic addition and deletion, JMX, Spring framework, and hibernate-memcached sets.
Client Connection Pool, good scalability, etc.

The following are examples of these three clients.

3. Sample program

1) memcached client for Java

Download the latest version of the Client
Package order: java_memcached-release_2.5.1.zip, unzip, find java_memcached-in the folder-
Release_2.5.1.jar. This is the jar package of the client. Add the jar package to the build path of the project. Then, you can use memcached in the project.

The sample code is as follows:

Package temp;

 

Import com. danga. memcached .*;

Import org. Apache. log4j .*;

 

Public class cachetest {

Public static void main (string [] ARGs ){

/**

* Initialize the sockiopool and manage the memcached connection pool.

**/

String [] servers = {"10.11.15.222: 10000 "};

Sockiopool pool = sockiopool. getinstance ();

Pool. setservers (servers );

Pool. setfailover (true );

Pool. setinitconn (10 );

Pool. setminconn (5 );

Pool. setmaxconn (250 );

Pool. setmaintsleep (30 );

Pool. setnagle (false );

Pool. setsocketto (3000 );

Pool. setalivecheck (true );

Pool. initialize ();

 

/**

* Create a memcachedclient instance

**/

Memcachedclient = new memcachedclient ();

For (INT I = 0; I <1000; I ++ ){

/**

* Add an object to the memcached Cache

**/

Boolean success = memcachedclient. Set ("" + I, "Hello! ");

/**

* Get an object from the memcached cache by key value

**/

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

}

}

}

2) spymemcached

The current version of spymemcached is 2.5. The official website is http://code.google.com/p/spymemcached /. Available from address: http://spymemcached.googlecode.com/files/memcached-2.5.jar
Download the latest version.

The sample code is as follows:

Package temp;

 

Import java.net. inetsocketaddress;

Import java. util. Concurrent. Future;

 

Import net. Spy. memcached. memcachedclient;

 

Public class testspymemcache {

Public static void main (string [] ARGs ){

// Save the object

Try {

/* Create a memcachedclient instance and specify the IP address and port number of the memcached Service */

Memcachedclient MC = new memcachedclient (New inetsocketaddress ("10.11.15.222", 10000 ));

Future <Boolean> B = NULL;

/* Set the key value, expiration time (in seconds), and object to be cached to memcached */

B = mc. Set ("NEEA: testdaf: ksidno", 900, "someobject ");

If (B. Get (). booleanvalue () = true ){

MC. Shutdown ();

}

} Catch (exception ex ){

Ex. printstacktrace ();

}

// Obtain the object

Try {

/* Create a memcachedclient instance and specify the IP address and port number of the memcached Service */

Memcachedclient MC = new memcachedclient (New inetsocketaddress ("10.11.15.222", 10000 ));

/* Query the cache from memcached based on the key value. If no cache exists, null is returned */

Object B = mc. Get ("NEEA: testdaf: ksidno ");

System. Out. println (B. tostring ());

MC. Shutdown ();

} Catch (exception ex ){

Ex. printstacktrace ();

}

}

}

3) xmemcached

The official website of xmemcached is http://code.google.com/p/xmemcached/. you can download the latest version 1.2.4 from its official website.
. Address: http://xmemcached.googlecode.com/files/xmemcached-1.2.4-src.tar.gz
.

The sample code is as follows:

Package temp;

 

Import java. Io. ioexception;

Import java. util. Concurrent. timeoutexception;

 

Import net. rubyeye. xmemcached. utils. addrutil;

Import net. rubyeye. xmemcached. memcachedclient;

Import net. rubyeye. xmemcached. memcachedclientbuilder;

Import net. rubyeye. xmemcached. xmemcachedclientbuilder;

Import net. rubyeye. xmemcached. Exception. memcachedexception;

 

Public class testxmemcache {

Public static void main (string [] ARGs ){

Memcachedclientbuilder builder = new xmemcachedclientbuilder (addrutil

. Getaddresses ("10.11.15.222: 10000 "));

Memcachedclient;

Try {

Memcachedclient = builder. Build ();

 

Memcachedclient. Set ("hello", 0, "Hello, xmemcached ");

String value = memcachedclient. Get ("hello ");

System. Out. println ("Hello =" + value );

Memcachedclient. Delete ("hello ");

Value = memcachedclient. Get ("hello ");

System. Out. println ("Hello =" + value );

// Close memcached Client

Memcachedclient. Shutdown ();

} Catch (memcachedexception e ){

System. Err. println ("memcachedclient Operation Fail ");

E. printstacktrace ();

} Catch (timeoutexception e ){

System. Err. println ("memcachedclient operation timeout ");

E. printstacktrace ();

} Catch (interruptedexception e ){

// Ignore

} Catch (ioexception e ){

System. Err. println ("shutdown memcachedclient fail ");

E. printstacktrace ();

}

}

}

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.