Memcached常用操作

memcached是一個高性能的、分布式內存對象緩存系統,應用廣泛。 通過緩存數據庫查詢結果,減少數據庫訪問次數,以提高動態Web應用的速度、 提高可擴展性。
它可以應對任意多個連接,使用非阻塞的網絡IO。由于它的工作機制是在內存中開辟一塊空間,然后建立一個HashTable,Memcached自管理這些HashTable。還使用內置的內存塊分配和哈希表算法,確保虛擬內存不會過來搗亂。
Memcached 官方網站:http://www.danga.com/memcached?

二. memcached 的安裝:
注:memcached 用到了libevent這個庫用于Socket的處理,所以還需要安裝libevent.官網: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 監護進程,監護進程不需要配置文件,只要在命令行里面加三四個參數就可以了:
[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使用的內存數量,單位是MB,我這里是100MB,
-u:(assume identity of <username> (only when run as root))是運行Memcache的用戶,我這里是root,
-l:(interface to listen on)是監聽的服務器IP地址,如果有多個地址的話,這里指定了服務器的IP地址127.0.0.1,
-p:是設置Memcache監聽的端口,這里設置了11211,最好是1024以上的端口,
-c:選項是最大運行的并發連接數,默認是1024,這里設置了256,根據服務器的負載量來設定,
-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

也可執行以下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 — 保存數據到Memcache服務器上
Memcache::get — 提取一個保存在Memcache服務器上的數據
Memcache::replace — 替換一個已經存在Memcache服務器上的項目
Memcache::delete — 從Memcache服務器上刪除一個保存的項目
Memcache::flush — 刷新所有Memcache服務器上保存的項目(類似于刪除所有的保存的項目)
Memcache::getStats — 獲取當前Memcache服務器運行的狀態

For More:?http://cn.php.net/memcache

二、查看系統的運行狀態:

stats
pid?????????????? Process id of this server process (memcache服務器的進程ID
uptime??????????? Number of seconds this server has been running (服務器已經運行的秒數)
time????????????? Current UNIX time according to the server (服務器當前的UNIX時間)
version?????????? Version string of this server (memcache版本)
pointer_size????? Current system pointer 當前操作系統的指針大小(32位系統一般是32bit)
rusage_user?????? Accumulated user time for this process (該進程累計的用戶時間(秒:微妙))
rusage_system???? Accumulated system time for this process (該進程累計的系統時間(秒:微妙))
curr_items??????? Current number of items stored by the server (服務器當前存儲的內容數量
total_items?????? Total number of items stored by this server ever since it started (服務器啟動以來存儲過的內容總數
bytes???????????? Current number of bytes used by this server to store items (服務器當前存儲內容所占用的字節數)
curr_connections? Number of open connections (當前打開著的連接數量)
total_connections Total number of connections opened since the server started running (服務器運行以來接受的連接總數)
connection_structures Number of connection structures allocated by the server (服務器分配的連接結構的數量)
cmd_get???????????? Cumulative number of retrieval requests (get命令(獲取)總請求次數)
cmd_set???????????? Cumulative number of storage requests (set命令(保存)總請求次數)
get_hits??????????? Number of keys that have been requested and found present (請求成功的總次數)
get_misses????????? Number of items that have been requested and not found (請求失敗的總次數)
threads???????????? Current number of thread (當前線程數)
bytes_read????????? Total number of bytes read by this server from network (服務器從網絡讀取到的總字節數)
bytes_written?????? Total number of bytes sent by this server to network (服務器向網絡發送的總字節數)
limit_maxbytes????? Number of bytes this server is allowed to use for storage. (服務器在存儲時被允許使用的字節總數)
evictions?????????? Number of valid items removed from cache to free memory for new items (為獲取空閑內存而刪除的items數(分配給memcache的空間用滿后需要刪除舊的items來得到空間分配給新的items))

其中,最關注最多的幾個參數:
uptime:是memcached運行的秒數。
cmd_get:是查詢緩存的次數。
cmd_get/uptime 結果是平均每秒請求緩存的次數——結果值越大,說明Memcached的利用率越高,站點的訪問量大,如果太低,用文件系統緩存就可以了,根本不會體現出使用memcached的強大性能。
cmd_set:是設置key=>value的次數。整個memcached是個大hash,用cmd_get沒有找到的內容,就會調用一下cmd_set寫進緩存里。
get_hits:是緩存命中的次數。所謂的命中率 = get_hits/cmd_get * 100%。
get_misses:是緩存未命中的次數。get_misses加上get_hits就等于cmd_get。
stats:顯示服務器信息、統計數據等
stats reset:清空統計數據

stats slabs:顯示各個slab的信息,包括chunk的大小、數目、使用情況等
stats items:顯示各個slab中item的數目和存儲時長(最后一次訪問距離現在的秒數)
quit:退出

三、利用shell命令操作Memcached
1、數據存儲(key為wan,value為123)
set
2、數據取回
get
3、替換數據(將以wan為key存儲的值替換為122)
replace
4、數值增加 1
incr
5、數值減少 2
decr
6、數據刪除
delete
7、查看Memcached當時狀態
printf “stats\r\n” | nc 127.0.0.1 11211
8、查看Memcached實時狀態

watch “printf ‘stats\r\n’ | nc 127.0.0.1 11211″

watch

Memcached protocol 中英文檔可以參考:

http://blog.s135.com/book/memcached/

四. 查看slabs的使用狀況
使用memcached的創造著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的頁數
Count: Slab內的記錄數
Full?: Slab內是否含有空閑chunk

五. 也可以圖形化監控 Memcached 的運行狀態
http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/
是一個PHP源文件,只需要修改源碼中的用戶名、密碼以及數組$MEMCACHE_SERVERS 就可以了。





一、存儲命令

存儲命令的格式:

1
2
<command name> <key> <flags> <exptime> <bytes>
<data block>

參數說明如下:

<command name>set/add/replace
<key>查找關鍵字
<flags>客戶機使用它存儲關于鍵值對的額外信息
<exptime>該數據的存活時間,0表示永遠
<bytes>存儲字節數
<data block>存儲的數據塊(可直接理解為key-value結構中的value)

1、添加

(1)、無論如何都存儲的set

set

這個set的命令在memcached中的使用頻率極高。set命令不但可以簡單添加,如果set的key已經存在,該命令可以更新該key所對應的原來的數據,也就是實現更新的作用。

可以通過“get 鍵名”的方式查看添加進去的記錄:

set_get

如你所知,我們也可以通過delete命令刪除掉,然后重新添加。

delete

(2)、只有數據不存在時進行添加的add

add

(3)、只有數據存在時進行替換的replace

replace

?

2、刪除

delete

可以看到,刪除已存在的鍵值和不存在的記錄可以返回不同的結果。

?

二、讀取命令

1、get

get命令的key可以表示一個或者多個鍵,鍵之間以空格隔開

get

2、gets

gets

可以看到,gets命令比普通的get命令多返回了一個數字(上圖中為13)。這個數字可以檢查數據是否發生改變。當key對應的數據改變時,這個多返回的數字也會改變。

3、cas

cas即checked and set的意思,只有當最后一個參數和gets所獲取的參數匹配時才能存儲,否則返回“EXISTS”。

cas

?

三、狀態命令

1、stats

stats

?

2、stats items

statsitems
執行stats items,可以看到STAT items行,如果memcached存儲內容很多,那么這里也會列出很多的STAT items行。

?

3、stats cachedump slab_id limit_num

我們執行stats cachedump 1 0 命令效果如下:

statscachedump

這里slab_id為1,是由2中的stats items返回的結果(STAT items后面的數字)決定的;limit_num看起來好像是返回多少條記錄,猜的一點不錯, 不過0表示顯示出所有記錄,而n(n>0)就表示顯示n條記錄,如果n超過該slab下的所有記錄,則結果和0返回的結果一致。

statscachedump1
通過stats items、stats cachedump slab_id limit_num配合get命令可以遍歷memcached的記錄。

?

4、其他stats命令

如stats slabs,stats sizes,stats reset等等使用也比較常見。

statsother

?

四、其他常見命令

1、append

append

在現有的緩存數據添加緩存數據,如現有緩存的key不存在服務器響應為NOT_STORED。

?

2、prepend

和append非常類似,但它的作用是在現有的緩存數據添加緩存數據。

prepend

?

3、flush_all

flush_all

該命令有一個可選的數字參數。它總是執行成功,服務器會發送 “OK\r\n” 回應。它的效果是使已經存在的項目立即失效(缺省),或在指定的時間后。此后執行取回命令,將不會有任何內容返回(除非重新存儲同樣的鍵名)。 flush_all 實際上沒有立即釋放項目所占用的內存,而是在隨后陸續有新的項目被儲存時執行(這是由memcached的懶惰檢測和刪除機制決定的)。

flush_all 效果是它導致所有更新時間早于 flush_all 所設定時間的項目,在被執行取回命令時命令被忽略。

4、其他命令

memcached還有很多命令,比如對于存儲為數字型的可以通過incr/decr命令進行增減操作等等,這里只列出開發和運維中經常使用的命令,其他的不再一一舉例說明。










本文轉自 chengxuyonghu 51CTO博客,原文鏈接:http://blog.51cto.com/6226001001/1727469,如需轉載請自行聯系原作者

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/453675.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/453675.shtml
英文地址,請注明出處:http://en.pswp.cn/news/453675.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

android自定義金額輸入鍵盤_Android 自定義控件 - 仿支付寶數字鍵盤

原標題&#xff1a;Android 自定義控件 - 仿支付寶數字鍵盤簡介在一些帶有支付功能的 App 中&#xff0c;輸入的密碼一般只能是純數字&#xff0c;雖然我們可以指定 EditText 輸入框只能輸入數字&#xff0c;但是為了提供用戶的使用體驗&#xff0c;我們往往更傾向于使用自定義…

博客目錄(python相關)

python 相關 文件格式相關系列 Python 第三方模塊之 beautifulsoup&#xff08;bs4&#xff09;- 解析 HTML Python 第三方模塊之 ElementTree&#xff08;ET&#xff09;- 解析XML文件 Python 第三方模塊之 lxml - 解析 HTML 和 XML 文件 python 第三方模塊 yaml - 處理 …

項目主體思索

1&#xff1a;分布式定義; 2&#xff1a;SSO集成方式 3&#xff1a;menu動態菜單的添加 4&#xff1a;tag頁面展示; 5&#xff1a;tiles的jsp復用&#xff1b; 暫時就想到這些了&#xff0c;以后繼續補充。轉載于:https://www.cnblogs.com/siyan/p/8286738.html

centos mysql pid_centos7 mysql The server quit without updating PID file(錯誤解決)

1 問題[rootlocalhost mysql]# /etc/rc.d/init.d/mysql statusMySQL is not running, but lock file (/var/lock/subsys/mysql[FAILED][rootlocalhost mysql]# /etc/rc.d/init.d/mysql startStarting MySQL...The server quit without updating PID file (/usr/local/mysql/dat…

tfs文件系統之NS配置管理

NameServer簡稱NS 充當著客戶與DS的交互橋梁 1.NS配置文件修改&#xff1a; [public] #log file size default 1GB log_size1073741824 #log file num default 64 log_num 64 #log file level default debug log_leveldebug #main queue size default 10240 task_max_queue_…

插件式架構設計實踐:插件式系統架構設計簡介

本系列博文將使用微軟RIA技術解決方案Silverlight以及擴展性管理框架Managed Extensibility Framework&#xff08;MEF&#xff09;&#xff0c;以插件式架構設計為導線&#xff0c;分享本人在從事基于微軟Silverlight技術構建的RIA系統中實施插件式系統架構設計的相關技術和經…

第十章 動態選路協議

RIP 缺陷&#xff1a; Routing Information Protocol RIP沒有子網的概念在路由器或鏈路發生故障后&#xff0c;需要很長的一段時間才能穩定下來采用跳數作為路由度量忽略了其他一些應該考慮的因素度量最大值為15則限制了可以使用RIP的網絡的大小OSPF Open Shortest Path First …

五種方式讓你在java中讀取properties文件內容不再是難題

2019獨角獸企業重金招聘Python工程師標準>>> 方式1.通過context:property-placeholder加載配置文件jdbc.properties中的內容 <context:property-placeholder location"classpath:jdbc.properties" ignore-unresolvable"true"/> 上面的配置…

hive metastore mysql_Hive MetaStore的結構

本篇主要是介紹Hive在MySQL中存儲的源數據的表結構。Hive MetaStore 數據庫表結構圖test.pngTBLS記錄數據表的信息字段解釋TBL_ID在hive中創建表的時候自動生成的一個id&#xff0c;用來表示&#xff0c;主鍵CREATE_TIME創建的數據表的時間&#xff0c;使用的是時間戳DBS_ID這個…

修煉一名程序員的職業水準

程序就是一系列按步驟進行的操作序列&#xff0c;它有好多種級別&#xff0c;比如最低級的微程序、次低級的匯編程序、高級的各種編程語言程序、最高級的腳本語言程序&#xff0c;也許我列的不對&#xff0c;但沒關系&#xff0c;我要說的是不管是那個級別的程序&#xff0c;其…

Rails開發細節《一》

常用命令 rails new new_app cd new_app rake db:create rails server rails generate controller Blog action1 action2 rails generate scaffold Product title:string description:textrails generate model Comment commenter:string body:text post:references rake db…

latex中怎樣使公式居中_LaTeX_多行公式對齊居中的同時選擇性的加編號

標簽: 【轉載請注明出處】http://www.cnblogs.com/mashiqi 2016/10/20 一年多沒寫博文了。今天寫一個短的,記錄一下使用LaTeX的一些經驗。 如何居中多行的公式呢?我試過很多種方法后,覺得下面這個最好用: 1 \begin{flalign*}2 % In this way (this arrange of &), the…

[SDOI2008]Cave 洞穴勘測

題目描述 輝輝熱衷于洞穴勘測。 某天&#xff0c;他按照地圖來到了一片被標記為JSZX的洞穴群地區。經過初步勘測&#xff0c;輝輝發現這片區域由n個洞穴&#xff08;分別編號為1到n&#xff09;以及若干通道組成&#xff0c;并且每條通道連接了恰好兩個洞穴。假如兩個洞穴可以通…

Linux指令大全

名稱&#xff1a;cat 使用權限&#xff1a;所有使用者 使用方式&#xff1a;cat [-AbeEnstTuv] [--help] [--version] fileName 說明&#xff1a;把檔案串連接后傳到基本輸出&#xff08;螢幕或加 > fileName 到另一個檔案&#xff09; 參數&#xff1a; -n 或 --number 由 …

mysql宏參數_C語言帶參數的宏定義

C語言允許宏帶有參數。在宏定義中的參數稱為“形式參數”&#xff0c;在宏調用中的參數稱為“實際參數”&#xff0c;這點和函數有些類似。對帶參數的宏&#xff0c;在展開過程中不僅要進行字符串替換&#xff0c;還要用實參去替換形參。帶參宏定義的一般形式為&#xff1a;#de…

自定義過濾器

首先在web.xml中對過濾器的監聽 1 <!-- 自定義過濾器 -->2 <filter>3 <filter-name>AscFilter</filter-name>4 <filter-class>com.llh.filter.AscFilter</filter-class>5 </filter>6 <filter-mapping>7 …

[MS Sql Server術語解釋]預讀,邏輯讀,物理讀

在MSSQL中使用 SET STATISTICS IO ON 打開IO統計功能之后&#xff0c;每次執行完一個查詢就會在下面的【消息】面板中顯示本次查詢IO的統計信息。 (0 行受影響) 表 demo。掃描計數 1&#xff0c;邏輯讀取 622 次&#xff0c;物理讀取 0 次&#xff0c;預讀 0 次&#xff0c;lob…

mysql 數據庫查詢測試_MySQL查詢測試經驗

測試表geoinfo,整個表超過1100萬行&#xff0c;表結構&#xff1a;CREATE TABLEgeoinfo (objectidint(11) NOT NULLAUTO_INCREMENT ,latitudedouble NOT NULL,longitudedouble NOT NULL,occupancybit(1) NOT NULL,timedatetime NOT NULL,cabidvarchar(16) NOT NULL,PRIMARY KEY…

更改阿里云域名解析臺里某個域名綁定的IP之后不能解析到新IP

1.由于要撤銷一組負載均衡&#xff0c;所以需要更改阿里云域名解析臺里某個域名由原來綁定的負載均衡公網IP換到服務器公網IP 2.在服務器上nginx指定了域名訪問&#xff0c;開啟nginx服務 3.暫時關閉該組負載均衡服務 4.實現通過服務器IP可以訪問項目&#xff0c;域名訪問不了 …

秒懂數據類型的真諦—Python基礎前傳(4)

一切編程語言都是人設計的&#xff0c;既然是人設計的&#xff0c;那么設計各種功能的時候就一定會有它的道理&#xff0c;那么設計數據類型的用意是什么呢&#xff1f; (一) 基本數據類型 基本數據類型&#xff1a; 數字 int字符串 str布爾值 bool列表 list元組 tuple字典 dic…