转摘memcache

一、概念

Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能。

二、适用场合

1.分布式应用。由于memcached本身基于分布式的系统,所以尤其适合大型的分布式系统。

2.数据库前段缓存。数据库常常是网站系统的瓶颈。数据库的大并发量访问,常常造成网站内存溢出。当然我们也可以使用Hibernate的缓存机制。但memcached是基于分布式的,并可独立于网站应用本身,所以更适合大型网站进行应用的拆分。

3.服务器间数据共享。举例来讲,我们将网站的登录系统、查询系统拆分为两个应用,放在不同的服务器上,并进行集群,那这个时候用户登录后,登录信息如何从登录系统服务器同步到查询系统服务器呢?这时候,我们便可以使用memcached,登录系统将登录信息缓存起来,查询系统便可以获得登录信息,就像获取本地信息一样。

三、不适用场合

那些不需要“分布”的,不需要共享的,或者干脆规模小到只有一台服务器的应用,memcached不会带来任何好处,相反还会拖慢系统效率,因为网络连接同样需要资源

四、安装

这里介绍windows环境的安装。

1.下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached

2.在cmd下输入'c:\memcached\memcached.exe-dinstall'安装

3.再输入:'c:\memcached\memcached.exe-dstart'启动。

以后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

六、范例

1.基于java_memcached-release_2.6.3

Java代码收藏代码

1.packagecom.hl.memcached.cache;

2.

3.importjava.util.Date;

4.

5.importcom.danga.MemCached.MemCachedClient;

6.importcom.danga.MemCached.SockIOPool;

7.

8.publicclassMyCache{

9.publicstaticvoidmain(String[]args){

10.MemCachedClientclient=newMemCachedClient();

11.String[]addr={"127.0.0.1:11211"};

12.Integer[]weights={3};

13.SockIOPoolpool=SockIOPool.getInstance();

14.pool.setServers(addr);

15.pool.setWeights(weights);

16.pool.setInitConn(5);

17.pool.setMinConn(5);

18.pool.setMaxConn(200);

19.pool.setMaxIdle(1000*30*30);

20.pool.setMaintSleep(30);

21.pool.setNagle(false);

22.pool.setSocketTO(30);

23.pool.setSocketConnectTO(0);

24.pool.initialize();

25.

26.//String[]s=pool.getServers();

27.client.setCompressEnable(true);

28.client.setCompressThreshold(1000*1024);

29.

30.//将数据放入缓存

31.client.set("test2","test2");

32.

33.//将数据放入缓存,并设置失效时间

34.Datedate=newDate(2000000);

35.client.set("test1","test1",date);

36.

37.//删除缓存数据

38.//client.delete("test1");

39.

40.//获取缓存数据

41.Stringstr=(String)client.get("test1");

42.System.out.println(str);

43.}

44.}

packagecom.hl.memcached.cache;

importjava.util.Date;

importcom.danga.MemCached.MemCachedClient;

importcom.danga.MemCached.SockIOPool;

publicclassMyCache{

publicstaticvoidmain(String[]args){

MemCachedClientclient=newMemCachedClient();

String[]addr={"127.0.0.1:11211"};

Integer[]weights={3};

SockIOPoolpool=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");

//将数据放入缓存,并设置失效时间

Datedate=newDate(2000000);

client.set("test1","test1",date);

//删除缓存数据

//client.delete("test1");

//获取缓存数据

Stringstr=(String)client.get("test1");

System.out.println(str);

}

}

2.基于alisoft-xplatform-asf-cache-2.5.1

1)配置memcached.xml

Xml代码收藏代码

1.<?xmlversion="1.0"encoding="UTF-8"?>

2.<memcached>

3.<!--name属性是程序中使用Cache的唯一标识;socketpool属性将会关联到后面的socketpool配置;-->

4.<clientname="mclient_0"compressEnable="true"defaultEncoding="UTF-8"

5.socketpool="pool_0">

6.<!--可选,用来处理出错情况-->

7.<errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler

8.</errorHandler>

9.</client>

10.

11.<!--

12.name属性和client配置中的socketpool属性相关联。

13.maintSleep属性是后台线程管理SocketIO池的检查间隔时间,如果设置为0,则表明不需要后台线程维护SocketIO线程池,默认需要管理。

14.socketTO属性是Socket操作超时配置,单位ms。aliveCheck

15.属性表示在使用Socket以前是否先检查Socket状态。

16.-->

17.<socketpoolname="pool_0"maintSleep="5000"socketTO="3000"

18.failover="true"aliveCheck="true"initConn="5"minConn="5"maxConn="250"

