首頁  >  文章  >  後端開發  >  php memcached方法有哪些

php memcached方法有哪些

青灯夜游
青灯夜游原創
2021-09-26 13:52:391167瀏覽

php memcached方法有:1、set();1、add();3、replace();4、get();5、delete();6、increment();7、decrement ();8、flush();9、connect()等等。

php memcached方法有哪些

 本教學操作環境:windows7系統、PHP7.1版,DELL G3電腦

PHP操作Memcached的方法彙總

(一)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(&#39;story_pool&#39;);
$m3 = new Memcached(&#39;story_pool&#39;);

# 现在$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(&#39;192.168.95.11&#39;, 11211);
#$a->addServer(&#39;192.168.95.11&#39;, 11210);
#$a->setOption(Memcached::OPT_COMPRESSION, false);
$b=$a->append(&#39;e&#39;,&#39;popop&#39;);
echo "<pre class="brush:php;toolbar:false">";
print_r($b);
echo "
";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的時候,整理的筆記,順便也將它貼出來吧,若有不足或錯誤的地方請各位指出哈)

推薦學習:《PHP影片教學

以上是php memcached方法有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:php有幾大模組下一篇:php有幾大模組