`

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");
}
}
分享到:
评论
1 楼 dengshh 2015-08-19  
[flash=200,200][url][img][list]
[*]
引用
引用
[u][b][i][b][i][u]
引用
[list]
[*][*][img][url][flash=200,200][*][*][*][*]
[/flash][/url][/img] [*][/list]
[/u][/i][/b]              [/i][/b][/u]

[/list][/img][/url][/flash]

相关推荐

    java连memcached的驱动 spymemcached-2.10.3.jar

    System.out.println("Connection to server sucessful."); // 添加数据 Future fo = mcc.set("runoob", 900, "Free Education"); // 打印状态 System.out.println("set status:" + fo.get()); // 输出 ...

    基于Google.App.Engine(GAE)的Java和GWT应用开发.pdf

    To build interesting and interactive web applications developers are turning to Java. However, building and deploying scalable web applications using Google Web Toolkit and Google App Engine for Java...

    高性能高并发服务器架构大全

    美国Facebok.com,中国Yeejee.com,日本mixi.jp均采用开源分布式缓存服务器Memcache 52 3,图片缓存和加 52  memcached+squid+apache deflate解决网站大访问量问题 52  FeedBurner:基于MySQL和JAVA的可扩展Web...

    手动清除memcached缓存方法

    1.查memcache状态 /usr/bin/perl /usr/local/src/memcached-1.4.5/scripts/memcached-tool localhost:11211 或者 telnet localhost 11211 2.手动清除memcached测试 1). flush_all echo “flush_all” | nc localhost...

    PHP5 完整官方 中文教程

    Memcache — Memcache Functions mhash — Mhash Functions Mimetype — Mimetype Functions Ming (flash) — Ming functions for Flash Misc. — Miscellaneous Functions mnoGoSearch — mnoGoSearch Functions MS...

    PHP5中文参考手册

    Memcache — Memcache Functions mhash — Mhash Functions Mimetype — Mimetype Functions Ming (flash) — Ming functions for Flash Misc. — Miscellaneous Functions mnoGoSearch — mnoGoSearch Functions MS...

    PHP手册2007整合中文版

    Memcache Functions LXXXVII. Mhash Functions LXXXVIII. Mimetype Functions LXXXIX. Ming functions for Flash XC. Miscellaneous Functions XCI. mnoGoSearch Functions XCII. Microsoft SQL Server Functions ...

    PHP官方手册中文版

    Memcache Functions LXXXVII. Mhash Functions LXXXVIII. Mimetype Functions LXXXIX. Ming functions for Flash XC. Miscellaneous Functions XCI. mnoGoSearch Functions XCII. Microsoft SQL Server ...

    PHP5 开发手册 简体中文手册

    45. PDO Driver How-To 46. Zend API:深入 PHP 内核 47. 扩展 PHP 3 VIII. FAQ:常见问题 48. 一般信息 49. 邮件列表 50. 获取 PHP 51. 数据库问题 52. 安装常见问题 53. 编译问题 54. 使用 PHP 55. PHP 和 HTML 56...

    php帮助文档,php。chm,php必备的中文手册

    45. PDO Driver How-To 46. Zend API:深入 PHP 内核 47. 扩展 PHP 3 VIII. FAQ:常见问题 48. 一般信息 49. 邮件列表 50. 获取 PHP 51. 数据库问题 52. 安装常见问题 53. 编译问题 54. 使用 PHP 55. PHP 和 HTML 56...

Global site tag (gtag.js) - Google Analytics