|
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import com.whalin.MemCached.MemCachedClient;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @ClassName: SpringMemcachedTest
* @Description: TODO(这里用一句话描述这个类的作用)
* @author linhz
* @date 2014-8-22 下午05:25:51
*
*/
public class SpringMemcachedTest {
private ApplicationContext app;
private MemCachedClient memcachedClient;
@Before
public void init() {
app = new ClassPathXmlApplicationContext("spring-memcached.xml");
memcachedClient = (MemCachedClient) app.getBean("memcachedClient");
}
@Test
public void test() {
try {
// 设置/获取
memcachedClient.set("philip", "set/get");
assertEquals("set/get", memcachedClient.get("philip"));
// 替换
memcachedClient.replace("philip", "replace");
assertEquals("replace", memcachedClient.get("philip").toString());
// 移除
memcachedClient.delete("philip");
assertNull(memcachedClient.get("philip"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
spring-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:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" xsi:schemaLocation ="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id= "propertyConfigurer1" class ="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name ="locations" > <list> <value> classpath:xmemcached.properties</value > </list> </property> <property name ="ignoreUnresolvablePlaceholders" value="true" /> </bean>
<bean id= "memcachedPool" class ="com.whalin.MemCached.SockIOPool" factory-method ="getInstance" init-method= "initialize" destroy-method ="shutDown" > <constructor-arg> <value> neeaMemcachedPool</value > </constructor-arg> <property name ="servers" > <list> <value> 132.96.27.25:11211</value > <value> 132.96.27.25:11212</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> </bean>
<bean id= "memcachedClient" class ="com.whalin.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> 版权声明:本文为博主原创文章,未经博主允许不得转载。 |
|
|
|
|
|
|