Memcache工作原理:
首先 memcached 是以守护程序方式运行于一个或多个服务器中,随时接受客户端的连接操作,客户端可以由各种语言编写,目前已知的客户端 API 包括 Perl/PHP/Python/Ruby/Java/C#/C 等等。客户端在与 memcached 服务建立连接之后,接下来的事情就是存取对象了,每个被存取的对象都有一个唯一的标识符 key,存取操作均通过这个 key 进行,保存到 memcached 中的对象实际上是放置内存中的,并不是保存在 cache 文件中的,这也是为什么 memcached 能够如此高效快速的原因。注意,这些对象并不是持久的,服务停止之后,里边的数据就会丢失。
与许多 cache 工具类似,Memcached 的原理并不复杂。它采用了C/S的模式,在 server 端启动服务进程,在启动时可以指定监听的 ip,自己的端口号,所使用的内存大小等几个关键参数。一旦启动,服务就一直处于可用状态。Memcached 的目前版本是通过C实现,采用了单进程,单线程,异步I/O,基于事件 (event_based) 的服务方式.使用 libevent 作为事件通知实现。多个 Server 可以协同工作,但这些 Server 之间是没有任何通讯联系的,每个 Server 只是对自己的数据进行管理。Client 端通过指定 Server 端的 ip 地址(通过域名应该也可以)。需要缓存的对象或数据是以 key->value 对的形式保存在Server端。key 的值通过 hash 进行转换,根据 hash 值把 value传递到对应的具体的某个 Server 上。当需要获取对象数据时,也根据 key 进行。首先对 key 进行 hash,通过获得的值可以确定它被保存在了哪台 Server 上,然后再向该 Server 发出请求。Client 端只需要知道保存 hash(key) 的值在哪台服务器上就可以了。
其实说到底,memcache 的工作就是在专门的机器的内存里维护一张巨大的 hash 表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率。
Memcached Users
- 下载 windows 下的 memcache 服务端
- 解压到任意目录
- 安装: x:/memcached/memcached -d install
- 启动: x:/memcached/memcached -d start
- 停止: x:/memcached/memcached -d stop
- 重启: x:/memcached/memcached -d restart
- 帮助: x:/memcached/memcached -h
- 安装成功, 用下面的代码测试一下
memcache 服务端页面内容如下:
This is a port of memcached to the win32 architecture by Kronuz
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:
- Unzip the binaries in your desired directory (eg. c:/memcached)
- Install the service using the command: 'c:/memcached/memcached.exe -d install' from either the command line
- Start the server from the Microsoft Management Console or by running the following command: 'c:/memcached/memcached.exe -d start'
- Use the server, by default listening to port 11211
Use 'memcached.exe -h' for extra help and command line server
You can check the ChangeLog to see what's new.
- memcached 1.2.1 for Win32
- memcached 1.2.1 for Win32 source code (Dec 23, 2006)
- memcached 1.2.1 for Win32 binaries (Dec 23, 2006)
- patches to add support for Win32 to the code in the svn (Dec 23, 2006)
Old files:
- memcached 1.2.0 for Win32
- memcached 1.2.0 for Win32 source code (Nov 23, 2006)
- memcached 1.2.0 for Win32 binaries (Nov 23, 2006)
- memcached 1.1.13-pre for Win32
- memcached 1.1.13 pre-release for Win32 source code (Jun 9, 2006)
- memcached 1.1.13 pre-release for Win32 binaries (Jun 9, 2006)
- memcached 1.1.12 for Win32
- memcached 1.1.12 for Win32 source code (March 31, 2006)
- memcached 1.1.12 for Win32 binaries (March 31, 2006)
- libevent 1.1a for Win32
- libevent 1.1a source code (March 29, 2006)
- libevent 1.1a library for MS Visual Studio (March 29, 2006)
示例代码:
/** Creates a new instance of memcachetest */
public memcachetest() {
}
/** This runs through some simple tests of the MemCacheClient.
* @param args the command line arguments
*/
public static void main(String[] args) {
MemCachedClient mcc = new MemCachedClient();
String[] serverlist = { "localhost:11211"}; //, "localhost:12346"};
mcc.set_compress_enable(true);
mcc.set_compress_savings(0.0); // compress everthing
mcc.set_compress_threshold(0); // compress everthing
mcc.set_servers(serverlist);
//mcc.set_serial(true);
// Integer foo = new Integer(-2);
mcc.set("foo", "Your mother eats army boots, in the day time, with her friends. " +
"English text should be nice and compressible.");
Thk thk = new Thk();
thk.setId(1);
thk.setTitle("好的");
System.out.println(mcc.set("thk", thk));
Object tmp = mcc.get("foo");
System.out.println(tmp);
System.out.println(mcc.get("thk"));
System.out.println("Sleeping ...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
while (mcc.get("foo") == null) {
System.out.print(".");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
System.out.println(mcc.get("foo"));
System.out.println(((Thk)mcc.get("thk")).getTitle());
}
}
class Thk implements Serializable{
private int id;
private String title;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}