memcache to java

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


<?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="properties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:memcache.properties</value>
</list>
</property>
</bean>

<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
factory-method="getInstance" init-method="initialize" destroy-method="shutDown">

<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>

<property name="servers">
<list>
<value>${memcache.server}</value>
</list>
</property>

<property name="initConn">
<value>${memcache.initConn}</value>
</property>

<property name="minConn">
<value>${memcache.minConn}</value>
</property>

<property name="maxConn">
<value>${memcache.maxConn}</value>
</property>

<property name="maintSleep">
<value>${memcache.maintSleep}</value>
</property>

<property name="nagle">
<value>${memcache.nagle}</value>
</property>

<property name="socketTO">
<value>${memcache.socketTO}</value>
</property>
</bean>

<bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
<constructor-arg>
<value>memCachedPool</value>
</constructor-arg>
</bean>

<!--添加bean去初始化我们自己的一个spring工具类 -->
<bean id="springContextHolder" class="com.support.cps.utils.SpringContextHolder"/>

</beans>





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容器的工具类


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> 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> T getBean(Class<T> 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容器中的东西了。




public class MemcacheUtil {
public static MemCachedClient getMemCachedClient() {
return SpringContextHolder.getBean("memcachedClient");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值