XMemcached与Spring

来源:互联网 发布:mac上怎么截图 编辑:程序博客网 时间:2024/05/20 07:33

/** 
* @Title: SpringXmemcachedTest.java
* @author linhz
* @date 2014-8-22 下午04:56:14
* @version ITPUB1.0
*/


import static junit.framework.Assert.*;

import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

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

/**
*
* @ClassName: SpringXmemcachedTest
* @Description: TODO(这里用一句话描述这个类的作用)
* @author linhz
* @date 2014-8-22 下午04:56:14
*
*/

public class SpringXmemcachedTest {

     private ApplicationContext app;
     private MemcachedClient xmemcachedClient;

     @Before
     public void init() {
          app = new ClassPathXmlApplicationContext("spring-xmemcached.xml");
          xmemcachedClient = (MemcachedClient) app.getBean("xmemcachedClient");
     }

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

               // 替换
               xmemcachedClient.replace("philip", 36000, "replace");
               assertEquals("replace", xmemcachedClient.get("philip"));

               // 移除
               xmemcachedClient.delete("philip");
               assertNull(xmemcachedClient.get("philip"));
          } catch (TimeoutException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (MemcachedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }
}


spring-xmemcached.xml:
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
       <!-- http://code.google.com/p/xmemcached/wiki/Spring_Integration -->

       <bean id= "propertyConfigurer1"
             class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
             <property name="locations" >
                   <list>
                         <value> classpath:xmemcached.properties</value >
                   </list>
             </property>
             <property name="ignoreUnresolvablePlaceholders" value="true" />
       </bean>


       <bean id= "memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder"
             p:connectionPoolSize="${memcached.connectionPoolSize}" p:failureMode="${memcached.failureMode}" >
             <!--
                  XMemcachedClientBuilder have two arguments.First is server list,and
                  second is weights array.
            -->
             <constructor-arg>
                   <list>
                         <bean class="java.net.InetSocketAddress" >
                               <constructor-arg>
                                     <value> ${memcached.server1.host}</value >
                               </constructor-arg>
                               <constructor-arg>
                                     <value> ${memcached.server1.port}</value >
                               </constructor-arg>
                         </bean>
                         <bean class="java.net.InetSocketAddress" >
                               <constructor-arg>
                                     <value> ${memcached.server2.host}</value >
                               </constructor-arg>
                               <constructor-arg>
                                     <value> ${memcached.server2.port}</value >
                               </constructor-arg>
                         </bean>
                   </list>
             </constructor-arg>
             <constructor-arg>
                   <list>
                         <value> ${memcached.server1.weight}</value >
                         <value> ${memcached.server2.weight}</value >
                   </list>
             </constructor-arg>
             <property name="commandFactory" >
                   <bean class="net.rubyeye.xmemcached.command.TextCommandFactory" />
             </property>
             <property name="sessionLocator" >
                   <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
             </property>
             <property name="transcoder" >
                   <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
             </property>
       </bean>
       <!-- Use factory bean to build memcached client -->
       <bean id= "xmemcachedClient" factory-bean ="memcachedClientBuilder"
             factory-method="build" destroy-method= "shutdown" />
</beans>

xmemcached.properties:
memcached.connectionPoolSize=50
memcached.failureMode=true
#server1
memcached.server1.host=132.96.27.25
memcached.server1.port=11211
memcached.server1.weight=1
#server2
memcached.server2.host=132.96.27.25
memcached.server2.port=11212
memcached.server2.weight=1
0 0
原创粉丝点击