liuhongyu 发表于 2015-11-19 08:00:07

Spring与whalin MemCached

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;

import com.whalin.MemCached.MemCachedClient;


import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
* @ClassName: SpringMemcachedTest
* @Description: TODO(这里用一句话描述这个类的作用)
* @author linhz
* @date 2014-8-22 下午05:25:51
*
*/

public class SpringMemcachedTest {
   private ApplicationContext app;
   private MemCachedClient memcachedClient;

   @Before
   public void init() {
          app = new ClassPathXmlApplicationContext("spring-memcached.xml");
          memcachedClient = (MemCachedClient) app.getBean("memcachedClient");
   }

   @Test
   public void test() {
          try {
               // 设置/获取
               memcachedClient.set("philip", "set/get");
               assertEquals("set/get", memcachedClient.get("philip"));

               // 替换
               memcachedClient.replace("philip", "replace");
               assertEquals("replace", memcachedClient.get("philip").toString());

               // 移除
               memcachedClient.delete("philip");
               assertNull(memcachedClient.get("philip"));
          } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
   }
}

spring-memcached.xml:<?xml version =&quot;1.0&quot; encoding= &quot;UTF-8&quot;?><beans xmlns =&quot;http://www.springframework.org/schema/beans&quot;       xmlns:xsi =&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:aop =&quot;http://www.springframework.org/schema/aop&quot;       xmlns:tx =&quot;http://www.springframework.org/schema/tx&quot;       xsi:schemaLocation =&quot;http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd&quot;>
       <bean id= &quot;propertyConfigurer1&quot;             class =&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot; >             <property name =&quot;locations&quot; >                   <list>                         <value> classpath:xmemcached.properties</value >                   </list>             </property>             <property name =&quot;ignoreUnresolvablePlaceholders&quot; value=&quot;true&quot; />       </bean>
       <bean id= &quot;memcachedPool&quot; class =&quot;com.whalin.MemCached.SockIOPool&quot;             factory-method =&quot;getInstance&quot; init-method= &quot;initialize&quot; destroy-method =&quot;shutDown&quot; >             <constructor-arg>                   <value> neeaMemcachedPool</value >             </constructor-arg>             <property name =&quot;servers&quot; >                   <list>                         <value> 132.96.27.25:11211</value >                         <value> 132.96.27.25:11212</value >                   </list>             </property>             <property name =&quot;initConn&quot; >                   <value> 20</ value>             </property>             <property name =&quot;minConn&quot; >                   <value> 10</ value>             </property>             <property name =&quot;maxConn&quot; >                   <value> 50</ value>             </property>             <property name =&quot;maintSleep&quot; >                   <value> 30</ value>             </property>             <property name =&quot;nagle&quot; >                   <value> false</value >             </property>             <property name =&quot;socketTO&quot; >                   <value> 3000</value >             </property>       </bean>
       <bean id= &quot;memcachedClient&quot; class =&quot;com.whalin.MemCached.MemCachedClient&quot; >             <constructor-arg>                   <value> neeaMemcachedPool</value >             </constructor-arg>             <!--                  以下两个属性在下一版本中将移除 <property name=&quot;compressEnable&quot;> <value>true</value>                  </property> <property name=&quot;compressThreshold&quot;> <value>4096</value>                  </property>            -->       </bean>
</beans>         版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: Spring与whalin MemCached