天天看点

spring调用memcached client for java

memcached client for java客户端api:memcached client for java 

网址:http://www.whalin.com/memcached 

调用测试类 

mclient.java 

spring调用memcached client for java

package bcndyl.test;  

import org.springframework.context.applicationcontext;  

import org.springframework.context.support.filesystemxmlapplicationcontext;  

import com.danga.memcached.memcachedclient;  

public class mclient {  

    public static void main(string[] args){  

        applicationcontext ctx=new filesystemxmlapplicationcontext("src/applicationcontext.xml");  

        memcachedclient mc = (memcachedclient)ctx.getbean("memcachedclient");  

        for(int i=0; i<100; i++){  

            //try{thread.sleep(2000);}catch(exception e){}  

            mc.set("key"+i, "value"+i);  

        }  

        try{thread.sleep(5000);}catch(exception e){}  

            system.out.println("get "+i+" value "+mc.get("key"+i));  

    }  

}  

spring配置文件 

spring调用memcached client for java

<?xml version="1.0" encoding="utf-8"?>  

<beans  

    xmlns="http://www.springframework.org/schema/beans"  

    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"  

    xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  

    <bean id="memcachedpool" class="com.danga.memcached.sockiopool" factory-method="getinstance"  

        init-method="initialize" destroy-method="shutdown">  

        <constructor-arg><value>neeamemcachedpool</value></constructor-arg>  

        <property name="servers">  

            <list>  

                <value>192.168.227.20:12111</value>  

                <value>192.168.227.20:12112</value>  

            </list>  

        </property>  

        <property name="initconn"><value>20</value></property>  

        <property name="minconn"><value>10</value></property>  

        <property name="maxconn"><value>50</value></property>  

        <property name="maintsleep"><value>30</value></property>  

        <property name="nagle"><value>false</value></property>  

        <property name="socketto"><value>3000</value></property>  

        <!--    

        <property name="initconn"><value></value></property>  

        <property name="initconn"><value></value></property>-->  

    </bean>  

    <bean id="memcachedclient" class="com.danga.memcached.memcachedclient">  

        <property name="compressenable"><value>true</value></property>  

        <property name="compressthreshold"><value>4096</value></property>  

</beans>  

经过测试,个人感觉,这个java的memcached api比spymemcached要好用,可以让spring来维护这个memcached的connection连接池