admin 发表于 2016-2-4 13:32

Spring集成XMemcached

一、Memcached Client简要介绍
Memcached Client目前有3种:
Memcached Client for Java
SpyMemcached
XMemcached


这三种Client一直存在各种争议:
Memcached Client for Java 比 SpyMemcached更稳定、更早、更广泛;
SpyMemcached 比 Memcached Client for Java更高效;
XMemcached 比 SpyMemcache并发效果更好。


用数据来说话,参考官方性能对比:
Memcached Client for Java:https://github.com/gwhalin/Memcached-Java-Client/wiki/PERFORMANCE
XMemcached:http://xmemcached.googlecode.com ... mark/benchmark.html


二、XMemcached特性
XMemcached特性:
高性能
支持完整的memcached文本协议,二进制协议。
支持JMX,可以通过MBean调整性能参数、动态添加/移除server、查看统计等。
支持客户端统计
支持memcached节点的动态增减。
支持memcached分布:余数分布和一致性哈希分布。
更多的性能调整选项。
四、XMemcached与Spring集成
memcached.properties做基本配置:
#连接池大小即客户端个数
memcached.connectionPoolSize= 50
#server1
memcached.server1.host= 127.0.0.1
memcached.server1.port= 12000
memcached.server1.weight= 4XML配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:cache="http://www.springframework.org/schema/cache"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-3.0.xsd">
    <!-- 加载资源文件其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->
    <context:property-placeholder location="classpath:memcache.properties" />
    <bean
         id= "memcachedClientBuilder"
         class= "net.rubyeye.xmemcached.XMemcachedClientBuilder"
         p:connectionPoolSize = "${memcached.connectionPoolSize}">
         <constructor-arg >
             <list>
               <beanclass= "java.net.InetSocketAddress" >
                     <constructor-arg >
                         <value>${memcached.server1.host}</value >
                     </constructor-arg >
                     <constructor-arg >
                         <value>${memcached.server1.port}</value >
                     </constructor-arg >
               </bean>
             </list >
         </constructor-arg >
         <constructor-arg >
             <list >
               <value>${memcached.server1.weight} </value >
             </list >
         </constructor-arg >
         <propertyname= "commandFactory" >
             <beanclass= "net.rubyeye.xmemcached.command.TextCommandFactory"/>
         </property >
         <propertyname= "sessionLocator" >
             <beanclass= "net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"/>
         </property >
         <propertyname= "transcoder" >
             <beanclass= "net.rubyeye.xmemcached.transcoders.SerializingTranscoder"/>
         </property >
   </bean >
   <bean
         id= "memcachedClient"
         factory-bean= "memcachedClientBuilder"
         factory-method= "build"
         destroy-method= "shutdown"/>
</beans>
MemcachedSpringTest:
package com.itstyle.cache;

import static org.junit.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;

public class MemcachedSpringTest {
      private ApplicationContext app;
    private MemcachedClient memcachedClient;

      @Before
      public void init() {
                app = new ClassPathXmlApplicationContext("spring-mvc.xml");
                memcachedClient = (MemcachedClient) app.getBean("memcachedClient");
      }
      @Test
      public void test() {
                try {
                        // 设置/获取
                        memcachedClient.set("zlex", 36000, "set/get");
                        assertEquals("set/get", memcachedClient.get("zlex"));
                        // 替换
                        memcachedClient.replace("zlex", 36000, "replace");
                        assertEquals("replace", memcachedClient.get("zlex"));
                        // 移除
                        System.out.println(memcachedClient.get("zlex"));
                        memcachedClient.delete("zlex");
                        assertNull(memcachedClient.get("zlex"));
                } catch (TimeoutException e) {
                        e.printStackTrace();
                } catch (InterruptedException e) {
                        e.printStackTrace();
                } catch (MemcachedException e) {
                        e.printStackTrace();
                }
      }
}
项目所需JAR包

项目结构:




页: [1]
查看完整版本: Spring集成XMemcached