Memcache class functions in PHP

Source: Internet
Author: User
Tags server port
The list of Memcache class functions is as follows:

Memcache::add? Adds a value that returns false if it already exists

Memcache::addserver? Add a server address that is available for use

Memcache::close? Close a Memcache object

Memcache::connect? Create a Memcache object

Memcache_debug? Control the Debug function

Memcache::d ecrement? Subtract from a value in a saved key

Memcache::d elete? Delete a key value

Memcache::flush? Clear all cached data

Memcache::get? Get a key value

Memcache::getextendedstats? Get run system statistics for all processes in the process pool

Memcache::getserverstatus? Get parameters to run the server

Memcache::getstats? Returns some running statistics for the server

Memcache::getversion? Returns the version information of the running Memcache

Memcache::increment? To add an addition to a value in a saved key

Memcache::p Connect? To create a Memcache persistent connection object

Memcache::replace-Overwrite an existing key

Memcache::set? Add a value that, if it already exists, overwrite

Memcache::setcompressthreshold? Compress data that is larger than a certain size

Memcache::setserverparams? Modifying the server's parameters at run time


Memcache::add usage


BOOL Memcache::add (String $key, mixed $var [, int $flag [, int $expire]])

Description:

If $key does not exist, use this function to store the value of $var. Functions with the same function are Memcache_add ().

Parameters:

$key: The key value that will be stored.

$var: Stored values, character types and integers are saved as original values, and other types are automatically serialized for later saving.

$flag: Whether the stored value is compressed with memcache_compressed, true means compression, and false means no compression.

$expire: The expiration time of the stored value, if 0 means it will not expire, you can use the Unix timestamp or description to indicate the time from now, but when you use the number of seconds to represent, do not exceed 2.592 million seconds (for 30 days).

return Value:

Returns TRUE if successful, and FALSE if it fails. If the $key value already exists, it returns false. In other cases, the usage of Memcache::add () is similar to Memcache::set ().

Example:


$memcache _obj = Memcache_connect ("localhost", 11211);

Memcache_add ($memcache _obj, ' var_key ', ' Test variable ', false, 30);

$memcache _obj->add (' Var_key ', ' Test variable ', false, 30);

?>

memcache::addserver Usage

