Memcache

Memcache

memcache和memcached

1,memcache是完全在PHP框架內開發的,memecached是使用libmemcached的。
2,memcached 會比 memcache 多幾個方法。

linux下操作memcache

1,啓動memcache參數

memcache -h 可以查詢幫助

    -p <num>      設置TCP端口號(默認設置爲: 11211)
    -U <num>      UDP監聽端口(默認: 11211, 0 時關閉) 
    -l <ip_addr>  綁定地址
    -d            以daemon方式運行
    -u <username> 綁定使用指定用於運行進程<username>
    -m <num>      允許最大內存用量,單位M (默認: 64 MB)
    -P <file>     將PID寫入文件<file>,這樣可以使得後邊進行快速進程終止, 需要與-d 一起使用

2,連接以及退出

telnet 127.0.0.1 11211
quit
version 查看版本

3,基本五種命令

set (key) (flags) (time) (bytes)

key 鍵值,存在則覆蓋,不存在則添加
flags 16位無符號整數,十進制表示,一起存入,作爲特殊的透明用途
time 時間,0表示永遠保存
bytes 需要存儲的字節數

例如:
    set id 0 0 5
    abcde

get

get key;

delete

delete key;

add //set

add id 0 0 5
abcde
也是存入數據,如果存在則無用,如果不存在則添加

replace //set

add id 0 0 5
abcde
也是存入值,如果存在則替換,如果不存在則無用

gets

set id  0 0 4
1234
STORED
get id
VALUE id 0 4
1234
END
gets id
VALUE id 0 4 18
1234
END

可以知道多了一個18,用於標識名稱/值對,如果對此名稱/值對執行另一個set 命令,則gets 返回的額外值將會發生更改,表示已經更新

cas

它使用與 set 命令相類似的語法,但包括一個額外的值:gets 返回的額外值。

set id 0 0 4  
qwer
STORED
gets id
VALUE id 0 4 20
qwer
END
cas id 0 0 4 6
3333
EXISTS
cas id 0 0 4 20
4444
STORED
gets 和cas 命令可以防止您使用自上次讀取後經過更新的名稱/值對

4,緩存管理命令

stats   轉儲所連接的 memcached 實例的當前統計數據
stats items
stats slabs   顯示各個slab的信息,包括chunk的大小、數目、使用情況等
stats sizes 輸出所有item的大小和個數
stats reset 清空統計數據
------------------------------------------------------------------------------

STAT pid 22459                             進程ID
STAT uptime 1027046                        服務器運行秒數
STAT time 1273043062                       服務器當前unix時間戳
STAT version 1.4.4                         服務器版本
STAT libevent 2.0.21-stable
STAT pointer_size 64                       操作系統字大小(這臺服務器是64位的)
STAT rusage_user 0.040000                  進程累計用戶時間
STAT rusage_system 0.260000                進程累計系統時間
STAT curr_connections 10                   當前打開連接數
STAT total_connections 82                  曾打開的連接總數
STAT connection_structures 13              服務器分配的連接結構數
STAT reserved_fds 20
STAT cmd_get 54                            執行get命令總數
STAT cmd_set 34                            執行set命令總數
STAT cmd_flush 3                           指向flush_all命令總數
STAT get_hits 9                            get命中次數
STAT get_misses 45                         get未命中次數
STAT delete_misses 5                       delete未命中次數
STAT delete_hits 1                         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
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 15785                      讀取字節總數
STAT bytes_written 15222                   寫入字節總數
STAT limit_maxbytes 67108864               分配的內存數(字節)
STAT accepting_conns 1                     目前接受的鏈接數
STAT listen_disabled_num 0                
STAT time_in_listen_disabled_us 0
STAT threads 4                             線程數
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT conn_yields 0
STAT bytes 0                               存儲item字節數
STAT curr_items 0                          item個數
STAT total_items 34                        item總數
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0                           爲獲取空間刪除item的總數
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0

5,清除命令

flush_all 將緩存重置到乾淨的狀態

6,其他

1,append 追加到緩存數據之後,跟着

set id 0 0 4
1234
STORED
get id
VALUE id 0 4
1234
END
append id 0 0 2
ab
STORED
get id
VALUE id 0 6
1234ab
END

2,prepend 將數據追加到當前緩存數據的之前,當緩存數據存在時才存儲

prepend id 0 0 5      
poiuy
STORED
get id
VALUE id 0 11
poiuy1234ab
END

PHP操作memcache的簡單demo

<?php

    header('content-type:text/html;charset=utf-8');
    if (!class_exists('Memcached')) {
        echo 'PHP Memcached extension was not installed';
        exit;
    }
    $mem = new Memcached();
    $mem->addServer("127.0.0.1", 11211) or die ("Could not connect");
    //顯示版本
    print_r($mem->getVersion());

    $mem->set('key1', 'This is first value', 60);
    $val = $mem->get('key1');
    echo "Get key1 value: " . $val ."<br />";

    $mem->replace('key1', 'This is replace value', 60);
    $val = $mem->get('key1');
    echo "Get key1 value: " . $val . "<br />";

    $arr = array('aaa', 'bbb', 'ccc', 'ddd');
    $mem->set('key2', $arr, 60);
    $val2 = $mem->get('key2');
    echo "Get key2 value: ";
    print_r($val2);
    echo "<br />";

    echo "<pre>";
    print_r($mem->getStats());

    $mem->delete('key1');
    $val = $mem->get('key1');
    echo "Get key1 value: " . $val . "<br />";

    $mem->flush();
    $val2 = $mem->get('key2');
    echo "Get key2 value: ";
    print_r($val2);
    echo "<br />";
?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章