您的位置:首页 > 数据库 > Memcache

分布式缓存系统Memcached简介与实践

2008-09-04 10:16 791 查看
缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载。缓存是解决这个问题的好办法。但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵活。此时Memcached或许是你想要的。

Memcached是什么?
Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。

Memcached能缓存什么?
通过在内存里维护一个统一的巨大的hash表,Memcached能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。

Memcached快么?

非 常快。Memcached使用了libevent(如果可以的话,在linux下使用epoll)来均衡任何数量的打开链接,使用非阻塞的网络I/O,对 内部对象实现引用计数(因此,针对多样的客户端,对象可以处在多样的状态), 使用自己的页块分配器和哈希表, 因此虚拟内存不会产生碎片并且虚拟内存分配的时间复杂度可以保证为O(1).。

Danga Interactive为提升Danga Interactive的速度研发了Memcached。目前,LiveJournal.com每天已经在向一百万用户提供多达两千万次的页面访问。而这 些,是由一个由web服务器和数据库服务器组成的集群完成的。Memcached几乎完全放弃了任何数据都从数据库读取的方式,同时,它还缩短了用户查看 页面的速度、更好的资源分配方式,以及Memcache失效时对数据库的访问速度。

Memcached的特点
Memcached的缓存是一种分布式的,可以让不同主机上的多个用户同时访问, 因此解决了共享内存只能单机应用的局限,更不会出现使用数据库做类似事情的时候,磁盘开销和阻塞的发生。

Memcached的使用
Memcached服务器端的安装 (此处将其作为系统服务安装)
下载文件:memcached 1.2.1 for Win32 binaries (Dec 23, 2006)
1 解压缩文件到c:/memcached
2 命令行输入 'c:/memcached/memcached.exe -d install'
3 命令行输入 'c:/memcached/memcached.exe -d start' ,该命令启动 Memcached ,默认监听端口为 11211
通过 memcached.exe -h 可以查看其帮助

一个简单的测试例子

package com.mapbar.util.cache;

import java.util.Date;

import java.util.ResourceBundle;

import com.danga.MemCached.MemCachedClient;

import com.danga.MemCached.SockIOPool;

/**

* 使用配置文件方式的memcache组件

*

*/

public class Cache {

protected static ResourceBundle rb=ResourceBundle.getBundle("memcached");;

public static MemCachedClient mcc = new MemCachedClient();

static {

init();

}

public static void init(){

synchronized (mcc) {

System.out.println("init call");

String[] servers =rb.getString("cache.servers").split(",");

String[] strWeights =rb.getString("cache.weights").split(",");

Integer[] weights = new Integer[strWeights.length];

for(int i=0;i<strWeights.length;i++){

weights[i]=Integer.parseInt(strWeights[i]);

}

//创建一个实例对象SockIOPool

SockIOPool pool = SockIOPool.getInstance();

// set the servers and the weights

//设置Memcached Server

pool.setServers( servers );

pool.setWeights( weights );

pool.setInitConn(Integer.parseInt(rb.getString("cache.initConn")));

pool.setMinConn(Integer.parseInt(rb.getString("cache.minConn")));

pool.setMaxConn(Integer.parseInt(rb.getString("cache.maxConn")));

pool.setMaxIdle(Long.parseLong(rb.getString("cache.maxIdle")));

// set the sleep for the maint thread

// it will wake up every x seconds and

// maintain the pool size

pool.setMaintSleep( 30 );

// Tcp的规则就是在发送一个包之前,本地机器会等待远程主机

// 对上一次发送的包的确认信息到来;这个方法就可以关闭套接字的缓存,

// 以至这个包准备好了就发;

pool.setNagle( false );

//连接建立后对超时的控制

pool.setSocketTO( 3000 );

//连接建立时对超时的控制

pool.setSocketConnectTO( 0 );

// initialize the connection pool

//初始化一些值并与MemcachedServer段建立连接

pool.initialize();

// lets set some compression on for the client

// compress anything larger than 64k

mcc.setCompressEnable( true );

mcc.setCompressThreshold( 64 * 1024 );

}

}

protected static void bulidCache(){

//set(key,value,Date) ,Date是一个过期时间,如果想让这个过期时间生效的话,这里传递的new Date(long date) 中参数date,需要是个大于或等于1000的值。

//因为java client的实现源码里是这样实现的 expiry.getTime() / 1000 ,也就是说,如果 小于1000的值,除以1000以后都是0,即永不过期

mcc.set( "test", "This is a test String" ,new Date(100000)); //十秒后过期

}

protected static void output() {

//从cache里取值

String value = (String) mcc.get( "test" );

System.out.println(value);

}

public static void main(String[] args){

bulidCache();

output();

}

}

其中在classespath下有个memcached.properties文件

内容如下:

##muti servers spilt by ,

#这里是你的memcached启动的地址
cache.servers=192.168.0.116:11211
cache.weights=1
#memcached be used
cache.cluster=true

#set some basic pool settings
#5 initial, 5 min, and 250 max conns
#and set the max idle time for a conn
#to 6 hours 6*60*60*1000
cache.initConn=5
cache.minConn=5
cache.maxConn=250
cache.maxIdle=21600000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: