Memcached是个高并发的内存键值对缓存系统,它的主要作用是将数据库查询结果,内容,以及其它一些耗时的计算结果缓存到系统内存中,从而加速Web应用程序的响应速度。

  Memcached 开始是作为Linux 程序 安装在Linux服务器上的, 开源之后 被重新编译 用于Windows环境 JellycanNorthscale两个站点都提供了Windows的二进制可执行文件下载 :

  http://code.jellycan.com/files/memcached-1.2.5-win32-bin.zip

  http://code.jellycan.com/files/memcached-1.2.6-win32-bin.zip

  http://downloads.northscale.com/memcached-win32-1.4.4-14.zip

  http://downloads.northscale.com/memcached-win64-1.4.4-14.zip

  http://downloads.northscale.com/memcached-1.4.5-x86.zip

  http://downloads.northscale.com/memcached-1.4.5-amd64.zip


在1.4.5版本之前 memcached 被安装成个服务, 之后的版本中该功能被移除了。
因此memcached 安装分为两类,第一类是1.4.5之前的版本,另一类是1.4.5之后的版本。

安装memcached < 1.4.5:

  1. 将下载的文件解压到任意目录。

  2. 1.4.5之前版本的memcached会被安装成个服务,以administrator打开控制台,运行下面的命令:

c:memcachedmemcached.exe -d install

* 注意将路径c:memcachedmemcached.exe替换成你本地的安装路径。

  3. 然后使用下面的命令启动或停止memcached服务:

c:memcachedmemcached.exe -d start
c:memcachedmemcached.exe -d stop

  4. 通过注册表键值来修改memcached的配置项 在运行中输入regedit.exe 导航到"HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesmemcached" 修改其中的键值。例如你想增加memcached所使用的最大内存限制,可以修改ImagePath的值:

"c:memcachedmemcached.exe" -d runservice -m 512

* 除了参数'-m 512'之外,你还可以使用其它的参数。通过“c:memcachedmemcached.exe -h”可以查看所有能使用的参数。

  5. 如果要卸载memcached服务,可以使用下面的命令:

c:memcachedmemcached.exe -d uninstall

 安装memcached >= 1.4.5

  1. 将下载的文件解压到任意目录。

  2. 1.4.5之后版本的memcached不能作为Windows服务来运行,必须使用Windows计划任务来运行 要将memcached配置成当Windows启动时自动运行,在命令行运行下面的命令:

schtasks /create /sc onstart /tn memcached /tr "'c:memcachedmemcached.exe' -m 512"

* 注意将路径c:memcachedmemcached.exe替换成你本地的安装路径。

** 除了参数'-m 512'之外,你还可以使用其它的参数。通过“c:memcachedmemcached.exe -h”可以查看所有能使用的参数。

  3. 通过下面的命令将memcached从Windows计划任务中移除:

schtasks /delete /tn memcached

 

PHP使用memcached

PHP使用memcached 需要安装memcache扩展包:

 1. 查看 PHP扩展包目录里是否有php_memcache.dll 文件, 没有 从https://pecl.php.net/package/memcache下载(选择windows dll文件), 复制到PHP扩展包目录里。

 2. 在php.ini中添加 代码以启用memcache扩展包:

extension=php_memcache.dll

 3. 创建 php 代码进行测试:

<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>n";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>n";
var_dump($get_result);?>

 

Python 使用memcached


Python使用memcached,需要安装memcached客户端:

1. 执行下面的命令进行memcached客户端的安装 第个适用于Python 2.x,第二个适用于Python 3.x。

pip install python-memcached
pip install python3-memcached

 2. 创建下面的python示例代码进行测试:

import memcache
mc = memcache.Client(['127.0.0.1:11211'], debug=0)
mc.set("some_key", "Some value")
value = mc.get("some_key")
mc.set("another_key", 3)
mc.delete("another_key")
mc.set("key", "1")   # note that the key used for incr/decr must be a string.mc.incr("key")
mc.decr("key")

 

Node.js 使用memcached

Node.js中可以通过memcached包来使用memcache,Github的地址https://github.com/3rd-Eden/memcached


var Memcached = require( 'memcached' );// connect to our memcached server on host 10.211.55.5, port 11211var memcached = new Memcached( "10.211.55.5:11211" );

memcached.set( "hello", 1, 10000, function( err, result ){    if( err ) console.error( err );    
console.dir( result );
memcached.end(); // as we are 100% certain we are not going to use the connection again, we are going to end it});

memcached.get( "hello", function( err, result ){    if( err ) console.error( err );    
console.dir( result );
memcached.end(); // as we are 100% certain we are not going to use the connection again, we are going to end it});

更详细的使用方法可以参考Github上的说明。

 

Memcached的数据统计

查看memcached的数据统计,可以通过telnet连接到memcached:

telnet 127.0.0.1 11211

* ip地址后面的数字为memcached运行的端口号

** 在Windows 10中telnet组件默认并没有添加,可通过Control Panel > Programs and Features > Turn Windows features on or off来添加。

使用stats来查看统计信息。stats统计结果中各数据项的含义:

NameTypeMeaning
pid32uProcess id of this server process
uptime32uNumber of secs since the server started
time32ucurrent UNIX time according to the server
versionstringVersion string of this server
pointer_size32Default size of pointers on the host OS (generally 32 or 64)
rusage_user32u.32uAccumulated user time for this process (seconds:microseconds)
rusage_system32u.32uAccumulated system time for this process (seconds:microseconds)
curr_items32uCurrent number of items stored
total_items32uTotal number of items stored since the server started
bytes64uCurrent number of bytes used to store items
curr_connections32uNumber of open connections
total_connections32uTotal number of connections opened since the server started running
connection_structures32uNumber of connection structures allocated by the server
reserved_fds32uNumber of misc fds used internally
cmd_get64uCumulative number of retrieval reqs
cmd_set64uCumulative number of storage reqs
cmd_flush64uCumulative number of flush reqs
cmd_touch64uCumulative number of touch reqs
get_hits64uNumber of keys that have been requested and found present
get_misses64uNumber of items that have been requested and not found
delete_misses64uNumber of deletions reqs for missing keys
delete_hits64uNumber of deletion reqs resulting in an item being removed.
incr_misses64uNumber of incr reqs against missing keys.
incr_hits64uNumber of successful incr reqs.
decr_misses64uNumber of decr reqs against missing keys.
decr_hits64uNumber of successful decr reqs.
cas_misses64uNumber of CAS reqs against missing keys.
cas_hits64uNumber of successful CAS reqs.
cas_badval64uNumber of CAS reqs for which a key was found, but the CAS value did not match.
touch_hits64uNumer of keys that have been touched with a new expiration time
touch_misses64uNumer of items that have been touched and not found
auth_cmds64uNumber of authentication commands handled, success or failure.
auth_errors64uNumber of failed authentications.
evictions64uNumber of valid items removed from cache to free memory for new items
reclaimed64uNumber of times an entry was stored using memory from an expired entry
bytes_read64uTotal number of bytes read by this server from network
bytes_written64uTotal number of bytes sent by this server to network
limit_maxbytes32uNumber of bytes this server is allowed to use for storage.
threads32uNumber of worker threads requested. (see doc/threads.txt)
conn_yields64uNumber of times any connection yielded to another due to hitting the -R limit.
hash_power_level32uCurrent size multiplier for hash table
hash_bytes64uBytes currently used by hash tables
hash_is_expandingboolIndicates if the hash table is being grown to a new size
expired_unfetched64uItems pulled from LRU that were never touched by get/incr/append/etc before expiring
evicted_unfetched64uItems evicted from LRU that were never touched by get/incr/append/etc.
slab_reassign_runningboolIf a slab page is being moved
slabs_moved64uTotal slab pages moved
crawler_reclaimed64uTotal items freed by LRU Crawler
lrutail_reflocked64uTimes LRU tail was found with active ref. Items moved to head to avoid OOM errors.

详细的内容可以查看这里 //github.com/memcached/memcached/blob/master/doc/protocol.txt