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    //當前操作系統的指針大?。?2位系統一般是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命令(獲?。┛傉埱蟠螖?/code>
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的內存大?。ㄗ止潱?/code>
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.www58058.com/38911

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

相關推薦

  • 第七周作業

    1、簡述linux操作系統啟動流程 CentOS 啟動流程圖: 通電自檢 power on system test(POST)–> BIOS –> 主機加電以后會將各個部件運轉起來,然后通過COMS上的軟件程序BIOS(Basic Input Output System)檢測每個設備cpu、內存、硬盤進行故障檢查并進行硬件…

    2018-02-07
  • Linux文件管理相關命令、bash特性及元數據

    一、 Linux上的文件管理類命令及其常用的使用方法。 1、cp命令:copy     源文件、目標文件         單源復制cp [OPTION]…[-T] SOURCE DEST   &nb…

    2017-07-09
  • 學習Linux,記得有困難,找“男人”。

      對于Linux初學者而言,較短時間內需要涉及到幾十個命令,而且每個命令還可輔以短選項、長選項…讓初學者在實踐時苦不堪言。   一頭霧水時,請找Super"man"!  "man"的使用非常簡單,命令行直接輸入man COMMAND,如#man ls。此處的COMMAND均指…

    Linux干貨 2016-01-15
  • grep

    1. 文本處理工具的使用:   cat 連接文件并打印到標準輸出設備,但是文件較大時,翻屏太快,與more或者less連用 命令反著輸入(tac)將會反向輸出文本文件 用法: cat 選項 參數   選項: -A 顯示不可打印字符 -b 對行進行編號,空白行不編號 -s 壓縮空白行 -n 對行進行編號,包括空白航  參數: &n…

    Linux干貨 2016-08-08
  • Linux文件查找命令find和壓縮解壓縮

    概述     Linux系統上一切皆文件,那么隨著時間的推移和管理的服務器增加,在眾多的文件中,如何找到我們需要的文件,這就需要用到文件查找命令了。本章就分享下關于文件查找命令的一些內容和壓縮解壓縮的相關內容。     具體分為一下幾個部分:    …

    Linux干貨 2016-08-16
  • 一個“蠅量級” C 語言協程庫

    協程(coroutine)顧名思義就是“協作的例程”(co-operative routines)。跟具有操作系統概念的線程不一樣,協程是在用戶空間利用程序語言的語法語義就能實現邏輯上類似多任務的編程技巧。實際上協程的概念比線程還要早,按照 Knuth 的說法“子例程是協程的特例”,一個子例程就是一次子函數調用,那么實際上協程就是類函數一樣的程序組件,你可以…

    Linux干貨 2016-08-15
欧美性久久久久