LNMP 增加memcached缓存系统 构建LNMMP

摘抄百度百科 memcache 的解读

memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著[1]  。这是一套开放源代码软件,以BSD license授权发布。

MemCache的工作流程如下:先检查客户端的请求数据是否在memcached中,如有,直接把请求数据返回,不再对数据库进行任何操作;如果请求的数据不在memcached中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到memcached中(memcached客户端不负责,需要程序明确实现);每次更新数据库的同时更新memcached中的数据,保证一致性;当分配给memcached内存空间用完之后,会使用LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据。[2] 

Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像视频文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。

Memcache是danga的一个项目,最早是LiveJournal 服务的,最初为了加速 LiveJournal 访问速度而开发的,后来被很多大型的网站采用。

Memcached是以守护程序(监听)方式运行于一个或多个服务器中,随时会接收客户端连接和操作。

二、安装memcached

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
[root@php-server ~]# yum install -y memcached.x86_64     #yum安装memcached
[root@php-server ~]# yum info memcached                  #列出memcached的rpm包信息 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * atomic: mirrors.neusoft.edu.cn
 * base: centos.ustc.edu.cn
 * epel: mirror01.idc.hinet.net
 * extras: centos.ustc.edu.cn
 * updates: mirrors.sina.cn
 * webtatic: us-east.repo.webtatic.com
Installed Packages
Name        : memcached
Arch        : x86_64
Version     : 1.4.24
Release     : 5.el6.art
Size        : 200 k
Repo        : installed
From repo   : atomic
Summary     : High Performance, Distributed Memory Object Cache
URL         : http://www.memcached.org/
License     : BSD
Description : memcached is a high-performance, distributed memory object caching
            : system, generic in nature, but intended for use in speeding up dynamic
            : web applications by alleviating database load.  #这就是为什么memcached可以减轻databases的原因  [root@php-server ~]# rpm -ql memcached                   #安装完memcached后,我们看下文件
/etc/rc.d/init.d/memcached                               #启动脚步
/etc/sysconfig/memcached                                 #配置文件
/usr/bin/memcached                                       #主程序文件目录位置
/usr/bin/memcached-tool                                  #工具程序文件目录位置
/usr/share/doc/memcached-1.4.24                          #后面的就不介绍了
/usr/share/doc/memcached-1.4.24/AUTHORS
/usr/share/doc/memcached-1.4.24/CONTRIBUTORS
/usr/share/doc/memcached-1.4.24/COPYING
/usr/share/doc/memcached-1.4.24/ChangeLog
/usr/share/doc/memcached-1.4.24/NEWS
/usr/share/doc/memcached-1.4.24/new_lru.txt
/usr/share/doc/memcached-1.4.24/protocol.txt
/usr/share/doc/memcached-1.4.24/readme.txt
/usr/share/doc/memcached-1.4.24/threads.txt
/usr/share/man/man1/memcached.1.gz
/var/run/memcached
[root@php-server ~]# cat /etc/sysconfig/memcached 
PORT="11211"                                            #memcached监听在哪个端口上
USER="memcached"                                        #运行memcached进程的用户
MAXCONN="1024"                                          #最大连接数
CACHESIZE="64"                                          #缓存大小(实际生产不会只有64M)
OPTIONS=""                                              #其他选项
[root@php-server ~]# systemctl start memcached.service            #启动memcached服务
Starting memcached:                                        [  OK  ]
[root@php-server ~]# netstat -tunlp |grep memcached     
tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      1672/memcached      
tcp        0      0 :::11211                    :::*                        LISTEN      1672/memcached      
udp        0      0 0.0.0.0:11211               0.0.0.0:*                               1672/memcached      
udp        0      0 :::11211                    :::*                                    1672/memcache

三、memcached命令介绍

常用命令含义说明,如下表所示:

选项

含义说

