Memcached xmemcached与 Spring集成

来源:互联网 发布:linux查看hba型号 编辑:程序博客网 时间:2024/05/14 03:07

a. xmemcached简介

b. xmemcached的分布式

c. xmemcached支持的存储对象

d. xmemcached的容错性

e. xmemcached的性能测试

    由于memcached可以与spring集成,所以本博客以和spring集成来讲以上几点


先来看看如何与spring集成,首先在pom中引入相应的包(spring相关的不列出来)

<dependency>  

<groupId>com.googlecode.xmemcached</groupId>  

<artifactId>xmemcached</artifactId>  

 <version>2.0.0</version>  

</dependency> 


以下是properties文件:



##########memcached config start #################################
# the pool size(the number of client)  
memcached.connectionPoolSize=20  
# in this mode, when a node out, it will throws MemcachedException when call this node  
memcached.failureMode=true  
#server1    
memcached.server1.host=localhost
memcached.server1.port=11211
memcached.server1.weight=1  
#server2    
memcached.server2.host=192.168.88.141
memcached.server2.port=11211
memcached.server2.weight=1
#server3    
memcached.server3.host=192.168.88.142
memcached.server3.port=11211
memcached.server3.weight=1


##########memcached config end #################################


<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:conf/sys/cache.properties"
ignore-unresolvable="true" />




<!-- memcached 分布式缓存配置 connectionPoolSize 连接数, failureMode 开发模式 -->
<bean id="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder" >
<!-- 设置服务器地址端口 ps: 多结点可以配置多个以及权重 -->
<constructor-arg>
<list>
<bean class="java.net.InetSocketAddress">
<constructor-arg>
<!-- 配置server1 ip地址 -->
<value>${memcached.server1.host}</value>
</constructor-arg>
<constructor-arg>
<!-- 配置server1 端口 -->
<value>${memcached.server1.port}</value>
</constructor-arg>
</bean>
<!-- <bean class="java.net.InetSocketAddress"> -->
<!-- <constructor-arg> -->
<!-- 配置server3 ip地址 -->
<!-- <value>${memcached.server2.host}</value> -->
<!-- </constructor-arg> -->
<!-- <constructor-arg> -->
<!-- 配置server2 端口 -->
<!-- <value>${memcached.server2.port}</value> -->
<!-- </constructor-arg> -->
<!-- </bean> -->
<!-- <bean class="java.net.InetSocketAddress"> -->
<!-- <constructor-arg> -->
<!-- 配置 server3 ip地址 -->
<!-- <value>${memcached.server3.host}</value> -->
<!-- </constructor-arg> -->
<!-- <constructor-arg> -->
<!-- 配置server3 端口 -->
<!-- <value>${memcached.server3.port}</value> -->
<!-- </constructor-arg> -->
<!-- </bean> -->
</list>
</constructor-arg>
<!-- 集群时设置结点权重 -->
<!-- <constructor-arg> -->
<!-- <list> -->
<!-- <value>${memcached.server1.weight}</value> -->
<!-- <value>${memcached.server2.weight}</value> -->
<!-- <value>${memcached.server3.weight}</value> -->
<!-- </list> -->
<!-- </constructor-arg> -->
<!-- 二进制通信编码方式  -->
<!-- <property name="commandFactory"> -->
<!-- <bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory" 
/> -->
<!-- </property> -->
<!-- 缓冲区分配器 -->
<property name="bufferAllocator">
<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
</property>
<!-- 文本通信编码方式 -->
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
</property>
<!-- Session 分配器 一致hash值 -->
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
</property>
<!-- 通信转码器 -->
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>


<property name="connectionPoolSize" value="${memcached.connectionPoolSize}"/>
<!-- <property name="failureMode" value="${memcached.failureMode}"/> -->
</bean>
<!-- 使用工厂bean来构建的memcached客户端 -->
<bean id="memcachedClient" factory-bean="memcachedClientBuilder"
factory-method="build" destroy-method="shutdown" />


</beans>




package com.eter.api.trans.common;


import static com.eter.framework.util.Assertion.notNullValue;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.TimeoutException;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import net.rubyeye.xmemcached.GetsResponse;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;


/**
 * memcached 缓存工具类
 * 
 * @author OF
 * @date 2016年10月9日
 */
