---
title: "spring调用memcached client for java"
author: "Alex_hyts"
date: "2016-05-06T15:45:18Z"
tags: []
categories: ["编程语言"]
slug: "spring-memcached-client-for-java"
draft: false
lastmod: "2016-05-06T15:45:18Z"
id: 2296353
url: "https://alex4java.iteye.com/blog/2296353"
---

### 转载：[spring调用memcached client for java](http://lveyo.iteye.com/blog/246564)

**memcached client for java客户端API：memcached client for java** 

网址：http://www.whalin.com/memcached 

调用测试类   
MClient.java 

Java代码  [![收藏代码](http://lveyo.iteye.com/images/icon_star.png)]()

01. package bcndyl.test;
02. import org.springframework.context.ApplicationContext;
03. import org.springframework.context.support.FileSystemXmlApplicationContext;
04. import com.danga.MemCached.MemCachedClient;
05. public class MClient {
06. public static void main(String\[] args){
07. ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");
08. MemCachedClient mc = (MemCachedClient)ctx.getBean("memcachedClient");
09. for(int i=0; i&lt;100; i++){
10. //try{Thread.sleep(2000);}catch(Exception e){}
11. mc.set("key"+i, "value"+i);
12. }
13. try{Thread.sleep(5000);}catch(Exception e){}
14. for(int i=0; i&lt;100; i++){
15. System.out.println("get "+i+" value "+mc.get("key"+i));
16. }
17. }
18. }

spring配置文件 

Xml代码  [![收藏代码](http://lveyo.iteye.com/images/icon_star.png)]()

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans
03. xmlns="http://www.springframework.org/schema/beans"
04. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt;
06. &lt;bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"
07. init-method="initialize" destroy-method="shutDown"&gt;
08. &lt;constructor-arg&gt;&lt;value&gt;neeaMemcachedPool&lt;/value&gt;&lt;/constructor-arg&gt;
09. &lt;property name="servers"&gt;
10. &lt;list&gt;
11. &lt;value&gt;192.168.227.20:12111&lt;/value&gt;
12. &lt;value&gt;192.168.227.20:12112&lt;/value&gt;
13. &lt;/list&gt;
14. &lt;/property&gt;
15. &lt;property name="initConn"&gt;&lt;value&gt;20&lt;/value&gt;&lt;/property&gt;
16. &lt;property name="minConn"&gt;&lt;value&gt;10&lt;/value&gt;&lt;/property&gt;
17. &lt;property name="maxConn"&gt;&lt;value&gt;50&lt;/value&gt;&lt;/property&gt;
18. &lt;property name="maintSleep"&gt;&lt;value&gt;30&lt;/value&gt;&lt;/property&gt;
19. &lt;property name="nagle"&gt;&lt;value&gt;false&lt;/value&gt;&lt;/property&gt;
20. &lt;property name="socketTO"&gt;&lt;value&gt;3000&lt;/value&gt;&lt;/property&gt;
21. &lt;!--
22. &lt;property name="initConn"&gt;&lt;value&gt;&lt;/value&gt;&lt;/property&gt;
23. &lt;property name="initConn"&gt;&lt;value&gt;&lt;/value&gt;&lt;/property&gt;--&gt;
24. &lt;/bean&gt;
25. &lt;bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient"&gt;
26. &lt;constructor-arg&gt;&lt;value&gt;neeaMemcachedPool&lt;/value&gt;&lt;/constructor-arg&gt;
27. &lt;property name="compressEnable"&gt;&lt;value&gt;true&lt;/value&gt;&lt;/property&gt;
28. &lt;property name="compressThreshold"&gt;&lt;value&gt;4096&lt;/value&gt;&lt;/property&gt;
29. &lt;/bean&gt;
30. &lt;/beans&gt;

经过测试，个人感觉，这个java的memcached API比spymemcached要好用，可以让spring来维护这个memcached的connection连接池