19.nagle="false">

20.<!--设置memcache服务端实例地址.多个地址用","隔开-->

21.<servers>127.0.0.1:11211</servers>

22.<!--

23.可选配置。表明了上面设置的服务器实例的Load权重.例如<weights>3,7</weights>表示30%load在

24.10.2.224.36:33001,70%load在10.2.224.46:33001

25.

26.<weights>3,7</weights>

27.-->

28.</socketpool>

29.</memcached>

<?xmlversion="1.0"encoding="UTF-8"?>

<memcached>

<!--name属性是程序中使用Cache的唯一标识;socketpool属性将会关联到后面的socketpool配置;-->

<clientname="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状态。

-->

<socketpoolname="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>

2)测试类

Java代码收藏代码

1.packagecom.hl.memcached.client.test;

2.

3.importjava.util.ArrayList;

4.importjava.util.List;

5.

6.importcom.alisoft.xplatform.asf.cache.ICacheManager;

7.importcom.alisoft.xplatform.asf.cache.IMemcachedCache;

8.importcom.alisoft.xplatform.asf.cache.memcached.CacheUtil;

9.importcom.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager;

10.importcom.hl.memcached.cache.client.TestBean;

11.

12.publicclassClientTest{

13.

14.@SuppressWarnings("unchecked")

15.publicstaticvoidmain(String[]args){

16.ICacheManager<IMemcachedCache>manager;

17.manager=CacheUtil.getCacheManager(IMemcachedCache.class,

18.MemcachedCacheManager.class.getName());

19.manager.setConfigFile("memcached.xml");

20.manager.start();

21.try{

22.IMemcachedCachecache=manager.getCache("mclient_0");

23.cache.put("key","value");

24.System.out.println(cache.get("key"));

25.}finally{

26.manager.stop();

27.}

28.}

29.

30.}

packagecom.hl.memcached.client.test;

importjava.util.ArrayList;

importjava.util.List;

importcom.alisoft.xplatform.asf.cache.ICacheManager;

importcom.alisoft.xplatform.asf.cache.IMemcachedCache;

importcom.alisoft.xplatform.asf.cache.memcached.CacheUtil;

importcom.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager;

importcom.hl.memcached.cache.client.TestBean;

publicclassClientTest{

@SuppressWarnings("unchecked")

publicstaticvoidmain(String[]args){

ICacheManager<IMemcachedCache>manager;

manager=CacheUtil.getCacheManager(IMemcachedCache.class,

MemcachedCacheManager.class.getName());

manager.setConfigFile("memcached.xml");

manager.start();

try{

IMemcachedCachecache=manager.getCache("mclient_0");

cache.put("key","value");

System.out.println(cache.get("key"));

}finally{

manager.stop();

}

}

}

七、使用memcached缓存javabean自定义对象

Memcached可以缓存String,也可以缓存自定义javabean。但必须是可序列化的javabean(implementsSerializable即可)

1.基于java_memcached-release_2.6.3

测试用javabean

Java代码收藏代码

1.packagecom.hl.memcached.cache.client;

2.

3.importjava.io.Serializable;

4.

5.publicclassTestBeanimplementsSerializable{

6.privatestaticfinallongserialVersionUID=5344571864700659321L;

7.

8.privateStringname;

9.privateIntegerage;

10.//get、set方法略

11.}

packagecom.hl.memcached.cache.client;

importjava.io.Serializable;

publicclassTestBeanimplementsSerializable{

privatestaticfinallongserialVersionUID=5344571864700659321L;

privateStringname;

privateIntegerage;

//get、set方法略

}

MyCache.java代码

Java代码收藏代码

1.packagecom.hl.memcached.cache;

2.

3.importjava.util.Date;

4.

5.importcom.danga.MemCached.MemCachedClient;

6.importcom.danga.MemCached.SockIOPool;

7.

8.publicclassMyCache{

9.publicstaticvoidmain(String[]args){

10.MemCachedClientclient=newMemCachedClient();

11.String[]addr={"127.0.0.1:11211"};

12.Integer[]weights={3};

13.SockIOPoolpool=SockIOPool.getInstance();

14.pool.setServers(addr);

15.pool.setWeights(weights);

16.pool.setInitConn(5);

17.pool.setMinConn(5);

18.pool.setMaxConn(200);

19.pool.setMaxIdle(1000*30*30);

20.pool.setMaintSleep(30);

21.pool.setNagle(false);

22.pool.setSocketTO(30);

23.pool.setSocketConnectTO(0);

24.pool.initialize();

25.

26.//String[]s=pool.getServers();

27.client.setCompressEnable(true);

28.client.setCompressThreshold(1000*1024);

29.

30.//将数据放入缓存

31.TestBeanbean=newTestBean();

32.bean.setName("name1");

33.bean.setAge(25);

34.client.add("bean1",bean);

35.

36.//获取缓存数据

37.TestBeanbeanClient=(TestBean)client.get("bean1");

38.System.out.println(beanClient.getName());

39.}

40.}

