Memcached (ix) client Advanced-java

Source: Internet
Author: User
Tags aop connection pooling constructor memcached xmlns

Brief introduction

There are currently 3 Java clients in common use.

Memcachedclient

Spymemcached

Xmemcached

Memcachedclient: The author of the test, in high concurrency, easy to report abnormal, out of the Memory.

Spy: Performance is slightly worse than xmemcached.

Xmemcached: Although it is domestic, but preferred, performance and stability is very strong.

Xmemcached

Code resolution

Setting connection pooling and weights

Java code

Memcachedclientbuilder builder = new Xmemcachedclientbuilder (addrutil.getaddresses ("10.11.155.26:11211 10.11.155.41:11211 10.10.76.31:11211 10.10.76.35:11211 "), new int[] {1, 1, 1, 1});  
Set the connection pool size, that is, the number of clients     
builder.setconnectionpoolsize (50);

Name space

Java code

Memcachedclient.beginwithnamespace ("SX"); Name space

Set up a consistent hash algorithm and Failuremode

Java code

try {  
    Memcachedclientbuilder builder = new Xmemcachedclientbuilder (Addrutil.getaddressmap (" 10.10.160.153:11211,10.10.160.153:11212 "));  
      
    Builder.setsessionlocator (New Ketamamemcachedsessionlocator ());  
    Builder.setfailuremode (TRUE); In this mode, when a node is hung, requests to this node will throw the memcachedexception exception directly.   
    memcachedclient client = Builder.build ();  
} catch (Exception e) {  
    System.out.println ("Custom Exception");  
    E.printstacktrace ();  
}

Add a status Listener

Java code

Memcachedclient.addstatelistener (New Memcachedclientstatelistener ())//Here is an anonymous class

Integration with the spring3.2.5

XML code

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.2.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.2.xsd "> <!--profile--> <bean id=" configproperties "class=" org.springframework.beans.fact  
     Ory.config.PropertiesFactoryBean "> <property name=" Locations ">       <list> <value>classpath:application.properties</value> </list&  
        Gt </property> </bean> <bean id= "Propertyconfigurer" class= "org.springframework.beans.factory.conf Ig. Preferencesplaceholderconfigurer "> <property name=" Properties "ref=" Configproperties "/> </bea N> <!--memcached initialization--> <bean id= "Builder" class= Cachedclientbuilder "> <constructor-arg> <bean class=" net.rubyeye.xmemcached.utils.Addr  
            Util "factory-method=" getaddresses "> <constructor-arg value=" ${memcached.host} "/> </bean> </constructor-arg> </bean> <bean id= "memcachedclient" class= "Net.ruby Eye.xmemcached.MemcachedClient "factory-bean=" builder "factory-method=" Build "destroy-method=" Shutdown "/> </ Beans>

From Iteye, Author: 85977328

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/tools/

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.