mina 实现与memcached 连接工具

阅读更多
前段时间学习了mina框架.就确定写一个小实例玩一下.
看到不少开源第三方的memcached连接工具(xmemcached),都采用NIO方式.
于是就有了一个想法用mina作为底层NIO框架与memcached连接.
希望能给学习Mina的新人帮助.
能力有限项目还没有很好完善.不过基本的相关操作已实现.
如果大家有兴趣和我一起完善这个项目,请站内信联络.
源码(svn):
http://asksite.googlecode.com/svn/trunk/minamemcahed

测试类及相关API调用
public class CacheTest {

	static MinaConnect mc = null;

	static LinkedList filterList = new LinkedList();

	@BeforeClass
	public static void init() {

		List addreeList = new ArrayList();

		//memcached server ip address and port
		InetSocketAddress address1 = new InetSocketAddress("127.0.0.1", 11211);

		InetSocketAddress address2 = new InetSocketAddress("192.168.1.105",11212);

		//add memcached server
		addreeList.add(new MemCachedAddress(address1, 3));

		addreeList.add(new MemCachedAddress(address2, 7));

		mc = new MinaConnect(addreeList);

		//start connect
		mc.connect();
	}

	@AfterClass
	public static void destory() {
		mc.shutDown();
	}

	@Test
	@Ignore
	public void testSet() throws Exception {
		User user = new User();
		user.setId(1);
		user.setName("tina");
		boolean result = mc.set("name", 0, user);
		System.out.println(result);

	}

	@Test
	public void testGet() throws Exception {
		User user = (User) mc.get("name");
		System.out.println(user.getName());
		/*
		 * user = (User) mc.get("user2"); System.out.println(user.getName());
		 * user = (User) mc.get("user3"); System.out.println(user.getName());
		 */
	}

	@Test
	@Ignore
	public void testReplace() throws Exception {
		User user = new User();
		user.setId(1);
		user.setName("replacetest");
		boolean result = mc.replace("test2", 10000, user);
		System.out.println(result);
	}

	@Test
	@Ignore
	public void testDelete() throws Exception {
		boolean result = mc.delete("test2");
		System.out.println(result);
	}

	@Test
	@Ignore
	public void testIncr() throws Exception {
		long i = mc.incr("incrtest", 2);
		System.out.println(i);
	}

	
	@Test
	@Ignore
	public void testGets() throws Exception {

		User[] users = new User[10];

		for (int i = 0; i < users.length; i++) {
			users[i] = new User();
			users[i].setId(i);
			users[i].setName("test" + i);

			if (i == 3) {
				InetSocketAddress address2 = new InetSocketAddress(
						"192.168.1.105", 11212);
				mc.removeServer(address2);
			}

			boolean result = mc.set("user" + i, 0, users[i]);

			System.out.println(result);
		}

		List list = new ArrayList();

		for (int i = 0; i < 10; i++) {
			list.add("user" + i);
		}

		Map map = mc.gets(list);

		Iterator keySet = map.keySet().iterator();

		while (keySet.hasNext()) {
			User user = (User) map.get(keySet.next());
			System.out.println(user.getName());
		}
	}

	@Test
	@Ignore
	public void testSomeNoreplay() throws Exception {

		
		User pan = (User) mc.get("test");

		System.out.println(pan.getName());

	}

	
}


spring 定义服务
	

		
			
				127.0.0.1:11211
			
		
		
		
		    
		        3
		    
		
	


相关jar依赖 mina 和 mina依赖的jar 包
jar包在附件中
  • minamemcached.jar (50.5 KB)
  • 下载次数: 154

你可能感兴趣的:(Mina,memcached,JBoss,Comet,SVN)