`
lveyo
  • 浏览: 915354 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring调用memcached client for java

阅读更多
memcached client for java客户端API:memcached client for java
网址:http://www.whalin.com/memcached

调用测试类
MClient.java
package bcndyl.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.danga.MemCached.MemCachedClient;

public class MClient {
	
	public static void main(String[] args){
		
		ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");
		MemCachedClient mc = (MemCachedClient)ctx.getBean("memcachedClient");
		for(int i=0; i<100; i++){
			//try{Thread.sleep(2000);}catch(Exception e){}
			mc.set("key"+i, "value"+i);
		}
		try{Thread.sleep(5000);}catch(Exception e){}
		for(int i=0; i<100; i++){
			System.out.println("get "+i+" value "+mc.get("key"+i));
		}
	}

}


spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"
		init-method="initialize" destroy-method="shutDown">
		<constructor-arg><value>neeaMemcachedPool</value></constructor-arg>
		<property name="servers">
			<list>
				<value>192.168.227.20:12111</value>
				<value>192.168.227.20:12112</value>
			</list>
		</property>
		<property name="initConn"><value>20</value></property>
		<property name="minConn"><value>10</value></property>
		<property name="maxConn"><value>50</value></property>
		<property name="maintSleep"><value>30</value></property>
		<property name="nagle"><value>false</value></property>
		<property name="socketTO"><value>3000</value></property>
		<!--  
		<property name="initConn"><value></value></property>
		<property name="initConn"><value></value></property>-->
	</bean>
	
	<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
		<constructor-arg><value>neeaMemcachedPool</value></constructor-arg>
		<property name="compressEnable"><value>true</value></property>
		<property name="compressThreshold"><value>4096</value></property>
	</bean>

</beans>


经过测试,个人感觉,这个java的memcached API比spymemcached要好用,可以让spring来维护这个memcached的connection连接池
5
2
分享到:
评论
8 楼 colbybobo 2014-02-20  
fangzhouxing 写道
出现下列错误:

[DEBUG,danga.MemCached.SockIOPool] ++++ initializing pool with following settings:
[DEBUG,danga.MemCached.SockIOPool] ++++ initial size: 20
[DEBUG,danga.MemCached.SockIOPool] ++++ min spare   : 10
[DEBUG,danga.MemCached.SockIOPool] ++++ max spare   : 50
[DEBUG,danga.MemCached.SockIOPool] ++++ initializing internal hashing structure for consistent hashing
[DEBUG,danga.MemCached.SockIOPool] ++++ added 127.0.0.1:12111 to server bucket
[DEBUG,danga.MemCached.SockIOPool] +++ creating initial connections (20) for host: 127.0.0.1:12111
[ERROR,danga.MemCached.SockIOPool] ++++ failed to get SockIO obj for: 127.0.0.1:12111
[ERROR,danga.MemCached.SockIOPool] Connection refused: no further information
java.net.ConnectException: Connection refused: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:574)
at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)
at com.danga.MemCached.SockIOPool$SockIO.getSocket(SockIOPool.java:1612)
at com.danga.MemCached.SockIOPool$SockIO.<init>(SockIOPool.java:1585)
at com.danga.MemCached.SockIOPool.createSocket(SockIOPool.java:782)
at com.danga.MemCached.SockIOPool.populateBuckets(SockIOPool.java:671)
at com.danga.MemCached.SockIOPool.initialize(SockIOPool.java:634)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1414)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1375)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:543)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:236)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.jetty.Server.doStart(Server.java:222)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:286)
at com.google.gwt.dev.HostedMode.doStartUpServer(HostedMode.java:367)
at com.google.gwt.dev.HostedModeBase.startUp(HostedModeBase.java:588)
at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:395)
at com.google.gwt.dev.HostedMode.main(HostedMode.java:232)
[DEBUG,danga.MemCached.SockIOPool] ++++ ignoring dead host: 127.0.0.1:12111 for 1000 ms
[ERROR,danga.MemCached.SockIOPool] ++++ failed to create connection to: 127.0.0.1:12111 -- only 0 created.
[DEBUG,danga.MemCached.SockIOPool] ++++ Starting self maintenance....
[DEBUG,danga.MemCached.SockIOPool] +++ ending self maintenance.
[DEBUG,danga.MemCached.SockIOPool] ++++ Starting self maintenance....

memecache服务没启动吧?
7 楼 we_sky 2011-12-16  
6 楼 wasw100 2011-08-12  
Bean property 'compressEnable' is not writable or has an invalid setter method
5 楼 diandian 2011-03-26  
hi lveyo.
我采用你这样的方式来做,但程序有时会提示:com.schooner.MemCached.SchoonerSockIOPool Sat Mar 26 17:57:41 GMT+08:00 2011 - attempting to get SockIO from uninitialized pool!
不知你那里有没有遇到过,如果知道解决办法,请赐教.
多谢!
4 楼 lveyo 2011-03-04  
diandian 写道
for(int i=0; i<100; i++){ 
             //try{Thread.sleep(2000);}catch(Exception e){} 
             mc.set("keya"+i, "value"+i); 

try{Thread.sleep(5000);}catch(Exception e){} 
for(int i=0; i<100; i++){ 
System.out.println("get "+i+" value "+mc.get("key"+i)); 

大哥,你取的KEY不对哦。
但还是要谢谢分享!


改过来了!谢谢提醒!
3 楼 diandian 2011-03-01  
for(int i=0; i<100; i++){ 
             //try{Thread.sleep(2000);}catch(Exception e){} 
             mc.set("keya"+i, "value"+i); 

try{Thread.sleep(5000);}catch(Exception e){} 
for(int i=0; i<100; i++){ 
System.out.println("get "+i+" value "+mc.get("key"+i)); 

大哥,你取的KEY不对哦。
但还是要谢谢分享!
2 楼 fangzhouxing 2009-03-01  
自己解答:端口号应该为 11211!
1 楼 fangzhouxing 2009-03-01  
出现下列错误:

[DEBUG,danga.MemCached.SockIOPool] ++++ initializing pool with following settings:
[DEBUG,danga.MemCached.SockIOPool] ++++ initial size: 20
[DEBUG,danga.MemCached.SockIOPool] ++++ min spare   : 10
[DEBUG,danga.MemCached.SockIOPool] ++++ max spare   : 50
[DEBUG,danga.MemCached.SockIOPool] ++++ initializing internal hashing structure for consistent hashing
[DEBUG,danga.MemCached.SockIOPool] ++++ added 127.0.0.1:12111 to server bucket
[DEBUG,danga.MemCached.SockIOPool] +++ creating initial connections (20) for host: 127.0.0.1:12111
[ERROR,danga.MemCached.SockIOPool] ++++ failed to get SockIO obj for: 127.0.0.1:12111
[ERROR,danga.MemCached.SockIOPool] Connection refused: no further information
java.net.ConnectException: Connection refused: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:574)
at sun.nio.ch.SocketAdaptor.connect(SocketAdaptor.java:100)
at com.danga.MemCached.SockIOPool$SockIO.getSocket(SockIOPool.java:1612)
at com.danga.MemCached.SockIOPool$SockIO.<init>(SockIOPool.java:1585)
at com.danga.MemCached.SockIOPool.createSocket(SockIOPool.java:782)
at com.danga.MemCached.SockIOPool.populateBuckets(SockIOPool.java:671)
at com.danga.MemCached.SockIOPool.initialize(SockIOPool.java:634)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1414)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1375)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:543)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
at com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload.doStart(JettyLauncher.java:236)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.jetty.Server.doStart(Server.java:222)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:286)
at com.google.gwt.dev.HostedMode.doStartUpServer(HostedMode.java:367)
at com.google.gwt.dev.HostedModeBase.startUp(HostedModeBase.java:588)
at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:395)
at com.google.gwt.dev.HostedMode.main(HostedMode.java:232)
[DEBUG,danga.MemCached.SockIOPool] ++++ ignoring dead host: 127.0.0.1:12111 for 1000 ms
[ERROR,danga.MemCached.SockIOPool] ++++ failed to create connection to: 127.0.0.1:12111 -- only 0 created.
[DEBUG,danga.MemCached.SockIOPool] ++++ Starting self maintenance....
[DEBUG,danga.MemCached.SockIOPool] +++ ending self maintenance.
[DEBUG,danga.MemCached.SockIOPool] ++++ Starting self maintenance....

相关推荐

    Spring memcached 源码

    首先,Spring Memcached的集成主要是通过`spring-memcached`库实现的,它提供了对Memcached服务器的客户端支持,使得Spring应用能够方便地调用Memcached服务。在源码中,`OrgManagerServiceImpl`类是一个示例,展示...

    simple-spring-memcached集成memcache

    public MemcachedCacheManager cacheManager(MemcachedClient memcachedClient) { MemcachedCacheManager cacheManager = new MemcachedCacheManager(); cacheManager.setMemcachedClient(memcachedClient); ...

    memcaChed java client jar包

    在Java应用中使用Memcached客户端,首先需要将`memcached.jar`添加到项目的类路径中。这个JAR文件包含了所有必要的类和方法,使得开发者能够通过Java代码创建连接、存储和检索缓存的数据。例如,你可以使用流行的...

    spring集成memcached

    1. **MemcachedClient for Java**:这是一种较早出现的客户端,以其稳定性和广泛应用而著称。 2. **SpyMemcached**:相比于MemcachedClient for Java,它在性能上有一定的优化。 3. **XMemcached**:该客户端在并发...

    simple-spring-memcached(ssm) 和spring的集成

    3. **配置 Spring**:在 `Spring` 配置文件(如 `beans.xml`)中,声明 `MemcachedClient` 实例,并指定配置信息。 4. **创建缓存注解**:`SSM` 提供了 `@Cacheable`、`@CacheEvict` 和 `@CachePut` 等注解,用于...

    memcached 的简单java实例

    MemcachedClient client = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses("127.0.0.1:11211")); ``` 3. **存储数据** 使用`set`方法将键值对存储到Memcached。 ```java String ...

    java使用memcached的实例

    本文详细介绍了如何在Java项目中使用Memcached进行数据缓存,包括Spring框架下的配置方式以及具体的代码实现。通过理解和掌握这些知识点,开发者可以有效地利用Memcached提升JavaWeb应用的性能和效率。

    Memcached-Java-Client-release_2.6.1.zip

    - 演示如何在Spring框架中配置和使用Memcached,结合Spring的AOP特性实现自动缓存管理。 通过以上内容,我们对Java与Memcached的整合有了全面了解。正确使用Memcached-Java-Client不仅可以提高应用性能,还能降低...

    Spring与memcached整合

    &lt;bean id="memcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean"&gt; ``` 3. **创建缓存操作接口**: 设计一个缓存操作接口,例如`CacheService`,其中包含`get`、`set`、`delete`等基本...

    spring memcached 所需jar

    2. **配置Memcached**:在Spring的配置文件中,我们需要声明一个`MemcachedClient` bean,指定服务器地址和连接池配置。例如: ```xml &lt;bean id="memcachedClient" class=...

    spring整合memcached

    Spring提供了一套完整的集成机制,使得开发者能够方便地在Java应用中使用Memcached。 整合步骤通常分为以下几个阶段: 1. **添加依赖**:在项目中引入Memcached和Spring的对应依赖。如果你使用的是Maven,可以在...

    (SSM框架)memcached整合Spring基于Cache注解.

    在Spring的配置文件中,需要定义一个`MemcachedCacheManager`,并将配置好的MemcachedClient注入。同时,通过`&lt;cache:annotation-driven&gt;`标签启用基于注解的缓存处理。 7. **MyBatis与Memcached结合**: 虽然...

    java 使用memcached以及spring 配置memcached完整实例代码

    在Java开发中,使用Memcached作为缓存系统可以显著提高应用程序的性能,因为它是一个高性能的分布式内存对象缓存系统。Memcached能够存储和检索小的数据片段,如字符串、JSON对象或序列化的Java对象,减少对数据库的...

    java连接memcached示例代码

    下面我们将详细探讨如何在Java中使用Memcached以及相关知识点。 首先,我们需要引入一个Java库来与Memcached通信,例如spymemcached或xmemcached。这两个库提供了Java客户端,方便我们进行API调用。这里以...

    simple-spring-memcached统一缓存的使用实例[整理].pdf

    &lt;bean id="memcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean"&gt; &lt;!-- 配置Memcached服务器地址等信息 --&gt; &lt;!-- 配置Spring的cache管理器 --&gt; &lt;!-- 其他配置... --&gt; &lt;!-- ...

    Spring_Memcached:使用与Spring集成的Memcached

    使用Spring Boot进行Memcached为什么要使用Memcached?您是否正在为每个事务与数据库进行交互? 然后,您必须应用缓存机制,以避免多次数据库调用和更快的响应。一个免费的开源内存中缓存系统,可通过减少数据库负载...

    SSM与memcached整合项目Spring Cache

    2. **配置Spring Cache**:在Spring的配置文件中,我们需要定义一个`CacheManager` bean,指定使用memcached作为缓存后端。这通常通过`MemcachedCacheManager`类实现,配置包括memcached服务器地址、超时时间等参数...

    memcached+spring

    public MemcachedClient memcachedClient() throws IOException { MemcachedConnectionFactoryBean factory = new MemcachedConnectionFactoryBean(); factory.setServers(Arrays.asList("localhost:11211")); //...

Global site tag (gtag.js) - Google Analytics