Java MemCached Window简单实现

1、下载下面附件 memcached-1.2.1-win32.zip 和 memcached-release_1.6.zip 。解压到指定目录,cmd切换进解压后的 memcached-1.2.1-win32 目录,执行 memcached.exe -d install 和 memcached.exe -l 127.0.0.1 -m 32 -d start 命令。这里只为memcached分配32M内存,此时在“服务”中可以看到 memcached Server这个服务。

 

2、创建Server端代码往 memcached 中存放数值

 

package com.neusoft;

import org.apache.log4j.BasicConfigurator;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class ServerTest {

	public static void main(String[] args) {
		BasicConfigurator.configure();
		String[] servers = {"127.0.0.1:11211"};
		SockIOPool pool = SockIOPool.getInstance();
		pool.setServers( servers );
		pool.setFailover( true );
		pool.setInitConn( 10 ); 
		pool.setMinConn( 5 );
		pool.setMaxConn( 250 );
		pool.setMaintSleep( 30 );
		pool.setNagle( false );
		pool.setSocketTO( 3000 );
		pool.setAliveCheck( true );
		pool.initialize();

		MemCachedClient memCachedClient = new MemCachedClient();
		for ( int i = 0; i < 10; i++ ) {
			memCachedClient.set( "biao.li" + i, i+" >>> Hello!" );
		}
	}
}

 

 

3、创建Client代码获取memcached 中的数值

package com.neusoft;

import org.apache.log4j.BasicConfigurator;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class ClientTest {

	public static void main(String[] args) {
		BasicConfigurator.configure();
		String[] servers = {"127.0.0.1:11211"};
		SockIOPool pool = SockIOPool.getInstance();
		pool.setServers( servers );
		pool.setFailover( true );
		pool.setInitConn( 10 ); 
		pool.setMinConn( 5 );
		pool.setMaxConn( 250 );
		pool.setMaintSleep( 30 );
		pool.setNagle( false );
		pool.setSocketTO( 3000 );
		pool.setAliveCheck( true );
		pool.initialize();

		MemCachedClient memCachedClient = new MemCachedClient();
		for ( int i = 0; i < 10; i++ ) {
			Object obj = memCachedClient.get("biao.li" + i);
			System.out.println(obj);
		}
	}
	
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值