search
HomeBackend DevelopmentPHP TutorialUse of php to operate memcache [transfer], php to operate memcache_PHP tutorial

How to operate memcache with php [Transfer], operate memcache with php

1. Introduction

Memcache is a project of danga.com. It was first used to serve LiveJournal. Currently, many people around the world use this caching project to build their own large-load websites to share the pressure on the database. It can handle any number of connections and uses non-blocking network IO. Since its working mechanism is to open up a space in the memory and then create a HashTable, Memcached manages these HashTables by itself. Memcache official website: http://www.danga.com/memcached, more detailed information can be found here.

Why are there two names: Memcache and memcached? In fact, Memcache is the name of this project, and memcached is the name of its main program file on the server side. You know what I mean~~~~. One is the project name and the other is the main program file name.

Memcache installation

It is divided into two processes: memcache server installation and memcached client installation.

The so-called server-side installation is to install Memcache on the server (usually a Linux system) to store data.

The so-called client installation refers to PHP (or other programs, Memcache also has other good API interfaces) to use the functions provided by Memcache on the server side. PHP needs to be added with extensions.

The memcache module is an efficient daemon process that provides procedural programs and object-oriented convenient interfaces for memory caching, especially for reducing database access when designing dynamic web programs.

Memcache also provides processing for communication conversations (session_handler).

More information about the Memcache module can be found at http://www.danga.com/memcached/.

1.1. Memcache configuration item list in php.ini

memcache在php.ini中的配置项列表
名称 默认值 是否可变 改变日志
memcache.allow_failover “1” PHP_INI_ALL Available since memcache 2.0.2.
memcache.max_failover_attempts "20" PHP_INI_ALL Available since memcache 2.1.0.
memcache.chunk_size "8192" PHP_INI_ALL Available since memcache 2.0.2.
memcache.default_port "11211" PHP_INI_ALL Available since memcache 2.0.2.
memcache.hash_strategy "standard" PHP_INI_ALL Available since memcache 2.2.0.
memcache.hash_function "crc32" PHP_INI_ALL Available since memcache 2.2.0.
session.save_handler "files" PHP_INI_ALL Supported since memcache 2.1.2
session.save_path "" PHP_INI_ALL Supported since memcache 2.1.2

For further details and definitions of PHP_INI_* constants, see the PHP manual php.ini configuration options.

1.2. The following is a brief explanation of the configuration items

memcache.allow_failover Boolean

Whether to transparently failover to other servers when an error occurs (note: failover is a verb).

memcache.max_failover_attempts integer

Define the server's quantity class settings and get data, only used in conjunction with memcache.allow_failover.

memcache.chunk_size integer

Data will be divided into chunks of the specified size (chunk_size) for transmission. The smaller this value (chunk_size), the more write operation requests are required. If you find other unexplained slowdowns, please try to increase this value. Up to 32768.

memcache.default_port string

When connecting to the memcache server, if no port is specified, the default tcp port will be used.

memcache.hash_strategy string

Control which strategy is used when mapping keys to servers. Setting this value consistently enables the hashing algorithm to be used consistently and will not be remapped when the server accepts additions or removals of variables from the pool. Setting this value gives standard results when the old strategy is used.

memcache.hash_function string

Control which hsah function is applied to the key mapping process to the server. The default value "crc32" uses the CRC32 algorithm, while "fnv" indicates the use of the FNV-1a algorithm.

session.save_handler string

Determine to use memcache for communication session processing (session handler) by setting this value to memcache.

session.save_path string

Define the delimiter for each server link used for call storage, for example: "tcp://host1:11211, tcp://host2:11211".

Each server link can contain parameters that are accepted by that server, similar to servers added using Memcache::addServer(), for example: "tcp://host1:11211?persistent=1&weight=1&timeout=1& amp ; amp;retry_interval=15".

1.3.memcache constant list

