SpyMemcache与Spring整合

来源:互联网 发布:衡安称重软件 编辑:程序博客网 时间:2024/06/06 10:42

/** 
 * @Title: SpringSpyMemcachedTest.java
 * @Package com.XXX.itpub.plugin.memcached.pub
 * @Description: TODO(用一句话描述该文件做什么)
 * @author linhz
 * @date 2014 -8- 22 下午05:25:51
 * @version ITPUB1.0
 */

package com.xxx.itpub.plugin.memcached.pub;

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

import java.util.concurrent.TimeoutException;

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

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

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

public class SpringSpyMemcachedTest {
       private ApplicationContext app ;
       private MemcachedClient spyMemcachedClient ;

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

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

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

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

spring-spymemcached.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://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">

       <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>


       <!-- SpyMemcached客户端-->
       <bean name ="spyMemcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean" >
             <property name="servers" value="${memcached.url}" />
             <property name="protocol" value="TEXT" /><!-- BINARY -->
             <property name="locatorType" value="CONSISTENT" />
             <property name="opTimeout" value="1000" />

             <property name="transcoder" >
                   <bean class="net.spy.memcached.transcoders.SerializingTranscoder" >
                         <property name="compressionThreshold" value="1024" />
                   </bean>
             </property>

             <property name="timeoutExceptionThreshold" value="1998" />
            <property name="hashAlg" >
                   <value type="net.spy.memcached.DefaultHashAlgorithm" >KETAMA_HASH</value>
             </property>
             <property name="failureMode" value="Redistribute" />
             <property name="useNagleAlgorithm" value="false" />
       </bean>
</beans>
属性说明:
Servers
一个字符串,包括由空格或逗号分隔的主机或IP地址与端口号
Daemon
设置IO线程的守护进程(默认为true)状态
FailureMode
设置故障模式(取消,重新分配,重试),默认是重新分配
HashAlg
设置哈希算法(见net.spy.memcached.HashAlgorithm的值)
InitialObservers
设置初始连接的观察者(观察初始连接)
LocatorType
设置定位器类型(ARRAY_MOD,CONSISTENT),默认是ARRAY_MOD
MaxReconnectDelay
设置最大的连接延迟
OpFact
设置操作工厂
OpQueueFactory
设置操作队列工厂
OpTimeout
以毫秒为单位设置默认的操作超时时间
Protocol
指定要使用的协议(BINARY,TEXT),默认是TEXT
ReadBufferSize
设置读取的缓冲区大小
ReadOpQueueFactory
设置读队列工厂
ShouldOptimize
如果默认操作优化是不可取的,设置为false(默认为true)
Transcoder
设置默认的转码器(默认以net.spy.memcached.transcoders.SerializingTranscoder)
UseNagleAlgorithm
如果你想使用Nagle算法,设置为true
WriteOpQueueFactory
设置写队列工厂
AuthDescriptor
设置authDescriptor,在新的连接上使用身份验证


xmemcached.properties:
memcached.connectionPoolSize=50
memcached.failureMode=true
memcached.url=132.96.27.25: 11211,132.96.27.25:11212
0 0
原创粉丝点击