Home>Article>Backend Development> What are the php memcached methods?
php memcached methods are: 1. set(); 1. add(); 3. replace(); 4. get(); 5. delete(); 6. increment(); 7. decrement (); 8. flush(); 9. connect() and so on.

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP operates Memcached Summary of methods
(1) memcache extension
1, boolMemcache::set( string$key, mixed$var[, int$flag[, int$expire]] )
#Update the value if the Key exists, and set the k-v pair if it does not exist. Note: $var can store any data
2, boolMemcache::add( string$key, mixed$var[, int$flag[, int$expire]] )
#Add only when key does not exist
3, boolMemcache: :replace( string$key, mixed$var[, int$flag[, int$expire]] )
#Replace the existing key value. If the key does not exist, an error will be returned.
4, stringMemcache::get( string$key[, int&$flags] )
arrayMemcache::get( array$keys[, array&$flags] )
#Get one or more values
5, boolMemcache::delete( string$key[, int$timeout= 0 ] )
#Delete the key element. If timeout is set, how many seconds will it take to delete it?
#[Note] Some versions corresponding to memcached using timeout will cause the deletion to fail. (0 is OK)
6, intMemcache::increment( string$key[, int$value= 1 ] )
#If the key exists and can be converted to a number, add int; otherwise, directly replace it with value. When the key does not exist, return false
7, intMemcache::decrement( string$key[, int$value= 1 ] )
8, boolMemcache::flush(void)
#All elements are invalid
9, boolMemcache::connect( string$host[, int$port[, int$timeout=1]] )
#Connect to the memcache server , it will automatically close after executing the script (use close to actively close)
10, boolMemcache::close(void)
#Close the memcache link (this The function will not close the persistent connection)
11. mixedMemcache::pconnect( string$host[, int$port[, int$timeout]] )
#Establish a persistent connection
12, boolMemcache::addServer( string$host[, int$port= 11211 [, bool$persistent[, int$weight[, int$timeout[, int$retry_interval[, bool$status[,callback$failure_callback[, int$timeoutms]] ]]]]]] )
#Add a server to the connection pool. The service opened through this method will be closed or actively closed when the script ends. close
#Use this Method, the network connection does not necessarily connect immediately, but will not be connected until the server needs to be used, so there is no overhead even if a large number of servers are added to the connection pool
Parameters:
$persistentWhether to persist, the default is true
$weightIndicates the weight
$retry_intervalThe retry time when the server connection fails, the default is 15 seconds, -1 means no retry
$status Controls whether this server is marked as online (if The connection fails and one server is missing from the connection pool, which will affect the original allocation algorithm)
$failure_callbackFunction executed after the connection fails (executed before failover), including Two parameters, failed host host and port
13, arrayMemcache::getExtendedStats([ string$type[, int$slabid[, int$limit= 100 ]]] )
#getExtendedStats() returns a two-dimensional associated data server statistics
#getExtendedStats(' slabs') to obtain the ID of the active slabs blocks on each server
#getExtendedStats('cachedump', $slabid, $limit) to obtain the cached items in each slab
Parameters:
#type The type of statistical information expected to be captured. The values that can be used are {reset, malloc, maps, cachedump, slabs, items, sizes}
#slabid Used with parameterstypeJointly copies data from the specified slab in blocks. The cachedump command will completely occupy the server and is usually used for strict debugging.
#limit is used in conjunction with the parametertypeto set the number of entities obtained from the server during cachedump.
14, intMemcache::getServerStatus( string$host[, int$port= 11211 ] )
#Return a The status of the server, 0 means the server is offline, non-0 means online.
15、arrayMemcache::getStats([ string$type[, int$slabid[, int$limit= 100 ]]] )
#getStats()Returnsa server statistics of associated data. Same as above
16, stringMemcache::getVersion(void)
#Return version number
17, boolMemcache::setCompressThreshold(int$threshold[, float$min_savings] )
#Enable automatic compression for large values
Parameters:
#threshold Controls the threshold value for automatic compression.
#min_saving Specifies the compression ratio of the actual stored value after compression. The supported value must be between 0 and 1. The default value is 0.2, which means 20% compression rate
18, boolMemcache::setServerParams( string$host[, int$port= 11211 [, int$timeout[, int$retry_interval= false [, bool$status[, callback$failure_callback]]] ]] )
# Used to modify server parameters during runtime
# Parameters are the same as above
##(2) memcached extension
1,Memcached::__construct([ string$persistent_id] )
persistent_id. All instances created with the samepersistent_idvalue share the same connection.
Memcached::addServer( string$host, int$port[, int$weight= 0 ] )
Memcached::addServers(array$servers)
Memcached::cas( float$cas_token, string$key, mixed$value[, int$expiration] )
value of thecurrent client, this key When the corresponding value has not been modified by other clients, Only then can the value be written. Check throughcas_tokenparameters
Memcached::casByKey(float$cas_token, string$server_key, string$key, mixed$value[, int$expiration] )
Memcached::set( string$key, mixed$value[, int$expiration] )
Memcached::setByKey( string$server_key, string$key, mixed$value[, int$expiration] )
Memcached::setMulti(array$items[, int$expiration] )
Memcached::setMultiByKey( string$server_key, array$items[, int$expiration] )
Memcached::add( string$key, mixed$value[, int$expiration] )
Memcached::addByKey( string$server_key, string$key, mixed$value[, int$expiration] )
Memcached::touch( string$key, int$expiration)
Memcached::touchByKey( string$server_key, string$key, int$expiration)
Memcached::append( string$key, string$value)
valuestring value corresponding to the parameter to the existing element
注意:如果Memcached::OPT_COMPRESSION常量开启,这个操作会失败,并引发一个警告,因为向压缩数据后追加数据可能会导致解压不了。
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 ""; print_r($b); echo "
";die; ?>