packagecom.hl.memcached.cache;

importjava.util.Date;

importcom.danga.MemCached.MemCachedClient;

importcom.danga.MemCached.SockIOPool;

publicclassMyCache{

publicstaticvoidmain(String[]args){

MemCachedClientclient=newMemCachedClient();

String[]addr={"127.0.0.1:11211"};

Integer[]weights={3};

SockIOPoolpool=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);

//将数据放入缓存

TestBeanbean=newTestBean();

bean.setName("name1");

bean.setAge(25);

client.add("bean1",bean);

//获取缓存数据

TestBeanbeanClient=(TestBean)client.get("bean1");

System.out.println(beanClient.getName());

}

}

2.基于alisoft-xplatform-asf-cache-2.5.1

Java代码收藏代码

1.packagecom.hl.memcached.client.test;

2.

3.importjava.util.ArrayList;

4.importjava.util.List;

5.

6.importcom.alisoft.xplatform.asf.cache.ICacheManager;

7.importcom.alisoft.xplatform.asf.cache.IMemcachedCache;

8.importcom.alisoft.xplatform.asf.cache.memcached.CacheUtil;

9.importcom.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager;

10.importcom.hl.memcached.cache.client.TestBean;

11.

12.publicclassClientTest{

13.

14.@SuppressWarnings("unchecked")

15.publicstaticvoidmain(String[]args){

16.ICacheManager<IMemcachedCache>manager;

17.manager=CacheUtil.getCacheManager(IMemcachedCache.class,

18.MemcachedCacheManager.class.getName());

19.manager.setConfigFile("memcached.xml");

20.manager.start();

21.try{

22.IMemcachedCachecache=manager.getCache("mclient_0");

23.

24.TestBeanbean=newTestBean();

25.bean.setName("name1");

26.bean.setAge(25);

27.cache.put("bean",bean);

28.TestBeanbeanClient=(TestBean)cache.get("bean");

29.System.out.println(beanClient.getName());

30.

31.List<TestBean>list=newArrayList<TestBean>();

32.list.add(bean);

33.cache.put("beanList",list);

34.List<TestBean>listClient=(List<TestBean>)cache.get("beanList");

35.if(listClient.size()>0){

36.TestBeanbean4List=listClient.get(0);

37.System.out.println(bean4List.getName());

38.}

39.}finally{

40.manager.stop();

41.}

42.}

43.

44.}

packagecom.hl.memcached.client.test;

importjava.util.ArrayList;

importjava.util.List;

importcom.alisoft.xplatform.asf.cache.ICacheManager;

importcom.alisoft.xplatform.asf.cache.IMemcachedCache;

importcom.alisoft.xplatform.asf.cache.memcached.CacheUtil;

importcom.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager;

importcom.hl.memcached.cache.client.TestBean;

publicclassClientTest{

@SuppressWarnings("unchecked")

publicstaticvoidmain(String[]args){

ICacheManager<IMemcachedCache>manager;

manager=CacheUtil.getCacheManager(IMemcachedCache.class,

MemcachedCacheManager.class.getName());

manager.setConfigFile("memcached.xml");

manager.start();

try{

IMemcachedCachecache=manager.getCache("mclient_0");

TestBeanbean=newTestBean();

bean.setName("name1");

bean.setAge(25);

cache.put("bean",bean);

TestBeanbeanClient=(TestBean)cache.get("bean");

System.out.println(beanClient.getName());

List<TestBean>list=newArrayList<TestBean>();

list.add(bean);

cache.put("beanList",list);

List<TestBean>listClient=(List<TestBean>)cache.get("beanList");

if(listClient.size()>0){

TestBeanbean4List=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

九、源代码及资料见附件

*java_memcached-release_2.6.3资料.rar(191.4KB)

*下载次数:83

*alisoft-xplatform-asf-cache-2.5.1-src资料.rar(1.3MB)

*下载次数:105

*memcached-1.2.1-win32安装文件.rar(82.6KB)

*下载次数:55

*memcached-1.4.13源代码.gz(313.2KB)

*下载次数:63

*demo.rar(1006.6KB)

*下载次数:104

相关推荐