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); } } }
?
?