一、概念
Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能。
二、适用场合
1.分布式应用。由于memcached本身基于分布式的系统,所以尤其适合大型的分布式系统。
2.数据库前段缓存。数据库常常是网站系统的瓶颈。数据库的大并发量访问,常常造成网站内存溢出。当然我们也可以使用Hibernate的缓存机制。但memcached是基于分布式的,并可独立于网站应用本身,所以更适合大型网站进行应用的拆分。
3.服务器间数据共享。举例来讲,我们将网站的登录系统、查询系统拆分为两个应用,放在不同的服务器上,并进行集群,那这个时候用户登录后,登录信息如何从登录系统服务器同步到查询系统服务器呢?这时候,我们便可以使用memcached,登录系统将登录信息缓存起来,查询系统便可以获得登录信息,就像获取本地信息一样。
三、不适用场合
那些不需要“分布”的,不需要共享的,或者干脆规模小到只有一台服务器的应用,memcached不会带来任何好处,相反还会拖慢系统效率,因为网络连接同样需要资源
四、安装
这里介绍windows环境的安装。
1.下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached
2.在cmd下输入 'c:\memcached\memcached.exe -d install' 安装
3.再输入: 'c:\memcached\memcached.exe -d start' 启动。
以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。
五、客户端
Memcached本身是使用C开发的,客户端可以是php、C#、或者java。我是做java的,所以这里只介绍基于java的客户端。
我在网上看到基于java的客户端有两个
1.java_memcached-release_2.6.3
1)简介
这是比较通用的Memcached客户端框架。具体原创不详。
2)依赖的jar
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
1)简介
这个东东是阿里软件的架构师岑文初进行封装的。里面的注释都是中文的,比较好。
2)依赖的jar
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
class="MsoListParagraph" style="color: #333333; font-family: Arial; line-height: 26px;">六、范例
1.基于java_memcached-release_2.6.3
Java代码 ?
- 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();??
- ??????????
- ??
- ????????client.setCompressEnable(true);??
- ????????client.setCompressThreshold(1000*1024);??
- ??????????
- ??
- ????????client.set("test2","test2");??
- ??????????
- ??
- ????????Date?date=new?Date(2000000);??
- ????????client.set("test1","test1",?date);??
- ??????????
- ??
- ??
- ??????????
- ??
- ????????String?str?=(String)client.get("test1");??
- ????????System.out.println(str);??
- ????}??
- }??
?
2.???基于alisoft-xplatform-asf-cache-2.5.1
1)??配置memcached.xml
?
Xml代码 ?
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <memcached>??
- ??????
- ????<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">??
- ??????????
- ????????<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>??
?
?
2)?测试类
?
Java代码 ?
- 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缓存java bean自定义对象
Memcached可以缓存String,也可以缓存自定义java bean。但必须是可序列化的java bean(implements Serializable即可)
1.?????????基于java_memcached-release_2.6.3
测试用java bean
?
Java代码 ?
- 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;??
- ??????
- }??
?
?MyCache.java 代码?
?
Java代码 ?
- 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();??
- ??????????
- ??
- ????????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());??
- ????}??
- }??
2.?????????基于alisoft-xplatform-asf-cache-2.5.1
?
Java代码 ?
- 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");??
- ??
- ????????????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();??
- ????????}??
- ????}??
- ??
- }