xmemcached 整合spring项目

1 篇文章 0 订阅
1 篇文章 0 订阅

Windows下的Memcache安装
1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached
2. 在终端(也即cmd命令界面)下输入 'c:\memcached\memcached.exe -d install' 安装
3. 再输入: 'c:\memcached\memcached.exe -d start' 启动。NOTE: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。

 

 可参考:http://snowolf.iteye.com/blog/1471805

 

XMemcached与Spring集成 

 

新建 spring-xmemcached.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
		<bean id="memcachedClientBuilder" name="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
		    <!-- XMemcachedClientBuilder have two arguments.First is server list,and second is weights array. -->
				<constructor-arg>
						<list>
								<bean class="java.net.InetSocketAddress">
										<constructor-arg>
										    <value>localhost</value>
										</constructor-arg>
										<constructor-arg>
										    <value>11211</value>
										</constructor-arg>
								</bean>
						</list>
				</constructor-arg>
		
				<constructor-arg>
						<list>
						    <value>1</value>
						</list>
				</constructor-arg>
			
			  <property name="connectionPoolSize" value="30"></property>
			  
				<property name="commandFactory">
				    <bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
				</property>
				
				<property name="sessionLocator">
				    <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
				</property>
				
				<property name="transcoder">
				    <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
				</property>
		</bean>
	 
	 <!-- Use factory bean to build memcached client -->
	 <bean id="memcachedClient" name="memcachedClient" factory-bean="memcachedClientBuilder" factory-method="build" destroy-method="shutdown"/>
</beans>

 并导入到applicationContent.xml中

<import resource="classpath:spring/spring-xmemcached.xml" />

 编写 Xmemcache 帮助类:

private static final int defaultWeight = 0;													//	默认权重
	
	private MemcachedClient client ;
	
	public MemcachedClient getClient() {
		return client;
	}

	public void setClient(MemcachedClient client) {
		this.client = client;
	}

	private static final Map<String, String> keyMap = new HashMap<String, String>();			//	可存储的key值
	
	static {
		Field[] fields = CacheConstant.class.getDeclaredFields();
		if(null != fields && fields.length > 0){
			for(Field field : fields){
				keyMap.put(field.getName(), null);
			}
		}			
	}
	
	/**
	 * 校验状态
	 * 
	 * @return
	 */
	private boolean checkClientStatus(){
		boolean flag = false;
		if(null != client && !client.isShutdown()){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 校验是否存在key值
	 * 
	 * @param key
	 * @return
	 */
	private boolean checkKey(String key){
		return keyMap.containsKey(key);
	}
	
	/**
	 * flushAll 清空所有,请谨慎使用
	 * 
	 */
	public void flushAll(){
		if(checkClientStatus()){
			try {
				client.flushAll();
			} catch (TimeoutException e) {
				logger.error("TimeoutException flushAll :".concat(e.getMessage()));
			} catch (InterruptedException e) {
				logger.error("InterruptedException flushAll :".concat(e.getMessage()));
			} catch (MemcachedException e) {
				logger.error("MemcachedException flushAll :".concat(e.getMessage()));
			}
		}
	}
	
	/**
	 * 删除某个key
	 * 
	 * @param key
	 * @return
	 */
	public boolean deleteKey(String key){
		boolean flag = false;
		if(checkClientStatus() && checkKey(key)){
			try {
				flag = client.delete(key);
			} catch (TimeoutException e) {
				logger.error("TimeoutException deleteKey key=".concat(key).concat(" :").concat(e.getMessage()));
			} catch (InterruptedException e) {
				logger.error("InterruptedException deleteKey key=".concat(key).concat(" :").concat(e.getMessage()));
			} catch (MemcachedException e) {
				logger.error("MemcachedException deleteKey key=".concat(key).concat(" :").concat(e.getMessage()));
			}
		}
		return flag;
	}
	
	/**
	 * 存储值
	 * 
	 * @param key
	 * @param val
	 * @return
	 */
	public boolean setVal(String key , Object val){
		boolean flag = false;
		if(checkClientStatus() && checkKey(key) && null != val){
			try {
				Object obj = client.get(key);
				if(null == obj){
					flag = client.set(key, defaultWeight, val);
				}else{
					flag = client.replace(key, defaultWeight, val);
				}
			} catch (TimeoutException e) {
				logger.error("TimeoutException setVal key=".concat(key).concat(" :").concat(e.getMessage()));
			} catch (InterruptedException e) {
				logger.error("InterruptedException setVal key=".concat(key).concat(" :").concat(e.getMessage()));
			} catch (MemcachedException e) {
				logger.error("MemcachedException setVal key=".concat(key).concat(" :").concat(e.getMessage()));
			}
		}
		return flag;
	}
	
	/**
	 * 获取值
	 * 
	 * @param key
	 * @return
	 */
	public Object getVal(String key){
		if(checkClientStatus() && checkKey(key)){
			try {
				return client.get(key);
			} catch (TimeoutException e) {
				logger.error("TimeoutException getVal key=".concat(key).concat(" :").concat(e.getMessage()));
			} catch (InterruptedException e) {
				logger.error("InterruptedException getVal key=".concat(key).concat(" :").concat(e.getMessage()));
			} catch (MemcachedException e) {
				logger.error("MemcachedException getVal key=".concat(key).concat(" :").concat(e.getMessage()));
			}
		}
		return null;
	}

 

ok啦,就可以使用了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值