15、public boolMemcached::appendByKey( string$server_key, string$key, string$value)
#向指定服务器已经存在的元素后追加value参数对应的字符串值
16、public boolMemcached::prepend( string$key, string$value)
#向一个已存在的元素前面追加数据
17、public boolMemcached::prependByKey( string$server_key, string$key, string$value)
#向指定服务器已经存在的元素前追加value参数对应的字符串值
18、public boolMemcached::replace( string$key, mixed$value[, int$expiration] )
#替换已存在key下的元素
19、public boolMemcached::replaceByKey( string$server_key, string$key, mixed$value[, int$expiration] )
#替换指定服务器的key下的元素
20、public intMemcached::decrement( string$key[, int$offset= 1 ] )
#减小数值元素的值
#不存在key返回错误、减到小于0结果为0、元素不是数值以0对待
21、public intMemcached::decrementByKey( string$server_key, string$key[, int$offset= 1 [, int$initial_value= 0 [, int$expiry= 0 ]]] )
#指定服务器减小数值元素的值,不存在的key则初始化为0
22、public intMemcached::increment( string$key[, int$offset= 1 ] )
#增加数值元素的值
23、public intMemcached::incrementByKey( string$server_key, string$key[, int$offset= 1 [, int$initial_value= 0 [, int$expiry= 0 ]]] )
#同上
24、public boolMemcached::delete( string$key[, int$time= 0 ] )
#删除一个元素
#设置时间后,表明在time时间后才删除,在这段时间内get、add、replace命令对该key都无效。
25、public boolMemcached::deleteByKey( string$server_key, string$key[, int$time= 0 ] )
#同上
26、public boolMemcached::deleteMulti( array$keys[, int$time= 0 ] )
#删除多个key
27、public boolMemcached::deleteMultiByKey( string$server_key, array$keys[, int$time= 0 ] )
#同上
28、public boolMemcached::flush([ int$delay= 0 ] )
#让所有缓冲区的数据失效
29、public mixedMemcached::get( string$key[, callback$cache_cb[, float&$cas_token]] )
#检索一个元素
#$callback 回调函数,没有$key之值时,将会调用这个函数,会传入三个参数memcache对象、key、引用传递变量的返回值(true时返回)
#$cas_token 配合cas使用。同一个客户端最后一个get将会生成一个64位唯一标识符存储,然后使用cas来查看更改,假若在此过程中被其他客户端修改则,返回false
30、public mixedMemcached::getByKey( string$server_key, string$key[, callback$cache_cb[, float&$cas_token]] )
#从特定的服务器检索元素
31、public mixedMemcached::getMulti( array$keys[, array&$cas_tokens[, int$flags]] )
#检索多个元素,提供$cas值,则添加cas值
#$flags 只能为Memcached::GET_PRESERVE_ORDER,保证返回的key的顺序和请求时一致。
32、public arrayMemcached::getMultiByKey( string$server_key, array$keys[, string&$cas_tokens[, int$flags]] )
#从特定服务器检索多个元素
33、public arrayMemcached::getAllKeys( void )
# Gets the keys stored on all the servers
34、public boolMemcached::getDelayed( array$keys[, bool$with_cas[, callback$value_cb]] )
#Request keys from the server. This method does not wait for the response but returns bool immediately. Use fetch and fetchAll to collect the results.
#$with_cas When true, it means that the cas value is recorded at the same time
#$value_cbResult callback function processing
35, public boolMemcached::getDelayedByKey( string$server_key, array$keys[, bool$with_cas[, callback$value_cb]] )
#Request multiple keys from the specified server
36, public arrayMemcached::fetch(void)
#From the last request to grab the next result.
37, public arrayMemcached::fetchAll(void)
#Fetch all remaining results
38, public mixedMemcached ::getOption( int$option)
#Get the Memcached option value
# One of the OPT_* series constants.
39, public boolMemcached::setOption( int$option, mixed$value)
#Set one memcached options
40, public boolMemcached::setOptions( array$options)
#Set multiple memcached options
41. public intMemcached::getResultCode(void)
#Returns the result code of the last operation
42. public stringMemcached::getResultMessage( void )
#Return the result description message of the last operation
43, public arrayMemcached::getServerByKey( string$server_key)
#Get the server information mapped by key
44, public arrayMemcached::getServerList(void)
#Get the server in the server pool Table
45, public arrayMemcached::getStats(void)
#Get statistical information in the server pool
46, public arrayMemcached::getVersion( void )
#Get all server version information in the server pool
47, public boolMemcached::isPersistent( void )
#Test whether the server is permanently connected
48, public boolMemcached::isPristine(void)
#Test whether memcache is recently created
49, public boolMemcached::quit(void)
#Close the connection
50, public boolMemcached::resetServerList( void )
#Reset the server service information of all servers
51, public voidMemcached::setSaslAuthData( string$username, string$password)
#Set the credentials to use for authentication
(The above are the notes I compiled while referring to the manual to learn memcached, and I will also post them by the way. Well, if there are any deficiencies or errors, please point them out)
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the php memcached methods?. For more information, please follow other related articles on the PHP Chinese website!