PHP操作Memcached的方法匯總


 

memcached非關系型數據庫安裝、php中的memcache的擴展安裝、以及php中的memcached的擴展安裝可以參考:

http://www.cnblogs.com/phpstudy2015-6/p/6670103.html

(一)memcache擴展

1、bool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )

#Key存在則更新值,不存在則設置k-v對。注:$var可以存儲任何數據

2、bool Memcache::add ( string $key , mixed $var [, int $flag [, int $expire ]] )

#key不存在的時候才添加

3、bool Memcache::replace ( string $key , mixed $var [, int $flag [, int $expire ]] )

#替換存在的key值,不存在key則返回錯誤

4、string Memcache::get ( string $key [, int &$flags ] )

array Memcache::get ( array $keys [, array &$flags ] )

#獲取一個或者多個值

5、bool Memcache::delete ( string $key [, int $timeout = 0 ] )

#刪除key元素,設置了timeout則多少秒后刪除

#【注意】有些版本對應memcached使用timeout將會導致刪除失敗(0可以)

6、int Memcache::increment ( string $key [, int $value = 1 ] )

#key存在且能轉換為數字,則加int;否則直接更換為value。當key不存在,則返回false

7、int Memcache::decrement ( string $key [, int $value = 1 ] )

8、bool Memcache::flush ( void )

#全部元素失效

9、bool Memcache::connect ( string $host [, int $port [, int $timeout=1 ]] )

#連接memcache服務器,執行完腳本后會自動關閉(使用close可以主動關閉)

10、bool Memcache::close ( void )

#關閉memcache的鏈接(這個函數不會關閉持久化連接)

11、mixed Memcache::pconnect ( string $host [, int $port [, int $timeout ]] )

#建立持久化連接

12、bool Memcache::addServer ( string $host [, int $port = 11211 [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback [, int $timeoutms ]]]]]]]] )

#增加一台服務器到連接池,通過此方法打開的服務,將會在腳本結束的時候關閉或者主動關閉close

#使用此方法,網絡連接不一定立即連接,而是等需要使用此服務器的時候,才會進行連接,因此即使添加大量的服務器到連接池也沒有開銷

參數:

$persistent   是否持久化,默認true

$weight   表示權重

$retry_interval   服務器連接失敗時重試時間,默認為15秒,-1表示不重試

$status   控制此服務器是否被標記為在線狀態(假若連接失敗,連接池少了一個服務器,會影響原有的分配算法)

$failure_callback   連接失敗后執行的函數(在故障轉移前執行),包含兩個參數,失敗主機host和port

13、array Memcache::getExtendedStats ([ string $type [, int $slabid [, int $limit = 100 ]]] )

#getExtendedStats()返回一個二維關聯數據的服務器統計信息

#getExtendedStats(‘slabs’)獲取到每個服務器上活動slabs分塊的id

#getExtendedStats('cachedump', $slabid, $limit)獲取每個slab里面緩存的項

參數:

#type   期望抓取的統計信息類型,可以使用的值有{reset, malloc, maps, cachedump, slabs, items, sizes}

#slabid   用於與參數type聯合從指定slab分塊拷貝數據,cachedump命令會完全占用服務器通常用於 比較嚴格的調試。

#limit   用於和參數type聯合來設置cachedump時從服務端獲取的實體條數。

14、int Memcache::getServerStatus ( string $host [, int $port = 11211 ] )

#返回一個服務器的狀態,0表示服務器離線,非0表示在線。

15、array Memcache::getStats ([ string $type [, int $slabid [, int $limit = 100 ]]] )

#getStats()返回一個關聯數據的服務器統計信息。同上

16、string Memcache::getVersion ( void )

#返回版本號

17、bool Memcache::setCompressThreshold ( int $threshold [, float $min_savings ] )

#開啟對於大值的自動壓縮

參數:

#threshold   控制多大值進行自動壓縮的閾值。

#min_saving   指定經過壓縮實際存儲的值的壓縮率,支持的值必須在0和1之間。默認值是0.2表示20%壓縮率

18、bool Memcache::setServerParams ( string $host [, int $port = 11211 [, int $timeout [, int $retry_interval = false [, bool $status [, callback $failure_callback ]]]]] )

#用於運行時修改服務器參數

#參數同上

 

