八、参考资料:
http://blog.developers.api.sina.com.cn/?p=124
http://www.infoq.com/cn/articles/memcached-java
标签:style blog http color io 使用 java ar for
Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能。
那些不需要“分布”的,不需要共享的,或者干脆规模小到只有一台服务器的应用,memcached不会带来任何好处,相反还会拖慢系统效率,因为网络连接同样需要资源
这里介绍windows环境的安装。
以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。
Memcached本身是使用C开发的,客户端可以是php、C#、或者java。我是做java的,所以这里只介绍基于java的客户端。
我在网上看到基于java的客户端有两个
1.java_memcached-release_2.6.3
这是比较通用的Memcached客户端框架。具体原创不详。
A.commons-pool-1.5.6.jar
B.java_memcached-release_2.6.3.jar
C.slf4j-api-1.6.1.jar
D.slf4j-simple-1.6.1.jar
2.alisoft-xplatform-asf-cache-2.5.1
这个东东是阿里软件的架构师岑文初进行封装的。里面的注释都是中文的,比较好。
A.alisoft-xplatform-asf-cache-2.5.1.jar
B.commons-logging-1.0.4.jar
C.hessian-3.0.1.jar
D.log4j-1.2.9.jar
E.stax-api-1.0.1.jar
F.wstx-asl-2.0.2.jar
package com.hl.memcached.cache; import java.util.Date; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MyCache { public static void main(String[] args) { MemCachedClient client=new MemCachedClient(); String [] addr ={"127.0.0.1:11211"}; Integer [] weights = {3}; SockIOPool pool = SockIOPool.getInstance(); pool.setServers(addr); pool.setWeights(weights); pool.setInitConn(5); pool.setMinConn(5); pool.setMaxConn(200); pool.setMaxIdle(1000*30*30); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(30); pool.setSocketConnectTO(0); pool.initialize(); // String [] s =pool.getServers(); client.setCompressEnable(true); client.setCompressThreshold(1000*1024); // 将数据放入缓存 client.set("test2","test2"); // 将数据放入缓存,并设置失效时间 Date date=new Date(2000000); client.set("test1","test1", date); // 删除缓存数据 // client.delete("test1"); // 获取缓存数据 String str =(String)client.get("test1"); System.out.println(str); } }
<?xml version="1.0" encoding="UTF-8"?> <memcached> <!-- name 属性是程序中使用Cache的唯一标识;socketpool 属性将会关联到后面的socketpool配置; --> <client name="mclient_0" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool_0"> <!-- 可选,用来处理出错情况 <errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler </errorHandler> --> </client> <!-- name 属性和client 配置中的socketpool 属性相关联。 maintSleep属性是后台线程管理SocketIO池的检查间隔时间,如果设置为0,则表明不需要后台线程维护SocketIO线程池,默认需要管理。 socketTO 属性是Socket操作超时配置,单位ms。 aliveCheck 属性表示在使用Socket以前是否先检查Socket状态。 --> <socketpool name="pool_0" maintSleep="5000" socketTO="3000" failover="true" aliveCheck="true" initConn="5" minConn="5" maxConn="250" nagle="false"> <!-- 设置memcache服务端实例地址.多个地址用","隔开 --> <servers>127.0.0.1:11211</servers> <!-- 可选配置。表明了上面设置的服务器实例的Load权重. 例如 <weights>3,7</weights> 表示30% load 在 10.2.224.36:33001, 70% load 在 10.2.224.46:33001 <weights>3,7</weights> --> </socketpool> </memcached>
package com.hl.memcached.client.test; import java.util.ArrayList; import java.util.List; import com.alisoft.xplatform.asf.cache.ICacheManager; import com.alisoft.xplatform.asf.cache.IMemcachedCache; import com.alisoft.xplatform.asf.cache.memcached.CacheUtil; import com.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager; import com.hl.memcached.cache.client.TestBean; public class ClientTest { @SuppressWarnings("unchecked") public static void main(String[] args) { ICacheManager<IMemcachedCache> manager; manager = CacheUtil.getCacheManager(IMemcachedCache.class, MemcachedCacheManager.class.getName()); manager.setConfigFile("memcached.xml"); manager.start(); try { IMemcachedCache cache = manager.getCache("mclient_0"); cache.put("key", "value"); System.out.println(cache.get("key")); } finally { manager.stop(); } } }
Memcached可以缓存String,也可以缓存自定义java bean。但必须是可序列化的java bean(implements Serializable即可)
测试用java bean
package com.hl.memcached.cache.client; import java.io.Serializable; public class TestBean implements Serializable{ private static final long serialVersionUID = 5344571864700659321L; private String name; private Integer age; //get、set方法略 }
MyCache.java 代码
import java.util.Date; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MyCache { public static void main(String[] args) { MemCachedClient client=new MemCachedClient(); String [] addr ={"127.0.0.1:11211"}; Integer [] weights = {3}; SockIOPool pool = SockIOPool.getInstance(); pool.setServers(addr); pool.setWeights(weights); pool.setInitConn(5); pool.setMinConn(5); pool.setMaxConn(200); pool.setMaxIdle(1000*30*30); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(30); pool.setSocketConnectTO(0); pool.initialize(); // String [] s =pool.getServers(); client.setCompressEnable(true); client.setCompressThreshold(1000*1024); // 将数据放入缓存 TestBean bean=new TestBean(); bean.setName("name1"); bean.setAge(25); client.add("bean1", bean); // 获取缓存数据 TestBean beanClient=(TestBean)client.get("bean1"); System.out.println(beanClient.getName()); } }
import java.util.ArrayList; import java.util.List; import com.alisoft.xplatform.asf.cache.ICacheManager; import com.alisoft.xplatform.asf.cache.IMemcachedCache; import com.alisoft.xplatform.asf.cache.memcached.CacheUtil; import com.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager; import com.hl.memcached.cache.client.TestBean; public class ClientTest { @SuppressWarnings("unchecked") public static void main(String[] args) { ICacheManager<IMemcachedCache> manager; manager = CacheUtil.getCacheManager(IMemcachedCache.class, MemcachedCacheManager.class.getName()); manager.setConfigFile("memcached.xml"); manager.start(); try { IMemcachedCache cache = manager.getCache("mclient_0"); TestBean bean=new TestBean(); bean.setName("name1"); bean.setAge(25); cache.put("bean", bean); TestBean beanClient=(TestBean)cache.get("bean"); System.out.println(beanClient.getName()); List<TestBean> list=new ArrayList<TestBean>(); list.add(bean); cache.put("beanList", list); List<TestBean> listClient=(List<TestBean>)cache.get("beanList"); if(listClient.size()>0){ TestBean bean4List=listClient.get(0); System.out.println(bean4List.getName()); } } finally { manager.stop(); } } }
http://blog.developers.api.sina.com.cn/?p=124
http://www.infoq.com/cn/articles/memcached-java
来自: http://my249645546.iteye.com/blog/1420061
标签:style blog http color io 使用 java ar for
原文地址:http://www.cnblogs.com/mjorcen/p/3979341.html