php memcached方法有:1、set();1、add();3、replace();4、get();5、delete();6、increment();7、decrement ();8、flush();9、connect()等等。
本教學操作環境: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、boolMemcache::connect ( string $host [, int
$port [, int
$timeout=1 ]] )
Memcache::close ( void )
#關閉memcache的連結(這個函數不會關閉持久化連線)11、mixedMemcache::pconnect ( string $host [, int
$port [, int
$timeout ]] )
Memcache::addServer ( string $host [, int
$port = 11211 [, bool
$persistent [, int
$weight [, int
$timeout [, int
$retry_interval [, bool
$status [,
callback $failure_callback
[, int
$timeoutms ]] ]]]]]] )
#$persistent 是否持久化,預設true
#$weight 表示權重
$status 控制此伺服器是否標示為線上狀態(假若連線失敗,連線池少了一個伺服器,會影響原有的分配演算法)
$failure_callback
13、array
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
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
$key , mixed $value [, int
$expiration] )
#執行一個"檢查並設定"的操作,它僅在
目前客戶端最後一次取值後,該key
對應的值沒有被其他客戶端修改的情況下,
才能夠將值寫入。透過
cas_token參數進行檢查
Memcached::casByKey
( float$cas_token
, string$server_key, string $key , mixed
$value [, int
$expiration] )
6、public bool Memcached::set( string
$key , mixed
$value [, int
$expiration] )
Memcached::setByKey ( string $server_key, string
$key , mixed
$value
$expiration
] )#指定伺服器,同上8、public bool Memcached::setMulti ( array
$items [, int
$expiration ] )
9、public bool Memcached::setMultiByKey ( string
$server_key, array
$items [, int
$expiration
10、public bool Memcached::add( string
$key , mixed
$value [, int
$expiration] )
Memcached::addByKey( string $server_key, string
$key , mixed
$value
$expiration
] )##在指定伺服器上的一個新的key下增加一個元素12、public bool
Memcached::touch( string
$key , int
$expiration
13、public bool Memcached::touchByKey( string
$server_key, string
$key
$expiration)
注意:如果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 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中文網其他相關文章!

本文比較了酸和基本數據庫模型,詳細介紹了它們的特徵和適當的用例。酸優先確定數據完整性和一致性,適合財務和電子商務應用程序,而基礎則側重於可用性和

本文討論了確保PHP文件上傳的確保,以防止諸如代碼注入之類的漏洞。它專注於文件類型驗證,安全存儲和錯誤處理以增強應用程序安全性。

本文討論了在PHP中實施API速率限制的策略,包括諸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之類的庫。它還涵蓋監視,動態調整速率限制和手

本文討論了使用password_hash和pyspasswify在PHP中使用密碼的好處。主要論點是,這些功能通過自動鹽,強大的哈希算法和SECH來增強密碼保護

本文討論了OWASP在PHP和緩解策略中的十大漏洞。關鍵問題包括注射,驗證損壞和XSS,並提供用於監視和保護PHP應用程序的推薦工具。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中