memcache常量列表
名称 类型 描述
MEMCACHE_COMPRESSED integer 用于调整在使用 Memcache::set(), Memcache::add() 和 Memcache::replace() 几个函数时的压缩比率。
MEMCACHE_HAVE_SESSION integer 如果通信对话的处理(session handler)被允许使用其值为 1,其他情况值为 0。

2Memcache Functions function list

2.1.Memcache::connect

2.1.1. Description

bool Memcache::connect ( string $host [, int $port [, int $timeout ]] )

Connect to memcache server

2.1.2. Parameters

$host(string) Server domain name or ip

$port(int) Server tcp port number, the default value is 11211

$timeout The expiration time of the connection memcache process. Think twice before modifying its default value of 1, so as not to lose all the advantages of memcache caching and cause the connection to become very slow.

2.1.3. Return value

Returns true if successful, false if failed

2.1.4. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
/* OO API */
$memcache = new Memcache;
$ memcache->connect('memcache_host', 11211);
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.2.Memcache::pconnect

2.2.1. Description

bool Memcache::pconnect ( string $host [, int $port [, int $timeout ]] )

Connect to the server in constant connection mode

2.2.2. Parameters

$host(string) Server domain name or ip

$port(int) Server tcp port number, the default value is 11211

$timeout The expiration time of the connection memcache process. Think twice before modifying its default value of 1, so as not to lose all the advantages of memcache caching and cause the connection to become very slow.

2.2.3. Return value

Returns true if successful, false if failed

2.2.4. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* procedural API */
$memcache_obj = memcache_pconnect('memcache_host', 11211);
/* OO API */
$memcache_obj = new Memcache;
$ memcache_obj->pconnect('memcache_host', 11211);
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.3.Memcache::close

2.3.1. Description

bool Memcache::close (void)

Close the object (does not work for constant connections)

2.3.2. Return value

Returns true if successful, false if failed

2.3.3. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
/* do something here .. */
memcache_close($memcache_obj);
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
/* do something here .. */
$ memcache_obj->close();
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.4.Memcache::addServer

2.4.1. Description

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

Add a server to the object (Note: addServer does not have the action of connecting to the server, so when the memcache process is not started, successful execution of addServer will also return true)

2.4.2. Parameters

host Server domain name or IP

port Port number, the default is 11211

persistent Whether to use persistent connections, the default is TRUE

weight Weight, the proportion in multiple server settings

timeout The number of seconds after which the connection to the server fails. Please think twice when changing the default value of 1. You may lose all cache advantages and cause the connection to become very slow

retry_interval The retry frequency when the server connection fails. The default is once every 15 seconds. If set to -1, automatic retry will be disabled. When the extension is loaded dynamically via dl(), regardless of this parameter or the constant connection setting parameter All will fail.

                        Each failed server has its own lifetime before it expires. When selecting a backend request, it will be skipped and the request will not be served. An expired connection will be successfully reconnected or marked as a failed connection waiting for the next time Try again. This effect means that the retry connections of each web server's child process when serving the page are related to their own retry frequency.

status Controls whether the server is marked as online, set this parameter to FALSE and set retry_interval is -1 Servers that fail to connect can be put into a server pool that describes unresponsive requests. Requests to this server will fail. Accept the settings set to failed servers. The default parameters are TRUE, indicating that the server can be defined as online.

failure_callback The callback function when failure occurs. The two parameters of the function are the hostname and port of the failed server

2.4.3. Return value

Returns TRUE on success and FALSE on failure.

Note: When testing the addServer function, we mainly tested its parameters retry_interval and status

2.4.4. Example

2.4.4.1.Testing of retry_interval parameter