-d 指定memcached进程作为一个守护进程启动
-m <num> 指定分配给memcached使用的内存,单位是MB,默认为64;
-u <username> 运行memcached的用户
-l <ip_addr> 监听的服务器IP地址,如果有多个地址的话,使用逗号分隔,格式可以为“IP地址:端口号”,例如:-l 指定192.168.0.184:19830,192.168.0.195:13542;端口号也可以通过-p选项指定
-p <num> Listen on TCP port <num>, the default is port 11211.
-c <num> 设置最大运行的并发连接数,默认是1024
-R <num> 为避免客户端饿死(starvation),对连续达到的客户端请求数设置一个限额,如果超过该设置,会选择另一个连接来处理请求,默认为20
-k 设置锁定所有分页的内存,对于大缓存应用场景,谨慎使用该选项
-P 保存memcached进程的pid文件
-s <file> 指定Memcached用于监听的UNIX socket文件
-a <perms> 设置-s选项指定的UNIX socket文件的权限
-U <num> Listen on UDP port <num>, the default is port 11211, 0 is off.
-M 当内存使用超出配置值时,禁止自动清除缓存中的数据项,此时Memcached不可以,直到内存被释放
-r 设置产生core文件大小
-f <factor> 用于计算缓存数据项的内存块大小的乘数因子,The default is 1.25.
-n 为缓存数据项的key、value、flag设置最小分配字节数,默认是48
-C Disable the use of CAS (and reduce the per-item size by 8 bytes).
-h 显示Memcached版本和摘要信息
-v 输出警告和错误信息
-vv 打印信息比-v更详细:不仅输出警告和错误信息,也输出客户端请求和响应信息
-i 打印libevent和Memcached的licenses信息
-t <threads> 处理用于请求的线程数,The default is 4.
-D <char> 用于统计报告中Key前缀和ID之间的分隔符,默认是冒号“:”
-L 尝试使用大内存分页(pages)
-B <proto> 指定使用的协议,默认行为是自动协商(autonegotiate),可能使用的选项有auto、ascii、binary。
-I <size> 覆盖默认的STAB页大小,默认是1M
-F 禁用flush_all命令
-o <options> 指定逗号分隔的选项,一般用于用于扩展或实验性质的选项

stats命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
STAT pid 22362    //memcache服务器的进程ID  
STAT uptime 1469315    //服务器已经运行的秒数
STAT time 1339671194    //服务器当前的unix时间戳
STAT version 1.4.9    //memcache版本
STAT libevent 1.4.9-stable    //libevent版本
STAT pointer_size 64    //当前操作系统的指针大小(32位系统一般是32bit,64就是64位操作系统)
STAT rusage_user 3695.485200    //进程的累计用户时间
STAT rusage_system 14751.273465    //进程的累计系统时间
STAT curr_connections 69    //服务器当前存储的items数量
STAT total_connections 855430    //从服务器启动以后存储的items总数量
STAT connection_structures 74    //服务器分配的连接构造数
STAT reserved_fds 20    //
STAT cmd_get 328806688    //get命令(获取)总请求次数
STAT cmd_set 75441133    //set命令(保存)总请求次数  
STAT cmd_flush 34    //flush命令请求次数
STAT cmd_touch 0    //touch命令请求次数
STAT get_hits 253547177    //总命中次数
STAT get_misses 75259511    //总未命中次数
STAT delete_misses 4    //delete命令未命中次数
STAT delete_hits 565730    //delete命令命中次数
STAT incr_misses 0    //incr命令未命中次数
STAT incr_hits 0    //incr命令命中次数
STAT decr_misses 0    //decr命令未命中次数
STAT decr_hits 0    //decr命令命中次数
STAT cas_misses 0    //cas命令未命中次数
STAT cas_hits 0        //cas命令命中次数
STAT cas_badval 0    //使用擦拭次数
STAT touch_hits 0    //touch命令未命中次数
STAT touch_misses 0    //touch命令命中次数
STAT auth_cmds 0    //认证命令处理的次数
STAT auth_errors 0    //认证失败数目
STAT bytes_read 545701515844        //总读取字节数(请求字节数)
STAT bytes_written 1649639749866    //总发送字节数(结果字节数)
STAT limit_maxbytes 2147483648        //分配给memcache的内存大小(字节)
STAT accepting_conns 1            //服务器是否达到过最大连接(0/1
STAT listen_disabled_num 0    //失效的监听数
STAT threads 4        //当前线程数
STAT conn_yields 14    //连接操作主动放弃数目
STAT hash_power_level 16    //
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 30705763
STAT evicted_unfetched 0
STAT bytes 61380700    //当前存储占用的字节数
STAT curr_items 28786    //当前存储的数据总数
STAT total_items 75441133    //启动以来存储的数据总数
STAT evictions 0    //为获取空闲内存而删除的items数(分配给memcache的空间用满后需要删除旧的items来得到空间分配给新的items)
STAT reclaimed 39957976    //已过期的数据条目来存储新数据的数目
END

上面给出了各个统计项的含义说明,不再累述,stats命令有几个二级子项,说明如下表所示:

命令 含义说明
stats slabs 显示各个slab的信息,包括chunk的大小、数目、使用情况等
stats items 显示各个slab中item的数目和最老item的年龄(最后一次访问距离现在的秒数)
stats detail [on|off|dump] 设置或者显示详细操作记录;
参数为on,打开详细操作记录;
参数为off,关闭详细操作记录;
参数为dump,显示详细操作记录(每一个键值get、set、hit、del的次数)
stats malloc 打印内存分配信息
stats sizes 打印缓存使用信息
stats reset 重置统计信息

下面的命令,我们通过表格的形式说明,如下表所示:

命令 用法格式 含义说明 示例
get get <key>*\r\n 用于获取缓存的数据,键为key。 get name
VALUE name 0 7
shirdrn
END
gets gets <key>*\r\n 用于获取缓存的数据,键为一组key。 gets name hobby
VALUE name 1 7
1234567
VALUE hobby 0 25
tenis basketball football
END
set set <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n 向缓存中存储数据,不管key对应的值存在与否,都设置key对应的值。 set name 0 1800 7
shirdrn
STORED
get name
VALUE name 0 7
shirdrn
END
touch touch <key> <exptime> [noreply]\r\n 更新缓存中key对应的值的过期时间。 touch name 1800
delete delete <key> [<time>] [noreply]\r\n 给定键key,删除缓存中key对应的数据。 delete name 60
add add <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n 向缓存中存储数据,只有key对应的值不存在时,才会设置key对应的值。 add hobby 0 1800 10
basketball
STORED
get hobby

VALUE hobby 0 10
basketball
END

replace replace <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n 覆盖一个已经存在Key及其对应的Value,替换一定要保证替换后的值的长度原始长度相同,否则replace失败。 get name
VALUE name 0 7
shirdrn
END
replace name 0 1800 7
youak47
STORED
get name
VALUE name 0 7
youak47
END
append append <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n 在一个已经存在的数据值(value)上追加,是在数据值的后面追加。 get hobby
VALUE hobby 0 10
basketball
END
append hobby 0 1800 9
football
STORED
get hobby
VALUE hobby 0 19
basketball football
END
prepend prepend <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n 在一个已经存在的数据值(value)上追加,是在数据值的前面追加。 get hobby
VALUE hobby 0 19
basketball football
END
prepend hobby 0 1800 6
tenis
STORED
get hobby
VALUE hobby 0 25
tenis basketball football
END
incr incr <key> <value> [noreply]\r\n 计数命令,可以在原来已经存在的数字上进行累加求和,计算并存储新的数值。 set active_users 0 1000000 7
1000000
STORED
get active_users
VALUE active_users 0 7
1000000
END
incr active_users 99
1000099
decr decr <key> <value> [noreply]\r\n 计数命令,可以在原来已经存在的数字上进行减法计算,计算并存储新的数值。 get active_users
VALUE active_users 0 7
1000099
END
decr active_users 3456
996643
flush_all flush_all [<time>] [noreply]\r\n 使缓存中的数据项失效,可选参数是在多少秒后失效。 flush_all 1800
version version\r\n 返回Memcached服务器的版本信息。 version
quit quit\r\n 退出telnet终端。 quit

memcached默认没有认证机制,但可借助于SASL进行认证;

PHP有两种Memcached扩展,老一点的叫memcache,新一点的叫memcached:

