---
title: "memcache to java"
author: "wangmored"
date: "2013-01-05T15:17:51Z"
tags: ["Java", "Spring", "memcached"]
categories: ["互联网"]
slug: "memcache-to-java"
draft: false
lastmod: "2013-06-18T16:56:00Z"
id: 1762673
url: "https://wangmored.iteye.com/blog/1762673"
---

Java中对memcache的实现有3种比较出名 memcached client for java、 spymemcached、 以及xmemcache。 较早之前的一些比较主要是集中在java memcached client和spymemcached之间 普遍的结论是：spymemcached校之java memcached client有更高的性能，但却没有java memcached client稳定。随着java memcached client新版本的发布，一些新的对比测试标明java memcached client在性能上并不比spymemcached逊色多少，再加上java memcached client被广泛使用，表现稳定，因此在一般情况下java memcached client是首选的memcache client. 还有一个由中国人编写的名为XMemcached的后起之秀 这个产品的性能表现是非常优秀的。但在使用的普遍性和项目未来的可维护上，在选型上需要慎重考虑。 综合考虑，java memcached client是一个稳妥的选择。 spring memcache 集成 applicationContext-mem.xml \[code="java"] classpath:memcache.properties memCachedPool ${memcache.server} ${memcache.initConn} ${memcache.minConn} ${memcache.maxConn} ${memcache.maintSleep} ${memcache.nagle} ${memcache.socketTO} memCachedPool \[/code] Memcache配置文件 memcache.properties文件内容如下： mcache.server=127.0.0.1\\:11211 memcache.initConn=20 memcache.minConn=10 memcache.maxConn=50 memcache.maintSleep=3000 memcache.nagle=false memcache.socketTO=3000 获得spring容器的工具类 \[code="java"] import java.util.Map; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /\** * 类: SpringContextHolder * 作者: * 时间: 2012-12-29 下午03:17:47 \*/ public class SpringContextHolder implements ApplicationContextAware { private static ApplicationContext applicationContext; /\** * 描述: 从静态变量ApplicationContext中取得Bean, * 自动转型为所赋值对象的类型. * 作者: wangmingli@zhangyue.com * 时间: 2012-12-29 下午05:35:53 \*/ @SuppressWarnings("unchecked") public static T getBean(String name) { checkApplicationContext(); return (T) applicationContext.getBean(name); } /\** * 描述: 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. * 如果有多个Bean符合Class, 取出第一个 * 作者: wangmingli@zhangyue.com * 时间: 2012-12-29 下午05:45:35 \*/ @SuppressWarnings("unchecked") public static T getBean(Class clazz) { checkApplicationContext(); Map beanMaps = applicationContext.getBeansOfType(clazz); if (beanMaps != null && !beanMaps.isEmpty()) { return (T) beanMaps.values().iterator().next(); } else { return null; } } /\** * 描述: 检查ApplicationContext是否为空 * 作者: wangmingli@zhangyue.com * 时间: 2012-12-29 下午05:42:49 \*/ private static void checkApplicationContext() { if (applicationContext == null) { throw new IllegalStateException( "applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder"); } } /\** * 描述: 取得存储在静态变量中的ApplicationContext. * 作者: wangmingli@zhangyue.com * 时间: 2012-12-29 下午05:44:49 \*/ public static ApplicationContext getApplicationContext() { checkApplicationContext(); return applicationContext; } /\** * 方法: setApplicationContext * 描述: 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. * 在配置文件中配置了bean的初始化，然后他就可以用于获得spring容器中的东西了 * @param applicationContext * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) \*/ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; } } 首先说一下ApplicationContextAware这个接口，这个接口中有一个方法： void setApplicationContext(ApplicationContext applicationContext) 下面是这个方法的简单说明： Set the ApplicationContext that this object runs in.Normally this call will be used to initialize the object. 我们在配置文件中配置了bean的初始化，然后他就可以用于获得spring容器中的东西了。 \[/code] public class MemcacheUtil { public static MemCachedClient getMemCachedClient() { return SpringContextHolder.getBean("memcachedClient"); } }