XMemcached集成

参考文档:https://code.google.com/p/xmemcached/wiki/User_Guide_zh

 

1:下载

xmemcached-1.4.2-bin-with-dependencies.tar.gz

https://code.google.com/p/xmemcached/downloads/list

解压文件,将其中的slf4j-api-1.7.5.jar  和 xmemcached-1.4.2.jar 放到项目

2:加入如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
	default-lazy-init="true">
	<description>Xmemcached 缓存配置</description>

	<bean name="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
		<!-- XMemcachedClientBuilder have two arguments.First is server list,and second is weights array. -->
		<constructor-arg>
			<list>
				<bean name="memcachedServer1" class="java.net.InetSocketAddress">
					<constructor-arg>
						<value>192.168.6.253</value>
					</constructor-arg>
					<constructor-arg>
						<value>11311</value>
					</constructor-arg>
				</bean>
				<!-- 配置多个服务器节点
				<bean class="java.net.InetSocketAddress">
					<constructor-arg>
						<value>localhost</value>
					</constructor-arg>
					<constructor-arg>
						<value>12001</value>
					</constructor-arg>
				</bean> -->
			</list>
		</constructor-arg>
		<constructor-arg>
			<list>
				<value>1</value>
				<!-- 配置多个服务器节点 权重
				<value>2</value> -->
			</list>
		</constructor-arg>
		<!-- 
		<property name="authInfoMap">
			<map>
				<entry key-ref="memcachedServer1">
					<bean class="net.rubyeye.xmemcached.auth.AuthInfo"
						factory-method="typical">
						<constructor-arg index="0">
							<value>cacheuser</value>
						</constructor-arg>
						<constructor-arg index="1">
							<value>123456</value>
						</constructor-arg>
					</bean>
				</entry>
			</map>
		</property>
		 -->
		<property name="connectionPoolSize" value="1"></property>
		<property name="commandFactory">
			<bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
			<!-- <bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean> -->
		</property>
		<!-- 采用一致性Hash 算法实现 -->
		<property name="sessionLocator">
			<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
		</property>
		<property name="transcoder">
			<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
		</property>
		 <!-- ByteBuffer allocator -->
        <property name="bufferAllocator">
                <bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
        </property>
        <!-- Failure mode -->
        <property name="failureMode" value="false"/>
	</bean>
	
	<!-- Use factory bean to build memcached client -->
	<bean name="memcachedClient" factory-bean="memcachedClientBuilder"
		factory-method="build" destroy-method="shutdown" />

</beans>

 

3:写单元测试:

package com.capitalbio.soft.test.xmemcached;

import net.rubyeye.xmemcached.MemcachedClient;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;

import com.capitalbio.soft.core.utils.spring.SpringContextHolder;
import com.capitalbio.soft.entity.account.Authority;
import com.capitalbio.soft.entity.account.Role;
import com.capitalbio.soft.test.core.SpringTransactionalTestCase;

@ContextConfiguration(locations = { "/applicationContext.xml","/applicationContext-xmemcached.xml" })
public class MemcachedTest extends SpringTransactionalTestCase {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Test public void testMemcached() {
		try {
			MemcachedClient client = SpringContextHolder.getBean(MemcachedClient.class);
			logger.debug("{}",client.get("key_conf"));
			boolean ret = client.set("key_conf", 0, new Role(1l, "张三en"));
			if(ret) {
				ret = client.replace("key_conf", 0, new Authority(1l, "权限"));
				if(ret) logger.debug("{}",client.get("key_conf"));
			}
			if(client.delete("key_conf")){
				logger.debug("删除成功! - {}",client.get("key_conf"));
			}
//			client.setWithNoReply
//			client.deleteWithNoReply
//			client.replaceWithNoReply
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 4:

到http://www.newasp.net/soft/63735.html#downloads 

下载windows版本的memcached,解压文件到无空格,无中文目录

5:

将下面代码保存为bat,更改其中的memcached路径为memcached解压的路径,且执行

@echo off

rem -c 代表访问memcached的并发数, -m 代表最大内存 -p 代表端口

sc create memcached_11311 binPath= "D:\\exec\\Memcached\\memcached_en32or64\\x64\\memcached.exe -d runservice -c 1111 -m 1024 -p 11311" DisplayName= "memcached_11311" start= auto

pause;

 6:启动项目,执行单元测试看是否成功!

 

7:以上配置为Spring3.0的,Spring3.0以下的配置为:

<!-- 以下是xmemcached对于spring3.0以下版本的配置 -->
	<!-- memcachedClient 简单配置
	<bean name="memcachedClient"
		class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean"
		destroy-method="shutdown">
		<property name="servers">
			<value>host1:port host2:port2</value>
		</property>
	</bean> -->
	<!-- memcached 复杂配置 -->
	<bean name="server1" class="java.net.InetSocketAddress">
		<constructor-arg>
			<value>host1</value>
		</constructor-arg>
		<constructor-arg>
			<value>port1</value>
		</constructor-arg>
	</bean>
	<bean name="memcachedClient"
		class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean"
		destroy-method="shutdown">
		<property name="servers">
			<value>host1:port,host2:port host3:port,host4:port</value>
		</property>
		<!-- server's weights -->
		<property name="weights">
			<list>
				<value>1</value>
				<value>2</value>
				<value>3</value>
			</list>
		</property>
		<!-- AuthInfo map,only valid on 1.2.5 or later version -->
		<property name="authInfoMap">
			<map>
				<entry key-ref="server1">
					<bean class="net.rubyeye.xmemcached.auth.AuthInfo"
						factory-method="typical">
						<constructor-arg index="0">
							<value>cacheuser</value>
						</constructor-arg>
						<constructor-arg index="1">
							<value>123456</value>
						</constructor-arg>
					</bean>
				</entry>
			</map>
		</property>
		<!-- nio connection pool size -->
		<property name="connectionPoolSize" value="2"></property>
		<!-- Use text protocol,二进制协议在xmemcached里使用namespace的时候有问题 -->
		<property name="commandFactory">
			<bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
		</property>
		<!-- Distributed strategy -->
		<property name="sessionLocator">
			<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
		</property>
		<!-- Serializing transcoder -->
		<property name="transcoder">
			<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
		</property>
		<!-- ByteBuffer allocator -->
		<property name="bufferAllocator">
			<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
		</property>
		<!-- Failure mode -->
		<property name="failureMode" value="false" />
	</bean>

 

8:具体请参见官方文档:https://code.google.com/p/xmemcached/wiki/User_Guide_zh

你可能感兴趣的:(xmemcached)