首页>代码>springside的showcase>/showcase/src/main/java/org/springside/examples/showcase/demos/cache/memcached/MemcachedDemo.java
package org.springside.examples.showcase.demos.cache.memcached;

import static org.junit.Assert.*;

import java.util.Map;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springside.modules.cache.memcached.SpyMemcachedClient;
import org.springside.modules.test.spring.SpringContextTestCase;

import com.google.common.collect.Lists;

@ContextConfiguration(locations = { "/cache/applicationContext-memcached.xml", "/applicationContext.xml" })
public class MemcachedDemo extends SpringContextTestCase {

	@Autowired
	private SpyMemcachedClient spyMemcachedClient;

	@Test
	public void normal() {

		String key = "consumer:1";
		String value = "admin";

		spyMemcachedClient.set(key, 60 * 60 * 1, value);

		String result = spyMemcachedClient.get(key);
		assertEquals(value, result);

		spyMemcachedClient.delete(key);
		result = spyMemcachedClient.get(key);
		assertNull(result);
	}

	@Test
	public void safeDelete() {
		String key = "consumer:1";
		spyMemcachedClient.set(key, 60, "admin");
		boolean result = spyMemcachedClient.safeDelete(key);
		assertTrue(result);

		result = spyMemcachedClient.safeDelete("consumer:1");
		assertFalse(result);
	}

	@Test
	public void getBulk() {

		String key1 = "consumer:1";
		String value1 = "admin";

		String key2 = "consumer:2";
		String value2 = "calvin";

		String key3 = "invalidKey";

		spyMemcachedClient.set(key1, 60 * 60 * 1, value1);
		spyMemcachedClient.set(key2, 60 * 60 * 1, value2);

		Map<String, String> result = spyMemcachedClient.getBulk(Lists.newArrayList(key1, key2));
		assertEquals(value1, result.get(key1));
		assertEquals(value2, result.get(key2));
		assertNull(result.get(key3));
	}

	@Test
	public void incr() {
		String key = "incr_key";

		//注意,incr返回的数值使用long表达
		long result = spyMemcachedClient.incr(key, 2, 1);
		assertEquals(1, result);
		//注意,get返回的数值使用字符串表达
		assertEquals("1", spyMemcachedClient.get(key));

		result = spyMemcachedClient.incr(key, 2, 1);
		assertEquals(3, result);
		assertEquals("3", spyMemcachedClient.get(key));

		key = "set_and_incr_key";
		//注意,set中的数值必须使用字符串,后面的incr操作结果才会正确.
		spyMemcachedClient.set(key, 60 * 60 * 1, "1");
		result = spyMemcachedClient.incr(key, 2, 1);
		assertEquals(3, result);
	}
}
最近下载更多
bannerwang  LV2 2018年10月30日
wkc  LV21 2018年5月13日
dachoumomo  LV12 2018年2月2日
yhlcrl  LV1 2016年3月10日
微微上翘  LV23 2015年11月30日
zzlsrj  LV10 2015年3月14日
骑着猪猪去逛街  LV32 2013年12月27日
pauldu1104  LV1 2013年7月14日
jaodge  LV1 2013年6月24日
cxw  LV1 2013年5月15日
最近浏览更多
酒瓯224656565 2019年10月14日
暂无贡献等级
shuiyizq  LV5 2019年7月30日
chengqiang  LV13 2019年5月28日
bannerwang  LV2 2018年10月30日
wkc  LV21 2018年5月13日
dachoumomo  LV12 2018年2月2日
zyl  LV34 2017年10月23日
sdomysky1234  LV6 2017年2月28日
xianggang  LV1 2017年1月8日
danelumax 2016年12月20日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友