(二)memcached擴展

1、Memcached::__construct ([ string $persistent_id ] )

#默認情況下,Memcached實例在請求結束后會被銷毀。但可以在創建時通過persistent_id為每個實例指定唯一的ID,在請求間共享實例。所有通過相同的persistent_id值創建的實例共享同一個連接。

<?php
# 創建一個普通的對象
$m1 = new Memcached();
echo get_class($m);

/* 創建持久化對象 */
$m2 = new Memcached('story_pool');
$m3 = new Memcached('story_pool');

# 現在$m2和$m3共享相同的連接 ,可以使用isPresistent進行檢測
?>

2、public bool Memcached::addServer ( string $host , int $port [, int $weight = 0 ] )

#增加指定服務器到服務器池中,此時不會建立與服務端的連接

3、public bool Memcached::addServers ( array $servers )

#添加多台服務器到服務池中

4、public bool Memcached::cas ( float $cas_token , string $key , mixed $value [, int $expiration ] )

#執行一個"檢查並設置"的操作,它僅在當前客戶端最后一次取值后,該key 對應的值沒有被其他客戶端修改的情況下, 才能夠將值寫入。通過cas_token參數進行檢查

5、public bool Memcached::casByKey ( float $cas_token , string $server_key , string $key , mixed $value [, int $expiration ] )

#指定服務器,同上

#【$server_key也是一個普通的key, *ByKey系列接口的工作過程是: 首先, 對$server_key進行hash, 得到$server_key應該存儲的服務器, 然后將相應的操作在 $server_key所在的服務器上進行】

6、public bool Memcached::set ( string $key , mixed $value [, int $expiration ] )

#將value值(值可以是任何有效的非資源型php類型)存到key下

7、public bool Memcached::setByKey ( string $server_key , string $key , mixed $value [, int $expiration ] )

#指定服務器,同上

8、public bool Memcached::setMulti ( array $items [, int $expiration ] )

#存儲多個元素

#$items     array(‘key’=>’value’)

9、public bool Memcached::setMultiByKey ( string $server_key , array $items [, int $expiration ] )

#指定服務器,同上

10、public bool Memcached::add ( string $key , mixed $value [, int $expiration ] )

#向一個新的key下面增加一個元素,key存在則失敗

11、public bool Memcached::addByKey ( string $server_key , string $key , mixed $value [, int $expiration ] )

#在指定服務器上的一個新的key下增加一個元素

12、public bool Memcached::touch ( string $key , int $expiration )

#為key設置新的過期時間

13、public bool Memcached::touchByKey ( string $server_key , string $key , int $expiration )

#為指定服務器中的key設置過期時間

14、public bool Memcached::append ( string $key , string $value )

#向已經存在的元素后追加value參數對應的字符串值

注意:如果Memcached::OPT_COMPRESSION常量開啟,這個操作會失敗,並引發一個警告,因為向壓縮數據后追加數據可能會導致解壓不了。

<?php

$a = new Memcached();

$a->addServer('192.168.95.11', 11211);

#$a->addServer('192.168.95.11', 11210);

#$a->setOption(Memcached::OPT_COMPRESSION, false);

$b=$a->append('e','popop');

echo "<pre>";

print_r($b);

echo "</pre>";die;

?>

15、public bool Memcached::appendByKey ( string $server_key , string $key , string $value )

#向指定服務器已經存在的元素后追加value參數對應的字符串值

16、public bool Memcached::prepend ( string $key , string $value )

#向一個已存在的元素前面追加數據

17、public bool Memcached::prependByKey ( string $server_key , string $key , string $value )

#向指定服務器已經存在的元素前追加value參數對應的字符串值

18、public bool Memcached::replace ( string $key , mixed $value [, int $expiration ] )

#替換已存在key下的元素

19、public bool Memcached::replaceByKey ( string $server_key , string $key , mixed $value [, int $expiration ] )

#替換指定服務器的key下的元素

20、public int Memcached::decrement ( string $key [, int $offset = 1 ] )

#減小數值元素的值

#不存在key返回錯誤、減到小於0結果為0、元素不是數值以0對待

21、public int Memcached::decrementByKey ( string $server_key , string $key [, int $offset = 1 [, int $initial_value = 0 [, int $expiry = 0 ]]] )