1、php-pecl-memcache.x86_64                   3.0.8-3.el6.art              atomic  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@php-server ~]# yum info php-pecl-memcache.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * atomic: mirrors.neusoft.edu.cn
 * base: centos.ustc.edu.cn
 * epel: mirror01.idc.hinet.net
 * extras: centos.ustc.edu.cn
 * updates: mirrors.sina.cn
 * webtatic: us-east.repo.webtatic.com
Available Packages
Name        : php-pecl-memcache
Arch        : x86_64
Version     : 3.0.8
Release     : 3.el6.art
Size        : 83 k
Repo        : atomic
Summary     : Extension to work with the Memcached caching daemon
URL         : http://pecl.php.net/package/memcache
License     : PHP
Description : Memcached is a caching daemon designed especially for
            : dynamic web applications to decrease database load by
            : storing objects in memory.
            
            : This extension allows you to work with memcached through
            : handy OO and procedural interfaces.
            
            : Memcache can be used as a PHP session handler.

2、php-pecl-memcached.x86_64                  2.1.0-9.el6.art              atomic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@php-server ~]# yum info php-pecl-memcached.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * atomic: mirrors.neusoft.edu.cn
 * base: centos.ustc.edu.cn
 * epel: mirror01.idc.hinet.net
 * extras: centos.ustc.edu.cn
 * updates: mirrors.yun-idc.com
 * webtatic: us-east.repo.webtatic.com
Available Packages
Name        : php-pecl-memcached
Arch        : x86_64
Version     : 2.1.0
Release     : 9.el6.art
Size        : 55 k
Repo        : atomic
Summary     : Extension to work with the Memcached caching daemon
URL         : http://pecl.php.net/package/memcached
License     : PHP and MIT
Description : This extension uses libmemcached library to provide API for communicating
            : with memcached servers.
            
            : memcached is a high-performance, distributed memory object caching system,
            : generic in nature, but intended for use in speeding up dynamic web
            : applications by alleviating database load.
            
            : It also provides a session handler (memcached).

    libmemcached 是一个 memcached 的库,客户端库,C 和 C++ 语言实现的客户端库,具有低内存占用率、线程安全、并提供对memcached功能的全面支持,下面我们安装libmemcached.x86_64:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
[root@php-server ~]# yum install -y libmemcached.x86_64 
[root@php-server ~]# rpm -ql libmemcached     #不一一举例,只举例memstat
/usr/bin/memaslap
/usr/bin/memcapable
/usr/bin/memcat
/usr/bin/memcp
/usr/bin/memdump
/usr/bin/memerror
/usr/bin/memexist
/usr/bin/memflush                   
/usr/bin/memparse
/usr/bin/memping
/usr/bin/memrm
/usr/bin/memslap
/usr/bin/memstat                   
/usr/bin/memtouch
/usr/lib64/libhashkit.so.2
/usr/lib64/libhashkit.so.2.0.0
/usr/lib64/libmemcached.so.11
/usr/lib64/libmemcached.so.11.0.0
/usr/lib64/libmemcachedprotocol.so.0
/usr/lib64/libmemcachedprotocol.so.0.0.0
/usr/lib64/libmemcachedutil.so.2
/usr/lib64/libmemcachedutil.so.2.0.0
/usr/share/doc/libmemcached-1.0.18
/usr/share/doc/libmemcached-1.0.18/AUTHORS
/usr/share/doc/libmemcached-1.0.18/COPYING
/usr/share/doc/libmemcached-1.0.18/ChangeLog
/usr/share/doc/libmemcached-1.0.18/README
/usr/share/doc/libmemcached-1.0.18/THANKS
/usr/share/doc/libmemcached-1.0.18/TODO
/usr/share/man/man1/memaslap.1.gz
/usr/share/man/man1/memcapable.1.gz
/usr/share/man/man1/memcat.1.gz
/usr/share/man/man1/memcp.1.gz
/usr/share/man/man1/memdump.1.gz
/usr/share/man/man1/memerror.1.gz
/usr/share/man/man1/memexist.1.gz
/usr/share/man/man1/memflush.1.gz
/usr/share/man/man1/memparse.1.gz
/usr/share/man/man1/memping.1.gz
/usr/share/man/man1/memrm.1.gz
/usr/share/man/man1/memslap.1.gz
/usr/share/man/man1/memstat.1.gz
/usr/share/man/man1/memtouch.1.gz
[root@php-server ~]# memstat --help
memstat v1.0

 

    Output the state of a memcached cluster.

 

Current options. A '=' means the option takes a value.

 

     --args=
        Argument for statistics
     --version 
        Display the version of the application and then exit.
     --help 
        Display this message and then exit.
     --quiet 
        stderr and stdin will be closed at application startup.
     --verbose 
        Give more details on the progression of the application.
     --binary 
        Switch to binary protocol.
     --debug 
        Provide output only useful for debugging.
     --server-version 
        Memcached daemon software version
     --servers=
        List which servers you wish to connect to.
     --analyze=
        Analyze the provided servers.
     --username=
        Username to use for SASL authentication
     --password=
        Password to use for SASL authentication

 

[root@mytest-120-240 html]# memstat --servers=127.0.0.1
Server: 127.0.0.1 (11211)
 pid: 26427
 uptime: 5476
 time: 1471855243
 version: 1.4.15
 libevent: 2.0.21-stable
 pointer_size: 64
 rusage_user: 0.090893
 rusage_system: 0.181786
 curr_connections: 22
 total_connections: 29
 connection_structures: 23
 reserved_fds: 50
 cmd_get: 6
 cmd_set: 9
 cmd_flush: 1
 cmd_touch: 0
 get_hits: 4
 get_misses: 2
 delete_misses: 0
 delete_hits: 3
 incr_misses: 0
 incr_hits: 2
 decr_misses: 0
 decr_hits: 2
 cas_misses: 0
 cas_hits: 0
 cas_badval: 0
 touch_hits: 0
 touch_misses: 0
 auth_cmds: 0
 auth_errors: 0
 bytes_read: 530
 bytes_written: 2396
 limit_maxbytes: 536870912
 accepting_conns: 1
 listen_disabled_num: 0
 threads: 10
 conn_yields: 0
 hash_power_level: 16
 hash_bytes: 524288
 hash_is_expanding: 0
 bytes: 237
 curr_items: 3
 total_items: 13
 expired_unfetched: 0
 evicted_unfetched: 0
 evictions: 0
 reclaimed: 0

LB Cluster保持会话的方法:

1
2
3
    session sticky
    session cluster
    session server

四、为php安装memcached扩展

    php有两个版本的memcached客户端,这里介绍memcached这个是新版的客户端是基于libmemcached,所以必须要安装libmemcached,命令为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@php-server ~]# yum install -y php-pecl-memcached.x86_64
[root@php-server ~]# ll /usr/lib64/php/modules/               #将memcached.so信息添加到php.ini
total 5980
-rwxr-xr-x 1 root root  241589 Jun 10 00:34 curl.so
-rwxr-xr-x 1 root root 3153493 Jun 10 00:34 fileinfo.so
-rwxr-xr-x 1 root root   42480 Feb 11  2016 igbinary.so
-rwxr-xr-x 1 root root  150412 Jun 10 00:34 json.so
-rwxr-xr-x 1 root root  147183 Jun 10 00:34 mcrypt.so
-rwxr-xr-x 1 root root   86984 Aug  2  2013 memcached.so
-rwxr-xr-x 1 root root  861233 Jun 10 00:34 phar.so
-rwxr-xr-x 1 root root 1428036 Jun 10 00:34 zip.so
[root@php-server ~]# wget  http://pecl.php.net/get/memcache-2.2.7.tgz

[root@php-server ~]# tar -xvf memcache-2.2.7.tgz 

[root@php-server ~]# cd memcache-2.2.7 

 [root@php-server ~]# phpize        //这里需要说明一下,这个命令在编译安装php时会在安装目录下的bin目录下,但是在yum安装的php中需要安装一个名叫php-devel的包才会有(我在这里被坑过……)

[root@php-server memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/opt/application/php/bin/php-config
[root@php-server memcache-2.2.7]# make;make install
[root@php-server memcache-2.2.7]# vim /etc/php.ini    #修改php.ini配置文件
extension_dir = "/opt/application/php/lib/php/extensions/no-debug-non-zts-20131226/"
extension = "memcache.so"
[root@php-server memcache-2.2.7]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
或者:
[root@php-server memcache-2.2.7]# cp /opt/application/php/lib/php/extensions/no-debug-non-zts-20131226/* /opt/application/php/lib/php/extensions/
[root@php-server memcache-2.2.7]# vim /etc/php.ini
extension = "memcache.so"

打开php探针网页:

blob.png

测试memcached

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

[root@mytest-120-240 html]# telnet 172.16.120.240 11211
Trying 172.16.120.240...
Connected to 172.16.120.240.
Escape character is '^]'.
stats
STAT pid 26427
STAT uptime 3178
STAT time 1471852945
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.045063
STAT rusage_system 0.112659
STAT curr_connections 22
STAT total_connections 28
STAT connection_structures 23
STAT reserved_fds 50
STAT cmd_get 6
STAT cmd_set 9
STAT cmd_flush 1
STAT cmd_touch 0
STAT get_hits 4
STAT get_misses 2
STAT delete_misses 0
STAT delete_hits 3
STAT incr_misses 0
STAT incr_hits 2
STAT decr_misses 0
STAT decr_hits 2
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 507
STAT bytes_written 1344
STAT limit_maxbytes 536870912
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 10
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 237
STAT curr_items 3
STAT total_items 13
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END

用python测试memcached

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@php-server ~]# more python-memcached.py 
#!/usr/bin/env python
import memcache
mc = memcache.Client(['127.0.0.1:11211'], debug=1)
 
mc.set("some_key", "Some value")
value = mc.get("some_key")
print value
 
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")

[root@php-server ~]# python python-memcached.py 执行中如遇到Traceback (most recent call last):  File "pyton-memcached.py", line 2, in <module>    import memcacheImportError: No module named memcache #
解压后多出一个文件夹:  python-memcached-1.53cd python-memcached-1.53python setup.py installSome value

原创文章,作者:heilinux,如若转载,请注明出处:http://www.178linux.com/38911

(0)
上一篇 2016-09-19 13:49
下一篇 2016-09-19 13:49

相关推荐

  • 路径操作

    路径操作模块 3.4版本之前 os.path模块 from os import path p = path.join(‘/etc’, ‘sysconfig’, ‘network’) print(type(p), p) print(path.exists(p)) print(path.split(p)) print(path.abspath(‘.’)) p =…

    2017-10-27
  • haproxy实现discuz论坛的动静分离和负载均衡

    一、在cs2、cs3、cs4上安装httpd [root@cs2 ~]# yum install httpd [root@cs3 ~]# yum install httpd [root@cs4 ~]# yum install httpd 二…

    Linux干货 2016-06-05
  • shell脚本编写-3

    1、for循环 for 变量名 in  列表;do 循环体 done 执行机制:依次将列表中元素赋值给“变量名”;每次赋值后即执一次循环体;直到列表中元素耗尽循环结束 列表生成方式: (1) 直接给出列表 (2) 整数列表: (a){start..end} (b) $(seq [start [step]] end)  (3) 返回列表的命…

    Linux干货 2016-08-18
  • varnish学习总结

    什么是web cache?   Web缓存是指一个Web资源(如html页面,图片,js,数据等)存在与Web服务器和客户端(浏览器)直接的副本。缓存会根据进来的请求保存输出内容的副本;当下一个请求到来的时候,如果是相同的URL,缓存会根据缓存机制决定是直接使用副本响应访问请求还是向源服务器再次发送请求。       …

    Linux干货 2015-07-15
  • linux初期了解

    计算机的组成及其功能 计算机有运算器,控制器,存储器,输出设备和输入设备组成: 1.运算器:运算器又称算术逻辑单元(Arithmetic Logic Unit简称ALU)。它是计算机对数据进行加工处理的部件,包括算术运算2.控制器:控制器负责从存储器中取出指令,并对指令进行译码 3.存储器:存储器是计算机记忆或暂存数据的部件。 4.输入设备:输入设备是给计算…

    Linux干货 2016-10-30
  • 集中练习7-bash脚本

    集中练习7-bash脚本

    Linux干货 2017-12-05