介绍:
Memcached 是一个高性能的分布式内存对象缓存系统,负责管理内存中一个巨大的hash表,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据 库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
格式:key: value
基于libevent库,所以安装前确保已安装此库。
客户端使用各种语言编写:Java, C++, PHP(网上讲解php的居多)
这里我用的java的,Memcached-Java-Client 是一个memcached Java客户端API,应用广泛,运行比较稳定。
下载地址:https://github.com/gwhalin/Memcached-Java-Client/downloads
下载:
官方的Memcached(http://memcached.org/)是基于linux平台的,不过网上有Memcached for win32。
下载地址:
http://code.jellycan.com/memcached/ (版本较新)
http://www.splinedancer.com/memcached-win32/ (版本较旧)
为了防止链接失效,我在我的网盘传了一份:
http://www.kuaipan.cn/file/id_35241628297855060.html
安装:
(以下摘至:http://www.splinedancer.com/memcached-win32/ )
Install
The win32 version of memcached can be run both as a NT Service or from the command line. To install memcached as a service, follow the next steps: > > > 1. Unzip the binaries in your desired directory (eg. c:\memcached) > > 2. Install the service using the command: 'c:\memcached\memcached.exe -d install' from the command line > > 3. Start the server from the Microsoft Management Console or by running the following command: 'c:\memcached\memcached.exe -d start' > > 4. Use the server, by default listening to port 11211 >
Building from source
To build from source, you will need Visual Studio 2005 (any edition with C++ should work), Windows SDK (eg. [Windows SDK for Windows Server 2008 and .NET Framework 3.5](http://www.microsoft.com/downloads/details.aspx?FamilyId=F26B1AA4-741A-433A-9BE5-FA919850BDBF&displaylang=en)) and [libevent](http://www.monkey.org/%7Eprovos/libevent/) (win32 binary provided on this page). > > > 1. Install Visual Studio 2005 > > 2. Install Windows SDK > > 3. Put libevent.lib in Win32-Prj/ folder > > 4. Open solution file and it should build >
中文版:
1.将Memcached 1.X.X.zip解压缩到 c:\memcached目录下(此目录自行定义)。
2.Ctrl+R,输入cmd,打开命令行窗口,转到c:\memcached目录下。
3.c:\memcached\memcached.exe -d install
4.c:\memcached\memcached.exe -d start
如果你要卸载,执行下面的命令:
1.c:\memcached\memcached.exe -d stop
2.c:\memcached\memcached.exe -d uninstall
memcached的基本设置:
-p监听的端口 -l连接的IP地址, 默认是本机 -d start 启动memcached服务 -d restart 重起memcached服务 -d stop|shutdown 关闭正在运行的memcached服务 -d install 安装memcached服务 -d uninstall 卸载memcached服务 -u以的身份运行 (仅在以root运行的时候有效) -m最大内存使用,单位MB。默认64MB -M 内存耗尽时返回错误,而不是删除项 -c最大同时连接数,默认是1024 -f块大小增长因子,默认是1.25 -n最小分配空间,key+value+flags默认是48 -h 显示帮助
在win下,可以通过打开控制台,使用netstat -a -n -o命令,来查看程序的端口占用情况。
来试试这个程序:
可以把java的jar包放到当前项目的lib子文件夹下,或在Eclipse中导入(右键项目,选择Properties->Java Build Path,切换到Libraries选项卡,选择Add External JARs,导入java memcached client中的jar包,图解:http://blog.csdn.net/justinavril/article/details/2783182)。 (代码摘至:http://hi.baidu.com/hivemind/blog/item/4380fe07ac6cb3dc7a894737.html)
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class mymem {
protected static MemCachedClient mcc = new MemCachedClient();
static{
// 设置缓存服务器列表,当使用分布式缓存的时,可以指定多个缓存服务器。这里应该设置为多个不同的服务,我这里将两个服务设置为一样的,大家不要向我学习,呵呵。
String[] servers =
{
"127.0.0.1:11211",
"127.0.0.1:11211",
};
// 设置服务器权重
Integer[] weights = {3, 2};
// 创建一个Socked连接池实例
SockIOPool pool = SockIOPool.getInstance();
// 向连接池设置服务器和权重
pool.setServers(servers);
pool.setWeights(weights);
// set some TCP settings
// disable nagle
// set the read timeout to 3 secs
// and don't set a connect timeout
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// initialize the connection pool
pool.initialize();
}
public static void main(String[] args) {
mcc.set("foo", "My name is Tanky Woo");
String bar = mcc.get("foo").toString();
System.out.println(">>> " + bar);
}
}
参考资料:
http://code.jellycan.com/memcached/
http://www.splinedancer.com/memcached-win32/
http://hi.baidu.com/hivemind/blog/item/4380fe07ac6cb3dc7a894737.html
https://github.com/gwhalin/Memcached-Java-Client/wiki/HOWTO