欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring调用memcached client for java

程序员文章站 2022-05-23 08:51:45
...

转载:spring调用memcached client for java

memcached client for java客户端API:memcached client for java 
网址:http://www.whalin.com/memcached 

调用测试类 
MClient.java 
Java代码  spring调用memcached client for java
            
    
    博客分类: Javamemcache Javamemcachememcached 
  

转载:spring调用memcached client for java

memcached client for java客户端API:memcached client for java 
  1. package bcndyl.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  5.   
  6. import com.danga.MemCached.MemCachedClient;  
  7.   
  8. public class MClient {  
  9.       
  10.     public static void main(String[] args){  
  11.           
  12.         ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");  
  13.         MemCachedClient mc = (MemCachedClient)ctx.getBean("memcachedClient");  
  14.         for(int i=0; i<100; i++){  
  15.             //try{Thread.sleep(2000);}catch(Exception e){}  
  16.             mc.set("key"+i, "value"+i);  
  17.         }  
  18.         try{Thread.sleep(5000);}catch(Exception e){}  
  19.         for(int i=0; i<100; i++){  
  20.             System.out.println("get "+i+" value "+mc.get("key"+i));  
  21.         }  
  22.     }  
  23.   
  24. }  


spring配置文件 

Xml代码  spring调用memcached client for java
            
    
    博客分类: Javamemcache Javamemcachememcached 
  

转载:spring调用memcached client for java

memcached client for java客户端API:memcached client for 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>  


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