spring调用memcached client for java

Java代码    收藏代码
  1. import org.springframework.context.ApplicationContext;  
  2. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  3. import com.danga.MemCached.MemCachedClient;  
  4. public class MClient {  
  5. public static void main(String[] args){  
  6. ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");  
  7. MemCachedClient mc = (MemCachedClient)ctx.getBean("memcachedClient");  
  8. for(int i=0; i<100; i++){  
  9. //try{Thread.sleep(2000);}catch(Exception e){}  
  10. mc.set("keya"+i, "value"+i);  
  11. }  
  12. try{Thread.sleep(5000);}catch(Exception e){}  
  13. for(int i=0; i<100; i++){  
  14. System.out.println("get "+i+" value "+mc.get("key"+i));  
  15. }  
  16. }  
  17. }  

spring配置文件 Xml代码 

Java代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans    
  3.     xmlns="http://www.springframework.org/schema/beans"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">    
  6.     
  7.     <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"    
  8.         init-method="initialize" destroy-method="shutDown">    
  9.         <constructor-arg><value>neeaMemcachedPool</value></constructor-arg>    
  10.         <property name="servers">    
  11.             <list>    
  12.                 <value>192.168.227.20:12111</value>    
  13.                 <value>192.168.227.20:12112</value>    
  14.             </list>    
  15.         </property>    
  16.         <property name="initConn"><value>20</value></property>    
  17.         <property name="minConn"><value>10</value></property>    
  18.         <property name="maxConn"><value>50</value></property>    
  19.         <property name="maintSleep"><value>30</value></property>    
  20.         <property name="nagle"><value>false</value></property>    
  21.         <property name="socketTO"><value>3000</value></property>    
  22.         <!--      
  23.         <property name="initConn"><value></value></property>    
  24.         <property name="initConn"><value></value></property>-->    
  25.     </bean>    
  26.         
  27.     <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">    
  28.         <constructor-arg><value>neeaMemcachedPool</value></constructor-arg>    
  29.         <property name="compressEnable"><value>true</value></property>    
  30.         <property name="compressThreshold"><value>4096</value></property>    
  31.     </bean>    
  32.     
  33. </beans>    

 

你可能感兴趣的:(memcached)