#指定服務器減小數值元素的值,不存在的key則初始化為0

22、public int Memcached::increment ( string $key [, int $offset = 1 ] )

#增加數值元素的值

23、public int Memcached::incrementByKey ( string $server_key , string $key [, int $offset = 1 [, int $initial_value = 0 [, int $expiry = 0 ]]] )

#同上

24、public bool Memcached::delete ( string $key [, int $time = 0 ] )

#刪除一個元素

#設置時間后,表明在time時間后才刪除,在這段時間內get、add、replace命令對該key都無效。

25、public bool Memcached::deleteByKey ( string $server_key , string $key [, int $time = 0 ] )

#同上

26、public bool Memcached::deleteMulti ( array $keys [, int $time = 0 ] )

#刪除多個key

27、public bool Memcached::deleteMultiByKey ( string $server_key , array $keys [, int $time = 0 ] )

#同上

28、public bool Memcached::flush ([ int $delay = 0 ] )

#讓所有緩沖區的數據失效

29、public mixed Memcached::get ( string $key [, callback $cache_cb [, float &$cas_token ]] )

#檢索一個元素

#$callback     回調函數,沒有$key之值時,將會調用這個函數,會傳入三個參數memcache對象、key、引用傳遞變量的返回值(true時返回)

#$cas_token     配合cas使用。同一個客戶端最后一個get將會生成一個64位唯一標識符存儲,然后使用cas來查看更改,假若在此過程中被其他客戶端修改則,返回false

30、public mixed Memcached::getByKey ( string $server_key , string $key [, callback $cache_cb [, float &$cas_token ]] )

#從特定的服務器檢索元素

31、public mixed Memcached::getMulti ( array $keys [, array &$cas_tokens [, int $flags ]] )

#檢索多個元素,提供$cas值,則添加cas值

#$flags     只能為Memcached::GET_PRESERVE_ORDER,保證返回的key的順序和請求時一致。

32、public array Memcached::getMultiByKey ( string $server_key , array $keys [, string &$cas_tokens [, int $flags ]] )

#從特定服務器檢索多個元素

33、public array Memcached::getAllKeys ( void )

# Gets the keys stored on all the servers

34、public bool Memcached::getDelayed ( array $keys [, bool $with_cas [, callback $value_cb ]] )

#向服務器端請求keys,這個方法不會等待響應而是立即返回bool,收集結果使用fetch、fetchAll

#$with_cas     true時,則表示同時記錄cas值

#$value_cb     結果回調函數處理

35、public bool Memcached::getDelayedByKey ( string $server_key , array $keys [, bool $with_cas [, callback $value_cb ]] )

#從指定服務器中請求多個keys

36、public array Memcached::fetch ( void )

#從最后一次請求中抓取下一個結果。

37、public array Memcached::fetchAll ( void )

#抓取所有剩余的結果

38、public mixed Memcached::getOption ( int $option )

#獲取Memcached的選項值

# OPT_*系列常量中的一個。

39、public bool Memcached::setOption ( int $option , mixed $value )

#設置一個memcached選項

40、public bool Memcached::setOptions ( array $options )

#設置多個memcached選項

41、public int Memcached::getResultCode ( void )

#返回最后一次操作的結果代碼

42、public string Memcached::getResultMessage ( void )

#返回最后一次操作的結果描述消息

43、public array Memcached::getServerByKey ( string $server_key )

#獲取key所映射的服務器信息

44、public array Memcached::getServerList ( void )

#獲取服務器池中服務器表

45、public array Memcached::getStats ( void )

#獲取服務器池中的統計信息

46、public array Memcached::getVersion ( void )

#獲取服務器池中所有服務器版本信息

47、public bool Memcached::isPersistent ( void )

#測試服務器是否永久連接

48、public bool Memcached::isPristine ( void )

#測試memcache是否最近創建的

49、public bool Memcached::quit ( void )

#關閉連接

50、public bool Memcached::resetServerList ( void )

#重置所有服務器的服務器服務信息

51、public void Memcached::setSaslAuthData ( string $username , string $password )

#Set the credentials to use for authentication

 

(以上是自己在參考手冊學習memcached的時候,整理的筆記,順便也將它貼出來吧,若有不足或者錯誤的地方請各位指出哈)

作者:那一葉隨風

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM