Memcached常用操作
memcached是一個高性能的、分布式內存對象緩存系統(tǒng),應用廣泛。 通過緩存數(shù)據(jù)庫查詢結果,減少數(shù)據(jù)庫訪問次數(shù),以提高動態(tài)Web應用的速度、 提高可擴展性。
它可以應對任意多個連接,使用非阻塞的網(wǎng)絡IO。由于它的工作機制是在內存中開辟一塊空間,然后建立一個HashTable,Memcached自管理這些HashTable。還使用內置的內存塊分配和哈希表算法,確保虛擬內存不會過來搗亂。
Memcached 官方網(wǎng)站:http://www.danga.com/memcached?
二. memcached 的安裝:
注:memcached 用到了libevent這個庫用于Socket的處理,所以還需要安裝libevent.官網(wǎng):http://www.monkey.org/~provos/libevent/
1. 先安裝libevent:
[root@localhost software]#?tar zxvf libevent-1.4.11-stable.tar.gz?
[root@localhost libevent-1.4.11-stable]#?./configure –prefix=/usr?
[root@localhost libevent-1.4.11-stable]#?make?
[root@localhost libevent-1.4.11-stable]#?make install?
2. 測試libevent是否安裝成功
[root@localhost libevent-1.4.11-stable]#?ls -al /usr/lib | grep libevent?
lrwxrwxrwx?? 1 root root?????? 22 07-10 13:10 libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2
-rwxr-xr-x?? 1 root root??? 31596 2007-01-07 libevent-1.1a.so.1.0.2
lrwxrwxrwx?? 1 root root?????? 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x?? 1 root root?? 308088 07-21 03:33 libevent-1.4.so.2.1.3
-rw-r--r--?? 1 root root?? 394474 07-21 03:33 libevent.a
lrwxrwxrwx?? 1 root root?????? 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x?? 1 root root?? 109490 07-21 03:33 libevent_core-1.4.so.2.1.3
-rw-r--r--?? 1 root root?? 148742 07-21 03:33 libevent_core.a
-rwxr-xr-x?? 1 root root????? 866 07-21 03:33 libevent_core.la
lrwxrwxrwx?? 1 root root?????? 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx?? 1 root root?????? 27 07-21 03:33 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x?? 1 root root?? 246870 07-21 03:33 libevent_extra-1.4.so.2.1.3
-rw-r--r--?? 1 root root?? 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x?? 1 root root????? 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx?? 1 root root?????? 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x?? 1 root root????? 831 07-21 03:33 libevent.la
lrwxrwxrwx?? 1 root root?????? 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3?
安裝OK。
3. 安裝memcached,同時需要安裝中指定libevent的安裝位置
[root@localhost software]#?tar zxvf memcached-1.4.0.tar.gz?
[root@localhost memcached-1.4.0]#?./configure –with-libevent=/usr?
[root@localhost memcached-1.4.0]#?make?
[root@localhost memcached-1.4.0]#?make intall?
4. 測試是否成功安裝memcached
[root@localhost memcached-1.4.0]#?ls -al /usr/local/bin | grep memcached?
-rwxr-xr-x? 1 root root? 188225 07-21 03:35 memcached?
安裝OK。
三. 如何啟動 memcached 服務:
只需要啟動一個 memcached 監(jiān)護進程,監(jiān)護進程不需要配置文件,只要在命令行里面加三四個參數(shù)就可以了:
[root@localhost bin]#?memcached -d -m 100 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid?
-d:?(run as a daemon)?選項是啟動一個守護進程?
-m:(max memory to use for items in megabytes (default: 64 MB))是分配給Memcache使用的內存數(shù)量,單位是MB,我這里是100MB,
-u:(assume identity of <username> (only when run as root))是運行Memcache的用戶,我這里是root,
-l:(interface to listen on)是監(jiān)聽的服務器IP地址,如果有多個地址的話,這里指定了服務器的IP地址127.0.0.1,
-p:是設置Memcache監(jiān)聽的端口,這里設置了11211,最好是1024以上的端口,
-c:選項是最大運行的并發(fā)連接數(shù),默認是1024,這里設置了256,根據(jù)服務器的負載量來設定,
-P:(save PID in <file>, only used with -d option)是設置保存Memcache的pid文件,這里是保存在 /tmp/memcached.pid?
注:也可以啟動多個守護進程,不過端口不能重復。
四. 安裝 Memcached 的PHP擴展:
在PHP中使用Memcached,有兩種方式:
一種是安裝PHP的memcached擴展。該擴展是用c寫的,效率較高,需要在服務器上安裝。
另外一種則是直接使用客戶端的php-memcached-client類庫。
下面是使用PECL中Memcache的專用擴展,因為畢竟是用C寫的,效率高,而且安裝部署起來也比較方便。
1. 在 http://pecl.php.net/package/memcache 選擇相應想要下載的memcache版本。我下載的是:memcache-2.2.5.tgz 版本。
2. 安裝 memcache
[root@localhost software]#?tar zxvf memcache-2.2.5.tgz?
[root@localhost software]#?cd memcache-2.2.5?
[root@localhost memcache-2.2.5]#?/usr/bin/phpize?
[root@localhost memcache-2.2.5]#?./configure –enable-memcache –with-php-config=/usr/bin/php-config –with-zlib-dir?
[root@localhost memcache-2.2.5]#?make?
[root@localhost memcache-2.2.5]#?make install?
這步會有類似這樣的提示:Installing shared extensions: /usr/local/php/modules
3. 把/etc/php.ini中的extension_dir = “./”修改為:extension_dir = "/usr/lib/php/modules"
4. 并添加: extension=memcache.so
也可執(zhí)行以下shell命令,對php.ini文件的修改:
sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"/nextension = "memcache.so"/n#' /usr/local/webserver/php/etc/php.ini
五. 安裝C/C++ Memcached客戶端庫:libmemcached
下載:http://download.tangent.org/libmemcached-0.32.tar.gz
1. 安裝 libmemcached
[root@localhost src]#?tar zxvf libmemcached-0.32.tar.gz?
[root@localhost src]#?cd libmemcached-0.32?
[root@localhost libmemcached-0.32]#?./configure --prefix=/usr?
[root@localhost libmemcached-0.32]#?make && make install?
2. 檢查安裝結果
[root@localhost src]#?ls /usr/lib/libmemcache*?//庫文件
[root@localhost src]#?ls /usr/include/libmemcached/*?//頭文件
[root@localhost src]#?ls /usr/bin/mem*?//命令行工具
六. 應用:
1. 啟動 memcache 服務
[root@localhost bin]#?memcached -d -m 100 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid?
2. 重啟 Web 服務器
[root@localhost bin]#?service httpd restart?
一、Memcache面向對象的常用接口包括:
Memcache::connect — 打開一個到Memcache的連接
Memcache::pconnect — 打開一個到Memcache的長連接
Memcache::close — 關閉一個Memcache的連接
Memcache::set — 保存數(shù)據(jù)到Memcache服務器上
Memcache::get — 提取一個保存在Memcache服務器上的數(shù)據(jù)
Memcache::replace — 替換一個已經(jīng)存在Memcache服務器上的項目
Memcache::delete — 從Memcache服務器上刪除一個保存的項目
Memcache::flush — 刷新所有Memcache服務器上保存的項目(類似于刪除所有的保存的項目)
Memcache::getStats — 獲取當前Memcache服務器運行的狀態(tài)
For More:?http://cn.php.net/memcache
二、查看系統(tǒng)的運行狀態(tài):
pid?????????????? Process id of this server process (memcache服務器的進程ID)
uptime??????????? Number of seconds this server has been running (服務器已經(jīng)運行的秒數(shù))
time????????????? Current UNIX time according to the server (服務器當前的UNIX時間)
version?????????? Version string of this server (memcache版本)
pointer_size????? Current system pointer 當前操作系統(tǒng)的指針大小(32位系統(tǒng)一般是32bit)
rusage_user?????? Accumulated user time for this process (該進程累計的用戶時間(秒:微妙))
rusage_system???? Accumulated system time for this process (該進程累計的系統(tǒng)時間(秒:微妙))
curr_items??????? Current number of items stored by the server (服務器當前存儲的內容數(shù)量)
total_items?????? Total number of items stored by this server ever since it started (服務器啟動以來存儲過的內容總數(shù))
bytes???????????? Current number of bytes used by this server to store items (服務器當前存儲內容所占用的字節(jié)數(shù))
curr_connections? Number of open connections (當前打開著的連接數(shù)量)
total_connections Total number of connections opened since the server started running (服務器運行以來接受的連接總數(shù))
connection_structures Number of connection structures allocated by the server (服務器分配的連接結構的數(shù)量)
cmd_get???????????? Cumulative number of retrieval requests (get命令(獲取)總請求次數(shù))
cmd_set???????????? Cumulative number of storage requests (set命令(保存)總請求次數(shù))
get_hits??????????? Number of keys that have been requested and found present (請求成功的總次數(shù))
get_misses????????? Number of items that have been requested and not found (請求失敗的總次數(shù))
threads???????????? Current number of thread (當前線程數(shù))
bytes_read????????? Total number of bytes read by this server from network (服務器從網(wǎng)絡讀取到的總字節(jié)數(shù))
bytes_written?????? Total number of bytes sent by this server to network (服務器向網(wǎng)絡發(fā)送的總字節(jié)數(shù))
limit_maxbytes????? Number of bytes this server is allowed to use for storage. (服務器在存儲時被允許使用的字節(jié)總數(shù))
evictions?????????? Number of valid items removed from cache to free memory for new items (為獲取空閑內存而刪除的items數(shù)(分配給memcache的空間用滿后需要刪除舊的items來得到空間分配給新的items))
其中,最關注最多的幾個參數(shù):
uptime:是memcached運行的秒數(shù)。
cmd_get:是查詢緩存的次數(shù)。
cmd_get/uptime 結果是平均每秒請求緩存的次數(shù)——結果值越大,說明Memcached的利用率越高,站點的訪問量大,如果太低,用文件系統(tǒng)緩存就可以了,根本不會體現(xiàn)出使用memcached的強大性能。
cmd_set:是設置key=>value的次數(shù)。整個memcached是個大hash,用cmd_get沒有找到的內容,就會調用一下cmd_set寫進緩存里。
get_hits:是緩存命中的次數(shù)。所謂的命中率 = get_hits/cmd_get * 100%。
get_misses:是緩存未命中的次數(shù)。get_misses加上get_hits就等于cmd_get。
stats:顯示服務器信息、統(tǒng)計數(shù)據(jù)等
stats reset:清空統(tǒng)計數(shù)據(jù)
stats slabs:顯示各個slab的信息,包括chunk的大小、數(shù)目、使用情況等
stats items:顯示各個slab中item的數(shù)目和存儲時長(最后一次訪問距離現(xiàn)在的秒數(shù))
quit:退出
三、利用shell命令操作Memcached
1、數(shù)據(jù)存儲(key為wan,value為123)
2、數(shù)據(jù)取回
3、替換數(shù)據(jù)(將以wan為key存儲的值替換為122)
4、數(shù)值增加 1
5、數(shù)值減少 2
6、數(shù)據(jù)刪除
7、查看Memcached當時狀態(tài)
printf “stats\r\n” | nc 127.0.0.1 11211
8、查看Memcached實時狀態(tài)
watch “printf ‘stats\r\n’ | nc 127.0.0.1 11211″
Memcached protocol 中英文檔可以參考:
http://blog.s135.com/book/memcached/
四. 查看slabs的使用狀況
使用memcached的創(chuàng)造著Brad寫的名為 memcached-tool 的Perl腳本,可以方便地獲得slab的使用情況(它將memcached的返回值整理成容易閱讀的格式)。可以從下面的地址獲得腳本:
http://code.sixapart.com/svn/memcached/trunk/server/scripts/memcached-tool
[root@localhost html]# vim memcached-tool
[root@localhost html]# chmod +x memcached-tool
[root@localhost html]# ./memcached-tool 127.0.0.1:11211
#? Item_Size?? Max_age? 1MB_pages Count?? Full?
1????? 80 B??????? 0 s?????????????? 1????? ? ?? 0????? no
2???? 104 B?????? 12175 s???????? 1?????????? 1 ???? no
3???? 176 B??? 1339587 s?????? 33?????? 196567? yes
各列的含義:
#: slab class編號
Item_Size: Chunk大小
Max_age: LRU內最舊的記錄的生存時間
1MB_pages: 分配給Slab的頁數(shù)
Count: Slab內的記錄數(shù)
Full?: Slab內是否含有空閑chunk
五. 也可以圖形化監(jiān)控 Memcached 的運行狀態(tài)
http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/
是一個PHP源文件,只需要修改源碼中的用戶名、密碼以及數(shù)組$MEMCACHE_SERVERS 就可以了。
一、存儲命令
存儲命令的格式:
| 1 2 | <command name> <key> <flags> <exptime> <bytes> <data block> |
參數(shù)說明如下:
| <command name> | set/add/replace |
| <key> | 查找關鍵字 |
| <flags> | 客戶機使用它存儲關于鍵值對的額外信息 |
| <exptime> | 該數(shù)據(jù)的存活時間,0表示永遠 |
| <bytes> | 存儲字節(jié)數(shù) |
| <data block> | 存儲的數(shù)據(jù)塊(可直接理解為key-value結構中的value) |
1、添加
(1)、無論如何都存儲的set
這個set的命令在memcached中的使用頻率極高。set命令不但可以簡單添加,如果set的key已經(jīng)存在,該命令可以更新該key所對應的原來的數(shù)據(jù),也就是實現(xiàn)更新的作用。
可以通過“get 鍵名”的方式查看添加進去的記錄:
如你所知,我們也可以通過delete命令刪除掉,然后重新添加。
(2)、只有數(shù)據(jù)不存在時進行添加的add
(3)、只有數(shù)據(jù)存在時進行替換的replace
?
2、刪除
可以看到,刪除已存在的鍵值和不存在的記錄可以返回不同的結果。
?
二、讀取命令
1、get
get命令的key可以表示一個或者多個鍵,鍵之間以空格隔開
2、gets
可以看到,gets命令比普通的get命令多返回了一個數(shù)字(上圖中為13)。這個數(shù)字可以檢查數(shù)據(jù)是否發(fā)生改變。當key對應的數(shù)據(jù)改變時,這個多返回的數(shù)字也會改變。
3、cas
cas即checked and set的意思,只有當最后一個參數(shù)和gets所獲取的參數(shù)匹配時才能存儲,否則返回“EXISTS”。
?
三、狀態(tài)命令
1、stats
?
2、stats items
執(zhí)行stats items,可以看到STAT items行,如果memcached存儲內容很多,那么這里也會列出很多的STAT items行。
?
3、stats cachedump slab_id limit_num
我們執(zhí)行stats cachedump 1 0 命令效果如下:
這里slab_id為1,是由2中的stats items返回的結果(STAT items后面的數(shù)字)決定的;limit_num看起來好像是返回多少條記錄,猜的一點不錯, 不過0表示顯示出所有記錄,而n(n>0)就表示顯示n條記錄,如果n超過該slab下的所有記錄,則結果和0返回的結果一致。
通過stats items、stats cachedump slab_id limit_num配合get命令可以遍歷memcached的記錄。
?
4、其他stats命令
如stats slabs,stats sizes,stats reset等等使用也比較常見。
?
四、其他常見命令
1、append
在現(xiàn)有的緩存數(shù)據(jù)后添加緩存數(shù)據(jù),如現(xiàn)有緩存的key不存在服務器響應為NOT_STORED。
?
2、prepend
和append非常類似,但它的作用是在現(xiàn)有的緩存數(shù)據(jù)前添加緩存數(shù)據(jù)。
?
3、flush_all
該命令有一個可選的數(shù)字參數(shù)。它總是執(zhí)行成功,服務器會發(fā)送 “OK\r\n” 回應。它的效果是使已經(jīng)存在的項目立即失效(缺省),或在指定的時間后。此后執(zhí)行取回命令,將不會有任何內容返回(除非重新存儲同樣的鍵名)。 flush_all 實際上沒有立即釋放項目所占用的內存,而是在隨后陸續(xù)有新的項目被儲存時執(zhí)行(這是由memcached的懶惰檢測和刪除機制決定的)。
flush_all 效果是它導致所有更新時間早于 flush_all 所設定時間的項目,在被執(zhí)行取回命令時命令被忽略。
4、其他命令
memcached還有很多命令,比如對于存儲為數(shù)字型的可以通過incr/decr命令進行增減操作等等,這里只列出開發(fā)和運維中經(jīng)常使用的命令,其他的不再一一舉例說明。
本文轉自 chengxuyonghu 51CTO博客,原文鏈接:http://blog.51cto.com/6226001001/1727469,如需轉載請自行聯(lián)系原作者 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結
以上是生活随笔為你收集整理的Memcached常用操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 在线支付_java如何实现在线
- 下一篇: 反射在java中的应用_java反射机制