1 修改pom.xml,添加依赖文件:
<dependency>
<groupid>com.whalin</groupid>
<artifactid>memcached-java-client</artifactid>
<version>3.0.2</version>
</dependency>
2 添加memcached-context.xml,注意要在web.xml中进行配置
<?xml version="1.0" encoding="utf-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemalocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!—注意下面的:memcache在使用的时候会用到-->
<beans:bean id="memcache" class="com.whalin.memcached.sockiopool"
factory-method="getinstance" init-method="initialize" destroy-method="shutdown">
<beans:constructor-arg>
<beans:value>memcache</beans:value>
</beans:constructor-arg>
<beans:property name="servers">
<beans:list>
<!--服务器地址-->
<beans:value>172.16.24.27:11211</beans:value>
</beans:list>
</beans:property>
<!--初始化时对每个服务器建立的连接数目-->
<beans:property name="initconn">
<beans:value>20</beans:value>
<!--每个服务器建立最小的连接数-->
<beans:property name="minconn">
<beans:value>10</beans:value>
<!--每个服务器建立最大的连接数-->
<beans:property name="maxconn">
<beans:value>50</beans:value>
<!--自查线程周期进行工作,其每次休眠时间-->
<beans:property name="maintsleep">
<beans:value>1000</beans:value>
<!--socket的参数,如果是true在写数据时不缓冲,立即发送出去-->
<beans:property name="nagle">
<beans:value>false</beans:value>
<!--socket阻塞读取数据的超时时间-->
<beans:property name="socketto">
<!-- 当memcached的连接路径出现问题的时候,代码连接的时候时间超时设置 -->
<beans:property name="socketconnectto">
<beans:value>500</beans:value>
</beans:bean>
</beans:beans>
3 在web.xml中配置:
4 编写memcachedutils,代码如下:
package com.kuman.cartoon.utils;
import java.util.date;
import org.apache.log4j.logger;
import com.whalin.memcached.memcachedclient;
/**
* @classname: memcachedutils
* @description: memcached工具类
* @author
* @date 2015-8-6
*
*/
public class memcachedutils {
private static final logger logger = logger.getlogger(memcachedutils.class);
private static memcachedclient cachedclient;
static {
if (cachedclient == null)
//括号中的名称要和配置文件memcached-context.xml中的名称一致
cachedclient = new memcachedclient("memcache");
}
private memcachedutils() {}
/**
* 向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。
*
* @param key
* 键
* @param value
* 值
* @return
*/
public static boolean set(string key, object value) {
return setexp(key, value, null);
* @param expire
* 过期时间 new date(1000*10):十秒后过期
public static boolean set(string key, object value, date expire) {
return setexp(key, value, expire);
* @param expire
private static boolean setexp(string key, object value, date expire) {
boolean flag = false;
try {
flag = cachedclient.set(key, value, expire);
} catch (exception e) {
// 记录memcached日志
logger.error("memcached set方法报错,key值:" + key + "\r\n");
}
return flag;
* 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。
public static boolean add(string key, object value) {
return addexp(key, value, null);
public static boolean add(string key, object value, date expire) {
return addexp(key, value, expire);
}
* 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。
private static boolean addexp(string key, object value, date expire) {
flag = cachedclient.add(key, value, expire);
logger.error("memcached add方法报错,key值:" + key + "\r\n");
* 仅当键已经存在时,replace 命令才会替换缓存中的键。
* @return
public static boolean replace(string key, object value) {
return replaceexp(key, value, null);
public static boolean replace(string key, object value, date expire) {
return replaceexp(key, value, expire);
private static boolean replaceexp(string key, object value, date expire) {
flag = cachedclient.replace(key, value, expire);
logger.error("memcached replace方法报错,key值:" + key + "\r\n");
* get 命令用于检索与之前添加的键值对相关的值。
public static object get(string key) {
object obj = null;
obj = cachedclient.get(key);
logger.error("memcached get方法报错,key值:" + key + "\r\n");
return obj;
* 删除 memcached 中的任何现有值。
public static boolean delete(string key) {
return deleteexp(key, null);
* 删除 memcached 中的任何现有值。
public static boolean delete(string key, date expire) {
return deleteexp(key, expire);
private static boolean deleteexp(string key, date expire) {
flag = cachedclient.delete(key, expire);
logger.error("memcached delete方法报错,key值:" + key + "\r\n");
* 清理缓存中的所有键/值对
public static boolean flashall() {
flag = cachedclient.flushall();
logger.error("memcached flashall方法报错\r\n");
/*@test
public void testmemcachedspring() {
memcachedutils.set("aa", "bb", new date(1000 * 60));
object obj = memcachedutils.get("aa");
system.out.println("***************************");
system.out.println(obj.tostring());
}*/
}
5 springmvc中调用的方式:
@requestmapping(value = "/toindex")
public string toindex(model model) {
//方法一,这种不建议使用
//memcachedclient memcachedclient = new memcachedclient("memcache");
//memcachedclient.set("name", "simple");
//system.out.println(memcachedclient.get("name"));
//方法二,建议这种
memcachedutils.set("name", "simple");
string name = (string)memcachedutils.get("name");
system.out.println(name);
return "/admin/index";
}