@Component
public class MemcachedUtil {
private static Logger log = LoggerFactory.getLogger(JedisEx.class);
@Autowired
private MemcachedClient memcachedClient;


/**
* 获取数据

* @param key
*            关键字
* @return 字符串
*/
public Object get(String key) {
Object result = null;
try {
result = memcachedClient.get(key);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}


/**
* 设置数据过期时间

* @param key
*            关键字
* @param value
*            值
* @param expire
*            生存时间 1000

* @return boolean
*/
public boolean set(String key, Object value, int expire) {
boolean result = false;
try {
result = memcachedClient.set(key, expire, value);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}


/**
* 设置数据

* @param key
*            关键字
* @param value
*            值
* @return boolean
*/
public boolean set(String key, Object value) {
boolean result = false;
try {
result = memcachedClient.set(key, 0, value);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}


/**
* 替换更新数据

* @param key
*            关键字
* @param value
*            值
* @return boolean
*/
public boolean update(String key, Object value) {
boolean result = false;
try {
result = memcachedClient.replace(key, 0, value);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}


/**
* 替换更新数据 过期时间

* @param key
*            关键字
* @param value
*            值
* @param expire
*             100
* @return boolean
*/
public boolean replace(String key, Object value, int expire) {
boolean result = false;
try {
result = memcachedClient.replace(key, expire, value);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}
/**
* 增加数据

* @param key
*            关键字
* @param value
*            值
* @return boolean
*/
public boolean add(String key, Object value,int expire) {
boolean result = false;
try {
result = memcachedClient.add(key,expire, value);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}
/**
* 增加数据

* @param key
*            关键字
* @param value
*            值
* @return boolean
*/
public boolean add(String key, Object value) {
boolean result = false;
try {
result = memcachedClient.add(key,0, value);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}
/**
* 向后追加数据

* @param key
*            关键字
* @param value
*            值
* @return boolean
*/
public boolean append(String key, Object value) {
boolean result = false;
try {
result = memcachedClient.append(key, value);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}


/**
* 向前追加数据

* @param key
*            关键字
* @param value
*            值
* @return boolean
*/
public boolean prepend(String key, Object value) {
boolean result = false;
try {
result = memcachedClient.prepend(key, value);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}


/**
* 递增

* @param key
*            关键字
* @param value
*            值
* @return LONG
*/
public long incr(String key, long value) {
long result = 0L;
try {
result = memcachedClient.incr(key, value);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}


/**
* 递减

* @param key
*            关键字
* @param value
*            值
* @return LONG
*/
public long decr(String key, long value) {
long result = 0L;
try {
result = memcachedClient.decr(key, value);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}


/**
* 删除

* @param key
*            关键字
* @return
*/
public boolean del(String key) {
boolean result = true;
try {
result = memcachedClient.delete(key);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}

/**
* 删除

* @param key
*            关键字
* @return
*/
public void deleteWithNoReply(String key) {
try {
 memcachedClient.deleteWithNoReply(key);
} catch (InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
}
/**
* 查询此缓存是否存在
* @param <T>
* @param key
* @param secondsToExpire
* @param value
* @return
*/
public <T> boolean cas(String key, int secondsToExpire, T value) {
boolean flag = false;
long cas = 0;
notNullValue("key",key);
try {
GetsResponse<Long> result = memcachedClient.gets(key);
if(result != null){
cas = result.getCas(); 
}
if(memcachedClient.cas(key, secondsToExpire, value, cas)){
flag = true;// 说明此值被修改过
}
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return flag;
}
/**
* 统计具体的项目

* @param projectName
*            项目名
* @return
*/
public Map<InetSocketAddress, Map<String, String>> getStatsByItem(
String projectName) {
Map<InetSocketAddress, Map<String, String>> result = null;
try {
result = memcachedClient.getStatsByItem(projectName);
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}


/**
* 统计协议用于查看统计信息

* @return
*/
public Map<InetSocketAddress, Map<String, String>> getStatsByItem() {
Map<InetSocketAddress, Map<String, String>> result = null;
try {
result = memcachedClient.getStats();
} catch (TimeoutException | InterruptedException | MemcachedException e) {
log.error("get error:[{}]", e.getMessage());
e.printStackTrace();
}
return result;
}


public static void main(String[] args) {
MemcachedClientBuilder builder = new XMemcachedClientBuilder(
"localhost:11211");
net.rubyeye.xmemcached.MemcachedClient memcachedClient = null;
try {
memcachedClient = builder.build();
// memcachedClient.set("hello", 0, "Hello,xmemcached");
String value = memcachedClient.get("hello");
System.out.println("hello=" + value);
// memcachedClient.delete("hello");
value = memcachedClient.get("hello");
System.out.println("hello=" + value);
} catch (MemcachedException e) {
System.err.println("MemcachedClient operation fail");
e.printStackTrace();
} catch (java.util.concurrent.TimeoutException e) {
System.err.println("MemcachedClient operation timeout");
e.printStackTrace();
} catch (InterruptedException e) {
// ignore
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
memcachedClient.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}


}
}

0 0