$mem = new Memcache;
$is_add = $mem->addServer('localhost', 11211, true, 1, 1, 15, true); // retrt_interval=15
$is_set = $mem->set('key1', 'People's Republic of China');
?>

In the above example, if the localhost server goes down or the memcache daemon crashes, the connection will be automatically retried 15 seconds after the request fails to connect to the server. Connect to the server, but will not connect to the server within these 15 seconds. That is, as long as there is a request, it will try to connect to the server within 15 seconds, but each server connection retry is independent. For example, if I add two One of the servers is localhost and the other is 172.16.100.60. They are counted from the time when their respective connections failed. As long as there is a request for their respective servers, they will go every 15 seconds. Connect to their respective servers.

2.4.4.2. Combination of retry_interval and status

$mem = new Memcache;
$is_add = $mem->addServer('localhost', 11211, true, 1, 1, -1, false); // retrt_interval=-1 , status=false
$is_set = $mem->set('key1', 'People's Republic of China');
?>

retrt_interval=-1 above, status=false In this case, the server that failed to connect is placed in a pool that does not respond to requests, so it has no impact on the key allocation algorithm, and it returns an error immediately. Whether it fails or fails over depends on the setting of memcache.allow_failover. Execute set, add, Requests such as replace and get will fail and return false, even if the memcache process is running normally.

2.4.4.3.Testing of status parameter

In addition to being used in combination with retry_interval, the use of status alone will have an impact on the results obtained by the function memcache::getServerStatu

Regardless of whether the memcache process is running normally or crashed, when the status is true, the result of getServerStatus is true, otherwise it is false

But when the memcache process is running normally, it has no impact on functions such as set, add, replace, get, etc.

2.5.Memcache::add

2.5.1. Description

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



Add a data to be cached. If the key as the cached data does not yet exist on the server,

2.5.2. Parameters

key The key of cached data. Its length cannot exceed 250 characters

var value, integer type will be stored directly, other types will be serialized and stored, the maximum value is 1M

flag Whether to use zlib compression? When flag=MEMCACHE_COMPRESSED, zlib compression will not be used when the data is very small. Only when the data reaches a certain size, zlib compression will be performed on the data. (There is no specific test data for the minimum value for compression)

expire Expiration time, 0 means never expires, you can use unix timestamp format or the number of seconds from the current time, when set to seconds, it cannot be greater than 2592000 (30 days)

2.5.3. Return value

Returns TRUE on success and FALSE on failure. If the key already exists, memcache:;add() behaves similarly to memcache::set in other respects

2.5.4. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial $memcache_obj = memcache_connect("localhost", 11211);
/* procedural API */
memcache_add($memcache_obj, 'var_key', 'test variable', FALSE, 30);
/* OO API */
$memcache_obj->add('var_key', 'test variable', FALSE, 30);
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.6.Memcache::replace

2.6.1. Description

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

Replace the cache variable content of a specified existing key

2.6.2. Parameters

key The key of cached data, its length cannot exceed 250 characters

var value, integer type will be stored directly, other types will be serialized and stored, the maximum value is 1M

flag Whether to use zlib compression? When flag=MEMCACHE_COMPRESSED, zlib compression will not be used when the data is very small. Only when the data reaches a certain size, zlib compression will be performed on the data. (There is no specific test data for the minimum value for compression)

expire Expiration time, 0 means never expires, you can use unix timestamp format or the number of seconds from the current time, when set to seconds, it cannot be greater than 2592000 (30 days)

2.6.3. Return value

Returns TRUE on success and FALSE on failure.

2.6.4. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial $memcache_obj = memcache_connect('memcache_host', 11211);
/* procedural API */
memcache_replace($memcache_obj, "test_key", "some variable", FALSE, 30);
/* OO API */
$memcache_obj->replace("test_key", "some variable", FALSE, 30);
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.7.Memcache::set

2.7.1. Description

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


Set the cache variable content of a specified key

2.7.2. Parameters

key The key of cached data, its length cannot exceed 250 characters

var value, integer type will be stored directly, other types will be serialized and stored, the maximum value is 1M

flag Whether to use zlib compression? When flag=MEMCACHE_COMPRESSED, zlib compression will not be used when the data is very small. Only when the data reaches a certain size, zlib compression will be performed on the data. (There is no specific test data for the minimum value for compression)

expire Expiration time, 0 means never expires, you can use unix timestamp format or the number of seconds from the current time, when set to seconds, it cannot be greater than 2592000 (30 days)

2.7.3. Return value

Returns TRUE on success and FALSE on failure.

2.7.4. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* procedural API */
/* connect to memcached server */
$memcache_obj = memcache_connect('memcache_host', 11211);
/*
set value of item with key 'var_key'
using 0 as flag value, compression is not used
expire time is 30 second
*/
memcache_set($memcache_obj, 'var_key', 'some variable', 0 , 30);
echo memcache_get($memcache_obj, 'var_key');
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* OO API */
$memcache_obj = new Memcache;
/* connect to memcached server */
$memcache_obj->connect('memcache_host', 11211);
/*
set value of item with key 'var_key', using on-the-fly compression
expire time is 50 seconds
*/
$memcache_obj->set('var_key ', 'some really big variable', MEMCACHE_COMPRESSED, 50);
echo $memcache_obj->get('var_key');
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.8.Memcache::get

2.8.1. Description

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

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

Get the variable cache value of a key

2.8.2. Parameters

key The key of the cached value

flags If a variable is passed, the flag result of getting the cached value set or added will be stored in the variable

2.8.3. Return value

Returns the cached variable content of the specified key or returns FALSE on failure or the value of the variable does not exist

If none of the keys in the outgoing key array exist, the returned result is an empty array, otherwise an associative array of keys and cached values ​​is returned

2.8.4.范例

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* procedural API */
$memcache_obj = memcache_connect(‘memcache_host‘, 11211);
$var = memcache_get($memcache_obj, ‘some_key‘);
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect(‘memcache_host‘, 11211);
$var = $memcache_obj->get(‘some_key‘);
/*
You also can use array of keys as a parameter.
If such item wasn‘t found at the server, the result
array simply will not include such key.
*/
/* procedural API */
$memcache_obj = memcache_connect(‘memcache_host‘, 11211);
$var = memcache_get($memcache_obj, Array(‘some_key‘, ‘another_key‘));
//如果some_key,another_key不存在 $var = array();
//如果some_key,another_key存在     $var = array(‘some_key‘=>‘缓存值‘, ‘another_key‘=>‘缓存值‘);
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect(‘memcache_host‘, 11211);
$var = $memcache_obj->get(Array(‘some_key‘, ‘second_key‘));
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.9.Memcache::delete

2.9.1.说明

bool Memcache::delete ( string $key [, int $timeout ] )

删除某一个变量的缓存

2.9.2.参数

key          缓存的键 键值不能为null和‘’,当它等于前面两个值的时候php会有警告错误。

timeout   删除这项的时间,如果它等于0,这项将被立刻删除反之如果它等于30秒,那么这项被删除在30秒内 

2.9.3.返回值

成功返回 TRUE,失败返回 FALSE。

2.9.4.范例

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* procedural API */
$memcache_obj = memcache_connect(‘memcache_host‘, 11211);
/* after 10 seconds item will be deleted by the server */
memcache_delete($memcache_obj, ‘key_to_delete‘, 10);
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect(‘memcache_host‘, 11211);
$memcache_obj->delete(‘key_to_delete‘, 10);
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.10.Memcache::flush

2.10.1.说明

bool Memcache::flush ( void )

清空所有缓存内容,不是真的删除缓存的内容,只是使所有变量的缓存过期,使内存中的内容被重写

2.10.2.返回值

成功返回 TRUE,失败返回 FALSE。

2.10.3. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_flush($memcache_obj);
/* OO API */
$ memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$memcache_obj->flush();
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.11.Memcache::getExtendedStats

2.11.1. Description

array Memcache::getExtendedStats ([ string $type [, int $slabid [, int $limit ]]] )

Get all server extension static information

2.11.2. Parameters

type Static information type. Valid values ​​include {reset, malloc, maps, cachedump, slabs, items, sizes}. This optional parameter is agreed according to certain rules. This optional parameter is a title entered to facilitate developers to view different categories of information

slabid is used to jointly set the cache heap to be a valid slice into the heap according to the specified type. The cache heap is command bound to the server and is strictly for debugging purposes

limit is used to jointly set the size of the cache heap to the heap limited by the entered number according to the specified type. The default value is 100

2.11.3. Return value

Returns a two-dimensional array of static information extended by the server, and returns FALSE on failure

2.11.4. Example

$memcache_obj = new Memcache;
$memcache_obj->addServer('memcache_host', 11211);
$memcache_obj->addServer('failed_host', 11211);
$stats = $memcache_obj->getExtendedStats(); print_r($stats);
?>
<br />输出结果
Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Array(
[memcache_host:11211] => Array(
[pid] => 3756
[uptime] => 603011
[time] => ] => 1.1.12
[rusage_user] => 0.451931
[rusage_system] => 0.634903
[curr_items] => 2483
[total_items] => 3079
[bytes] => 2718136
[curr_connections] => 2
[total_connections] => 807
[connection_structures] => 13
[cmd_get] => 9748
[cmd_set] => 3096
[get_hits] => 5976
[get_misses] => 3772
[bytes_read] => 3448968
[bytes_written] => [limit_maxbytes] => 33554432
),
[failed_host:11211] =>
)

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial2.12.Memcache::getStats

2.12.1. Description

array Memcache::getStats ([ string $type [, int $slabid [, int $limit ]]] )

Get the last added server static information

2.12.2. Parameters

type Static information type. Valid values ​​include {reset, malloc, maps, cachedump, slabs, items, sizes}. This optional parameter is agreed according to certain rules. This optional parameter is a title entered to facilitate developers to view different categories of information

slabid is used to jointly set the cache heap to be a valid slice into the heap according to the specified type. The cache heap is command bound to the server and is strictly for debugging purposes

limit is used to jointly set the size of the cache heap to the heap limited by the entered number according to the specified type. The default value is 100

2.12.3. Return value

Returns an array of server static information, returning FALSE on failure

2.13.Memcache::getServerStatus

2.13.1. Description

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

Get the corresponding server information by entering the host and port

2.13.2. Parameters

host server domain name or IP

port port number, default is 11211

2.13.3. Return value

Returns server status, 0 means failure, otherwise returns a non-0 number

2.13.4. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* OO API */
$memcache = new Memcache;
$memcache->addServer('memcache_host', 11211);
echo $memcache->getServerStatus( 'memcache_host', 11211);
/* procedural API */
$memcache = memcache_connect('memcache_host', 11211);
echo memcache_get_server_status($memcache, 'memcache_host', 11211);
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.14.Memcache::getVersion

2.14.1. Description

string Memcache::getVersion (void)

Get the version number information of the server

2.14.2. Return value

Returns the version number string of the server successfully, returns FALSE on failure

2.14.3. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* OO API */
$memcache = new Memcache;
$memcache->connect('memcache_host', 11211);
echo $memcache->getVersion( );
/* procedural API */
$memcache = memcache_connect('memcache_host', 11211);
echo memcache_get_version($memcache);
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.15.Memcache::setCompressThreshold

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

Set compression limit

2.15.2. Parameters

threshold sets the minimum value for controlling the variable length of automatic compression

min_saving specifies the minimum compression ratio. The value must be between 0 - 1. The default is 0.2, which represents a compression ratio of 20%.

2.15.3. Return value

Returns TRUE on success and FALSE on failure.

2.15.4. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* OO API */
$memcache_obj = new Memcache;
$memcache_obj->addServer('memcache_host', 11211);
$memcache_obj->setCompressThreshold(20000 , 0.2);
/* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_set_compress_threshold($memcache_obj, 20000, 0.2);
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.16.Memcache::setServerParams

2.16.1. Description

bool Memcache::setServerParams ( string $host [, int $port [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback ]]]]] )

Function added after Memcache version 2.1.0, set server parameters at runtime

2.16.2. Parameters

host Server domain name or IP

port port number, default is 11211

timeout The number of seconds for timeout connection failure. Please think twice when changing the default value of 1. You may lose all cache advantages and cause the connection to become very slow

retry_interval The retry frequency when the server connection fails. The default is once every 15 seconds. If set to -1, automatic retry will be disabled. When the extension is loaded dynamically via dl(), regardless of this parameter or the constant connection setting parameter All will fail. Each failed server has its own lifetime before it expires. When selecting a backend request, it will be skipped and the request will not be served. An expired connection will be successfully reconnected or marked as failed pending the next retry. This effect means that the retry connections of each web server's child process when serving the page are related to their own retry frequency.

status controls whether the server is marked online. Setting this parameter to FALSE and setting retry_interval to -1 can cause the server that failed to connect to be placed in a server pool that describes the server that does not respond to requests. Requests to this server will fail. Accepts the setting of a failed server. The default parameter is TRUE, which means that the server can be defined as online.

failure_callback The callback function when failure occurs. The two parameters of the function are the hostname and port of the failed server

2.16.3. Return value

Returns TRUE on success and FALSE on failure.

2.16.4. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial function _callback_memcache_failure($host, $port)
{
print "memcache '$host:$port' failed";
}
/* OO API */
$memcache = new Memcache;
// Add the server in offline mode
$memcache->addServer('memcache_host', 11211, FALSE, 1, 1, -1, FALSE);
// Bring the server back online
$memcache->setServerParams('memcache_host', 11211, 1, 15, TRUE, '_callback_memcache_failure');
/* procedural API */
$memcache_obj = memcache_connect ('memcache_host', 11211);
memcache_set_server_params($memcache_obj, 'memcache_host', 11211, 1, 15, TRUE, '_callback_memcache_failure');
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.17.Memcache::increment

2.17.1. Description

int Memcache::increment ( string $key [, int $value ] )

Give an increment to the cache variable of the specified key. If the variable is not a number, it will not be converted into a number. This increment will be added to the original number of the variable. If the variable does not exist, no new variable will be added. For Do not use this function to compress stored variables because the corresponding value method will fail.

2.17.2. Parameters

key key of cached value

var value, integer type will be stored directly, other types will be serialized and stored

2.17.3. Return value

Returns the new variable value successfully, returns FALSE on failure.

2.17.4. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
/* increment counter by 2 */
$current_value = memcache_increment($memcache_obj, 'counter', 2);
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
/* increment counter by 3 */
$current_value = $memcache_obj->increment('counter', 3);
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.18.Memcache::decrement

2.18.2. Description

int Memcache::decrement ( string $key [, int $value ] )

Give a decrement value to the cache variable of the specified key. Similar to the increment operation, this value will be subtracted from the original variable. The value of the item will be subtracted after being converted into a number. The value of the new item will not be less than 0. Do not use this function for compressed stored variables because the corresponding value method will fail.

2.18.2. Parameters

key key of cached value

var value, integer type will be stored directly, other types will be serialized and stored

2.18.3. Return value

Returns the new variable value successfully, returns FALSE on failure.

2.18.4. Example

Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial /* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
/* decrement item by 2 */
$new_value = memcache_decrement($memcache_obj, 'test_item', 2);
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
/* decrement item by 3 */
$new_value = $memcache_obj->decrement('test_item', 3);
?> Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial Use of php to operate memcache [transfer], php to operate memcache_PHP tutorial

2.19.memcache_debug

2.19.1. Description

bool memcache_debug ( bool $on_off )

Set whether the memcache debugger is enabled, the value is TRUE or FALSE. Affected by whether the --enable-debug option is used during php installation, this function will only return TRUE if used, otherwise it will always return FALSE.

2.19.2. Parameters

on_off sets whether debugging mode is on, TRUE is on, FALSE is off

2.19.3. Return value

If php is installed with the --enable-debug option, it returns TRUE, otherwise it returns FALSE.

Redirected to: http://www.cnblogs.com/whoamme/p/3437146.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1116663.htmlTechArticleUsage of php operation memcache [Transfer], php operation memcache 1. Introduction Memcache is a project of danga.com. It was first used to serve LiveJournal. Currently, many people around the world use this cache...
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment