首先我在自已的本机上安装了一个memcached 服务器,具体的安装说明可以参考我的上一篇博客,在这里就不介绍了。在这里主要介绍了一下java 与memcached客户端使用。
在网上看了下,目前有几种比较流行的memcached client框架,我选了一个综合性能也是最早出来的一款Memcached-Java-Client
在
http://code.google.com/p/memcache-client-forjava/downloads/list
这个网站进行上载memcached-Java_Client的相关二进制包,并将此二进制包加入到自已的应用环境中便可。
以下是我写的一相简单的测试例子。
package com.ht; import com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient; import com.alisoft.xplatform.asf.cache.memcached.client.SockIOPool; public class TestMemcached { public static void main(String[] args) { String[] server = {"192.168.83.1:12345"}; //初始化SockIOPool,管理memcached连接池 SockIOPool pool = SockIOPool.getInstance(); pool.setServers(server); pool.setFailover(true); pool.setInitConn(10); pool.setMinConn(5); pool.setMaxConn(100); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(3000); pool.setAliveCheck(true); pool.initialize(); //建立memcachedclient 对象 MemCachedClient client = new MemCachedClient(); for(int i=0;i<100;i++){ /*将对象加入到memcached缓存*/ boolean success = client.set("" + i, "Hello!"); /*从memcached缓存中按key值取对象*/ String result = (String) client.get("" + i); System.out.println(String.format("set( %d ): %s", i, success)); System.out.println(String.format("get( %d ): %s", i, result)); } for(int i=0;i<10;i++){ client.delete(""+i); } } }