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";
}