Home  >  Article  >  Backend Development  >  What are the php memcached methods?

What are the php memcached methods?

青灯夜游
青灯夜游Original
2021-09-26 13:52:391227browse

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.

What are the php memcached methods?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP operates Memcached Summary of methods

(1) memcache extension

1, bool Memcache::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, bool Memcache::add ( string $key , mixed $var [, int $flag [, int $expire ]] )

#Add only when key does not exist

3, bool Memcache: :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, string Memcache::get ( string $key [, int &$flags ] )

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

#Get one or more values

5, bool Memcache::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, int Memcache::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, int Memcache::decrement ( string $key [, int $value = 1 ] )

8, bool Memcache::flush (void)

#All elements are invalid

9, bool Memcache::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, bool Memcache::close (void)

#Close the memcache link (this The function will not close the persistent connection)

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

#Establish a persistent connection

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 ]] ]]]]]] )

#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:

$persistent Whether to persist, the default is true

$weight Indicates the weight

$retry_interval The 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_callback Function executed after the connection fails (executed before failover), including Two parameters, failed host host and port

13, array Memcache::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 parameterstype Jointly 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 parameter type to set the number of entities obtained from the server during cachedump.

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

#Return a The status of the server, 0 means the server is offline, non-0 means online.

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

#getStats()Returns a server statistics of associated data. Same as above

16, string Memcache::getVersion (void)

#Return version number

17, bool Memcache::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, bool Memcache::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 ] )

#By default, the Memcached instance will be destroyed after the request ends . However, you can share instances between requests by specifying a unique ID for each instance at creation time by

persistent_id. All instances created with the same persistent_id value share the same connection.

<?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 ] )

#Add the specified server to the server pool, and no connection will be established with the server at this time

3, public bool

Memcached::addServers (array $servers)

#Add multiple servers to the service pool

4, public bool

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

#Performs a "check and set" operation, which Only after the last

value of the current client, this key When the corresponding value has not been modified by other clients, Only then can the value be written. Check through cas_token parameters

5, public bool

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

#specified Server, same as above

#[$server_key is also an ordinary key. The working process of *ByKey series interface is: first, hash $server_key to get the server where $server_key should be stored, and then perform the corresponding operation in Performed on the server where $server_key is located】

6, public bool

Memcached::set( string $key, mixed $value [, int $expiration] )

#Save the value (the value can be any valid non-resource php type) under key

7, public bool

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

#Specify the server, same as above

8, public bool

Memcached::setMulti (array $items [, int $expiration ] )

#Storing multiple elements

#$items array('key'=>'value')

9、public bool

Memcached::setMultiByKey ( string $server_key, array $items [, int $expiration] )

#Specify the server, Same as above

10, public bool

Memcached::add( string $key, mixed $value [, int $expiration] )

#Add an element to a new key. If the key exists, it will fail.

11. public bool

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

# on the specified server Add an element under a new key

12, public bool

Memcached::touch( string $key, int $expiration)

#Set a new expiration time for key

13, public bool

Memcached::touchByKey( string $server_key, string $key , int $expiration)

#Set the expiration time for the key in the specified server

14, public bool

Memcached::append ( string $key , string $value )

#Append the

value string value corresponding to the parameter to the existing element

注意:如果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 ]] )

#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_cb Result callback function processing

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

#Request multiple keys from the specified server

36, public array Memcached::fetch (void)

#From the last request to grab the next result.

37, public array Memcached::fetchAll(void)

#Fetch all remaining results

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

#Get the Memcached option value

# One of the OPT_* series constants.

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

#Set one memcached options

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

#Set multiple memcached options

41. public int Memcached::getResultCode(void)

#Returns the result code of the last operation

42. public string Memcached::getResultMessage( void )

#Return the result description message of the last operation

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

#Get the server information mapped by key

44, public array Memcached::getServerList(void)

#Get the server in the server pool Table

45, public array Memcached::getStats (void)

#Get statistical information in the server pool

46, public array Memcached::getVersion( void )

#Get all server version information in the server pool

47, public bool Memcached::isPersistent( void )

#Test whether the server is permanently connected

48, public bool Memcached::isPristine (void)

#Test whether memcache is recently created

49, public bool Memcached::quit (void)

#Close the connection

50, public bool Memcached::resetServerList( void )

#Reset the server service information of all servers

51, public void Memcached::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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn