一、在linux安装memcached,首先按照libevent包,再安装memcached,启动时如果找不到libevent,运行LD_DEBUG=libs /usr/local/memcached/bin/memcached -v,
然后创建软链接ln -s path topath,就可以了启动了。
二、applicationContext-memcached.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
default-lazy-init="true">
<context:property-placeholder location="classpath:application.properties"/>
<bean id="memcachedPool" class="com.whalin.MemCached.SockIOPool"
factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<value>qwe</value>
</constructor-arg>
<property name="servers">
<list>
<value>${memcache.server}</value>
</list>
</property>
<property name="initConn" value="${memcache.initConn}" />
<property name="minConn" value="${memcache.minConn}" />
<property name="maxConn" value="${memcache.maxConn}" />
<property name="maxIdle" value="${memcache.maxIdle}" />
<property name="maintSleep" value="${memcache.maintSleep}" />
<property name="nagle" value="${memcache.nagle}" />
<property name="socketTO" value="${memcache.socketTO}" />
<property name="socketConnectTO" value="${memcache.socketConnectTO}"/>
</bean>
<bean id="cache" class="com.whalin.MemCached.MemCachedClient">
<constructor-arg>
<value>qwe</value>
</constructor-arg>
</bean>
</beans>
三、application.properties#memcached configue
memcache.server=127.0.0.1:11211
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maxIdle=1800000
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000
memcache.socketConnectTO=3000
四、测试:
package com.csair.memcached;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;
public class TestSpring {
public static void main(String[] args) {
String path = "applicationContext-memcached.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(path);
SockIOPool pool = context.getBean("memcachedPool",SockIOPool.class);
MemCachedClient cache = context.getBean("cache",MemCachedClient.class);
cache.add("test", "ppt1");
System.out.println(cache.get("test"));
}
}