BOOL Memcache::addserver (string $host [, int $port [, bool $persistent [, int $weight [, int$timeout [, int $retry _inter Val [, BOOL $status [, Callback $failure _callback]]] []]

Description:

Add an available server address to the connection pool, the connection is opened with Memcache::addserver, the script is closed automatically after execution, or it can be closed manually with Memcache::close (). The same function is Memcache_add_server ().

When using this method (relative to Memcache::connect () and Memcache::p the Connect () method), the network connection will only be established when needed, so it will not increase the system burden by adding a lot of servers to the connection pool. Because many servers may not be used.

Failback occurs at any stage of this method execution, and as long as the other servers are normal, the failed users of these connection requests will not notice. Any type of socket or memcached server-level error can trigger a failure recovery. Normal client-side errors such as adding an existing key value do not cause a failure recovery.

Parameters:

Address of the $host server

$port Server Port

$persistent whether it is a persistent connection

$weight the weight that this server takes on all servers

Duration of $timeout Connection

$retry _interval Connection retry interval, default is 15, set to 1 means no retries

$status control the online status of the server

$failure _callback allows you to set a fallback function to handle error messages.

return Value:

Returns TRUE if successful, and FALSE if it fails.

Example:


$memcache = new Memcache;
$memcache->addserver (' Memcache_host ', 11211);
$memcache->addserver (' memcache_host2′, 11211);

$memcache _obj = memcache_connect (' memcache_host ', 11211);
Memcache_add_server ($memcache _obj, ' memcache_host2′, 11211);

?>

memcache::close Usage

BOOL Memcache::close (void)

Description:

Close the Memcache server connection. This function does not close long connections, and long connections are closed only when the Web server is shut down or restarted. The same function memcache_close ()

return Value:

Returns TRUE if successful, and FALSE if it fails.

Example:


$memcache _obj = memcache_connect (' memcache_host ', 11211);

Memcache_close ($memcache _obj);

$memcache _obj = new Memcache;
$memcache _obj->connect (' Memcache_host ', 11211);

$memcache _obj->close ();

?>

Memcache::connect Usage

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

Description:

Open the Memcached server connection, establish a connection to the memcached server, and the connection opened with Memcache::connect automatically shuts down after the script is executed. You can also use Memcache::close () to close the connection. The same function is Memcache_connect ().

Parameters:

$host: To the host of the link memcached is listening to, this parameter will have another special connection method Unix:///path/to/memcached.sock, that is, with the UNIX domain name sockets, in which case the port must be set to 0

$port: To the port where memcached is listening to the link, the port must be set to 0 with the UNIX domain name sockets

$timeout: The number of seconds to connect to the daemon, when you change the default value of 1 seconds, you need to consider that if your connection is too slow, you may lose the advantage of caching.

return Value:

Returns TRUE if successful, and FALSE if it fails.

Example:


$memcache _obj = memcache_connect (' memcache_host ', 11211);
$memcache = new Memcache;
$memcache->connect (' Memcache_host ', 11211);

?>

memcache::d ebug

BOOL Memcache_debug (bool $on _off)

Description:

Controls debug functionality, provided PHP uses the-enable-debug option when compiling, otherwise this function will not work.

Parameters:

$on _off:true to turn on debugging, false to turn off debugging

return Value:

If PHP uses the-enable-debug option when compiling, returns true, otherwise false

Memcache::d ecrement usage

int Memcache::d ecrement (String $key [, int $value])

Description:

Memcache: The function of the:d Ecremen method is to subtract from the value in a saved key, similar to memcache::increment.

You can also use the memcache_decrement () function.

Parameters:

Key: The name of the keys you want to reduce

Value: The values that you want to decrease.

return Value:

If successful, returns the value after being reduced if the failure returns false.

Example:


$memcache = new Memcache;

$memcache->connect (' localhost ', 11211);

$memcache->set (' Test_item ', 8);

$memcache->increment (' Test_item ', 4);

echo $memcache->decrement (' Test_item ', 7);

Showing 5

?>

This example even memcache::increment functions are shown in a piece.

Memcache::d elete Usage

BOOL Memcache::d elete (String $key [, int $timeout])

Description:

Delete a key value, if the parameter $timeout is set, then the stored value will expire after the set number of seconds, you can also use the function Memcache_delete ()

return Value:

Returns TRUE if successful, and FALSE if it fails.

Example:


$memcache _obj = memcache_connect (' memcache_host ', 11211);

Memcache_delete ($memcache _obj, ' Key_to_delete ', 10);

$memcache _obj = new Memcache;
$memcache _obj->connect (' Memcache_host ', 11211);
$memcache _obj->delete (' Key_to_delete ', 10);

?>

Memcache::flush

BOOL Memcache::flush (void)

Description:

Clears all cached data. Memcache::flush does not actually release resources, it simply marks all caches as expired, which allows the new cache to overwrite the occupied memory space. The same function is Memcache_flush ()

return Value:

Returns TRUE if successful, and FALSE if it fails.

Example:


$memcache _obj = memcache_connect (' memcache_host ', 11211);

Memcache_flush ($memcache _obj);

$memcache _obj = new Memcache;
$memcache _obj->connect (' Memcache_host ', 11211);

$memcache _obj->flush ();

?>

Memcache::get

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

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

Description:

The function of the method is to get a key value, which can be an array, and the result will contain a key-value pair.

Parameters:

$key is the value of a key or an array of keys.

$flags If this parameter exists, then $flags is related to the value written to this parameter, and these $flags are similar to the $flags in the Memcache::set () function.

return Value:

If successful, returns the value corresponding to key and returns False if it fails.

Example:


$memcache _obj = memcache_connect (' memcache_host ', 11211);
$var = Memcache_get ($memcache _obj, ' Some_key ');

$memcache _obj = new Memcache;
$memcache _obj->connect (' Memcache_host ', 11211);
$var = $memcache _obj->get (' Some_key ');

$memcache _obj = memcache_connect (' memcache_host ', 11211);
$var = Memcache_get ($memcache _obj, Array (' Some_key ', ' Another_key '));

$memcache _obj = new Memcache;
$memcache _obj->connect (' Memcache_host ', 11211);
$var = $memcache _obj->get (Array (' Some_key ', ' Second_key '));

?>

Memcache::getextendedstats

Array Memcache::getextendedstats ([string $type [, int $slabid [, int $limit]])

Description:

Gets the running system statistics for all processes in the process pool. The same function is Memcache_get_extended_stats ()

Parameters:

$type indicates the type of return required: RESET, malloc, maps, Cachedump, slabs, items, sizes;

$slabid used when the first parameter is set to "Cachedump".

$limit used when the first parameter is set to "Cachedump".

return Value:

If successful, the statistics are returned, and the failure returns false

Example:

$memcache _obj = new Memcache;
$memcache _obj->addserver (' Memcache_host ', 11211);
$memcache _obj->addserver (' Failed_host ', 11211);

$stats = $memcache _obj->getextendedstats ();

Slabs mechanism allocation of memory management

$statsslab = $memcache _obj->getextendedstats (slabs);

?>

Memcache::getserverstatus

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

Description:

Gets the parameters that run the server. Returns the status of a server online or offline. The same function is Memcache_get_server_status ()

Parameters:

$host: The host of the connection you are listening to

$port the port of the host to which the connection is being tuned, the default is 11211

return Value:

Successful return to the server state, the server does not start will return 0, the other number when the server is a startup state.

Example:


$memcache = new Memcache;
$memcache->addserver (' Memcache_host ', 11211);
echo $memcache->getserverstatus (' Memcache_host ', 11211);

$memcache = Memcache_connect (' memcache_host ', 11211);
Echo memcache_get_server_status ($memcache, ' memcache_host ', 11211);

?>

Memcache::getstats

Array Memcache::getstats ([string $type [, int $slabid [, int $limit]])

Description:

Returns some run statistics for the server. The same function is Memcache_get_stats ()

Parameters:

$type indicates the type of return required: RESET, malloc, maps, Cachedump, slabs, items, sizes;

$slabid used when the first parameter is set to "Cachedump".

$limit used when the first parameter is set to "Cachedump".

memcache::getversion

String memcache::getversion (void)

Description:

Returns the version information of the running memcache. Same function memcache_get_version ()

return Value:

The version information of the server is returned successfully, and false is returned when it fails.

Example:


$memcache = new Memcache;
$memcache->connect (' Memcache_host ', 11211);
echo $memcache->getversion ();

$memcache = Memcache_connect (' memcache_host ', 11211);
echo memcache_get_version ($memcache);

?>

memcache::increment

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

To add an addition to a value in a saved key

Usage reference memcache::d ecrement

Memcache::p Connect

BOOL Memcache::p Connect (string $host [, int $port [, int $timeout]])

Description:

To create a Memcache persistent connection object

Usage is similar to Memcache::connect (), where the difference is memcache::p Connect is a persistent connection established. This connection is not closed when the script finishes or the Memcache::close () function is run. The function that is the same as it is Memcache_pconnect ()

Parameters:

$host: To the host of the link memcached is listening to, this parameter will have another special connection method Unix:///path/to/memcached.sock, that is, with the UNIX domain name sockets, in which case the port must be set to 0

$port: To the port where memcached is listening to the link, the port must be set to 0 with the UNIX domain name sockets

$timeout: The number of seconds to connect to the daemon, when you change the default value of 1 seconds, you need to consider that if your connection is too slow, you may lose the advantage of caching.

return Value:

Returns TRUE if successful, FALSE if unsuccessful


$memcache _obj = memcache_pconnect (' memcache_host ', 11211);

$memcache _obj = new Memcache;
$memcache _obj->pconnect (' Memcache_host ', 11211);

?>

Memcache::replace

BOOL Memcache::replace (String $key, mixed $var [, int $flag [, int $expire]])

Description:

Overwrite an existing key. The same function is Memcache_replace ()

Parameters:

$key: The key value that will be stored.

$var: Stored values, character types and integers are saved as original values, and other types are automatically serialized for later saving.

$flag: Whether the stored value is compressed with memcache_compressed, true means compression, and false means no compression.

$expire: The expiration time of the stored value, if 0 means it will not expire, you can use the Unix timestamp or description to indicate the time from now, but when you use the number of seconds to represent, do not exceed 2.592 million seconds (for 30 days).

return Value:

Returns TRUE if successful, and FALSE if it fails. If the $key value already exists, it returns false.


$memcache _obj = memcache_connect (' memcache_host ', 11211);

Memcache_replace ($memcache _obj, "Test_key", "some variable", false, 30);

$memcache _obj->replace ("Test_key", "some variable", false, 30);

?>

Memcache::set

BOOL Memcache::set (String $key, mixed $var [, int $flag [, int $expire]])

Description:

Adds a value that, if it already exists, is overwrite. The same function is Memcache_set ()

Parameters:

$key: The key value that will be stored.

$var: Stored values, character types and integers are saved as original values, and other types are automatically serialized for later saving.

$flag: Whether the stored value is compressed with memcache_compressed, true means compression, and false means no compression.

$expire: The expiration time of the stored value, if 0 means it will not expire, you can use the Unix timestamp or description to indicate the time from now, but when you use the number of seconds to represent, do not exceed 2.592 million seconds (for 30 days).

return Value:

Returns TRUE if successful, and FALSE if it fails.

Example:

$memcache _obj = new Memcache;

$memcache _obj->connect (' Memcache_host ', 11211);

$memcache _obj->set (' Var_key ', ' Some really big variable ', memcache_compressed, 50);

echo $memcache _obj->get (' Var_key ');

Memcache::setcompressthreshold

BOOL Memcache::setcompressthreshold (int $threshold [, float $min _savings])

Description:

Compress data that is larger than a certain size. The same function is Memcache_set_compress_threshold ()

Parameters:

The Setcompressthreshold method has two parameters, the first parameter represents the critical point of processing the data size, the second parameter represents the scale of the compression, and the default is 0.2.

return Value:

Returns TRUE if successful, and FALSE if it fails.

Example:


$memcache _obj = new Memcache;
$memcache _obj->addserver (' Memcache_host ', 11211);
$memcache _obj->setcompressthreshold (20000, 0.2);

$memcache _obj = memcache_connect (' memcache_host ', 11211);
Memcache_set_compress_threshold ($memcache _obj, 20000, 0.2);

?>

Memcache::setserverparams

BOOL Memcache::setserverparams (string $host [, int $port [, int $timeout [, int$retry_interval [, BOOL $status [, Callba CK $failure _callback]]]

Description:

Modifies the server's parameters at run time. The same function is Memcache_set_server_params ().

Parameters:

Address of the $host server

$port Server Port

Duration of $timeout Connection

$retry _interval Connection retry interval, default is 15, set to 1 means no retries

$status control the online status of the server

$failure _callback allows you to set a fallback function to handle error messages.

return Value:

Returns TRUE if successful, and FALSE if it fails.

Example:


function _callback_memcache_failure ($host, $port) {
Print "Memcache ' $host: $port ' failed ';
}

$memcache = new Memcache;

Offline mode adds a server
$memcache->addserver (' Memcache_host ', 11211, False, 1, 1,-1, false);

Set up the server as an online
$memcache->setserverparams (' Memcache_host ', 11211, 1, A, true, ' _callback_memcache_failure ');

$memcache _obj = memcache_connect (' memcache_host ', 11211);
Memcache_set_server_params ($memcache _obj, ' memcache_host ', 11211, 1, A, true, ' _callback_memcache_failure ');

?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.