Memcache functions in PHP

Source: Internet
Author: User
Tags pconnect
Memcache functions in PHP Memcache functions are listed as follows:

Memcache: add? Add a value. if it already exists, false is returned.

Memcache: addServer? Add an available server address

Memcache: close? Disables a Memcache object.

Memcache: connect? Create a Memcache object

Memcache_debug? Control debugging

Memcache: decrement? Deletes the value of a saved key.

Memcache: delete? Delete a key value

Memcache: flush? Clear all cached data

Memcache: get? Get a key value

Memcache: getExtendedStats? Obtains the running system statistics of all processes in the process pool.

Memcache: getServerStatus? Obtain the parameters of the running server

Memcache: getStats? Returns the running statistics of the server.

Memcache: getVersion? Returns the version information of the running Memcache instance.

Memcache: increment? Adds the value of a saved key.

Memcache: pconnect? Creates a persistent connection object for Memcache.

Memcache: replace-overwrite an existing key

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

Memcache: setCompressThreshold? Compress data larger than a certain size

Memcache: setServerParams? Modify server parameters at runtime


Memcache: add usage


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

Note:

If $ key does not exist, use this function to store the value of $ var. The function with the same functions is memcache_add ().

Parameters:

$ Key: the key value to be stored.

$ Var: the stored values, values, and integer types are saved based on the original values. Other types are automatically serialized and saved.

$ Flag: whether to use MEMCACHE_COMPRESSED to compress the stored value. true indicates compression, and false indicates no compression.

$ Expire: the expiration time of the stored value. if it is 0, it indicates that it will not expire. you can use a unix timestamp or description to indicate the time from now on, however, when you use the number of seconds, do not exceed 2592000 seconds (30 days ).

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned. If the $ key value already exists, FALSE is returned. In other cases, the usage of Memcache: add () is similar to that of 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_interval [, bool $ status [, callback $ failure_callback])

Note:

Add an available server address to the connection pool, and enable the connection with Memcache: addServer. the connection is automatically closed after the script is executed, or you can use Memcache: close () to manually close the connection pool. The same function is memcache_add_server ().

When this method is used (compared with the Memcache: connect () and Memcache: pconnect () methods), the network connection is established only when necessary, therefore, the system load will not be increased because many servers are added to the connection pool, because many servers may not be used.

Fault recovery will occur at any stage of the method execution. as long as other servers are normal, the failed users of these connection requests will not notice it. Any socket or memcached server-level error can trigger fault recovery. Normal client errors, such as adding an existing key value, will not cause fault recovery.

Parameters:

$ Host server address

$ Port server port

$ Persistent is a persistent connection

$ Weight of this server among all servers

$ Timeout connection duration

$ Retry_interval specifies the retry interval of the connection. the default value is 15. if the value is-1, no retry is performed.

$ Status controls the online status of the server

$ Failure_callback allows you to set a callback function to handle error messages.

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

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)

Note:

Disable the memcache server connection. This function does not close persistent connections. persistent connections are closed only when the web server is closed or restarted. Same function memcache_close ()

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

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

Note:

Open the memcached server connection and establish a connection to the memcached server. the connection opened with Memcache: connect will be automatically closed after the script is executed. You can also use Memcache: close () to close the connection. The same function is memcache_connect ().

Parameters:

$ Host: the host that points to the link memcached is listening to. this parameter has another special connection method unix: // path/to/memcached. sock, that is, the unix domain name sockets. in this case, the port must be set to 0.

$ Port: The port pointing to the link memcached is listening to. when the unix domain name sockets is used, the port must be set to 0.

$ Timeout: the number of seconds used to connect to the Daemon. when you change the default value of 1 second, consider it. if your connection is too slow, you may lose the cache advantage.

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

Example:


$ Memcache_obj = memcache_connect ('memcache _ host', 11211 );
$ Memcache = new Memcache;
$ Memcache-> connect ('memcache _ host', 11211 );

?>

Memcache: debug

Bool memcache_debug (bool $ on_off)

Note:

Control the debugging function, provided that the-enable-debug option is used during php compilation, otherwise this function will not work.

Parameters:

$ On_off: true indicates that debugging is enabled, and false indicates that debugging is disabled.

Return value:

If the-enable-debug option is used during php compilation, true is returned; otherwise, false is returned.

Memcache: decrement usage

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

Note:

The Memcache: decremen method is used to subtract values from a saved key. its usage is similar to that of Memcache: increment.

You can also use the memcache_decrement () function.

Parameters:

Key: name of the Key to be removed

Value: The Value to be reduced.

Return value:

If the operation succeeds, the value after reduction is returned. if the operation fails, false is returned.

Example:


$ Memcache = new Memcache;

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

$ Memcache-> set ('test _ item', 8 );

$ Memcache-> increment ('test _ item', 4 );

Echo $ memcache-> decrement ('test _ item', 7 );

// Display 5

?>

In this example, the Memcache: increment function is used together.

Memcache: delete usage

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

Note:

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

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

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)

Note:

Clear all cached data. Memcache: flush does not actually release resources. it only marks all the caches as expired, so that the new cache can overwrite the occupied memory space. The same function is memcache_flush ()

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

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

Note:

The function of the method is to obtain a key value. The key value can be an array and the result contains a key-value pair.

Parameters:

$ Key is the key value or the array value of a key.

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

Return value:

If the operation succeeds, the value corresponding to the key is returned. if the operation fails, false is returned.

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

Note:

Obtains the running system statistics of all processes in the process pool. The same function is memcache_get_extended_stats ()

Parameters:

$ Type indicates the type to be returned: reset, malloc, maps, cachedump, slabs, items, sizes;

$ Used when the first slabid parameter is set to "cachedump.

$ Limit is used when the first parameter is set to "cachedump.

Return value:

If the call succeeds, the system returns statistics. if the call fails, the system returns false.

Example:

$ Memcache_obj = new Memcache;
$ Memcache_obj-> addServer ('memcache _ host', 11211 );
$ Memcache_obj-> addServer ('failed _ host', 11211 );

$ Stats = $ memcache_obj-> getExtendedStats ();

// The slabs mechanism allocates management memory

$ Statsslab = $ memcache_obj-> getExtendedStats (slabs );

?>

Memcache: getServerStatus

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

Note:

Obtain the parameters of the running server. Returns the online or offline status of a server. The same function is memcache_get_server_status ()

Parameters:

$ Host: host of the connection being listened

$ Port: The port of the host that is listening to. the default value is 11211.

Return value:

The server status is returned successfully. if the server is not started, 0 is returned. if other numbers are returned, the server is started.

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

Note:

Return some running statistics of the server. The same function is memcache_get_stats ()

Parameters:

$ Type indicates the type to be returned: reset, malloc, maps, cachedump, slabs, items, sizes;

$ Used when the first slabid parameter is set to "cachedump.

$ Limit is used when the first parameter is set to "cachedump.

Memcache: getVersion

String Memcache: getVersion (void)

Note:

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

Return value:

The version of the server is returned. if the version fails, false is returned.

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

Adds the value of a saved key.

For usage instructions, refer to Memcache: decrement.

Memcache: pconnect

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

Note:

Creates a persistent connection object for Memcache.

Usage is similar to Memcache: connect (). The difference is that Memcache: pconnect is a persistent connection established. This connection will not be closed either after the script is executed or when the Memcache: close () function is run. The same function as memcache_pconnect ()

Parameters:

$ Host: the host that points to the link memcached is listening to. this parameter has another special connection method unix: // path/to/memcached. sock, that is, the unix domain name sockets. in this case, the port must be set to 0.

$ Port: The port pointing to the link memcached is listening to. when the unix domain name sockets is used, the port must be set to 0.

$ Timeout: the number of seconds used to connect to the Daemon. when you change the default value of 1 second, consider it. if your connection is too slow, you may lose the cache advantage.

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.


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

Note:

Overwrites an existing key. The same function is memcache_replace ()

Parameters:

$ Key: the key value to be stored.

$ Var: the stored values, values, and integer types are saved based on the original values. Other types are automatically serialized and saved.

$ Flag: whether to use MEMCACHE_COMPRESSED to compress the stored value. true indicates compression, and false indicates no compression.

$ Expire: the expiration time of the stored value. if it is 0, it indicates that it will not expire. you can use a unix timestamp or description to indicate the time from now on, however, when you use the number of seconds, do not exceed 2592000 seconds (30 days ).

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned. If the $ key value already exists, FALSE is returned.


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

Note:

Add a value. if it already exists, overwrite it. The same function is memcache_set ()

Parameters:

$ Key: the key value to be stored.

$ Var: the stored values, values, and integer types are saved based on the original values. Other types are automatically serialized and saved.

$ Flag: whether to use MEMCACHE_COMPRESSED to compress the stored value. true indicates compression, and false indicates no compression.

$ Expire: the expiration time of the stored value. if it is 0, it indicates that it will not expire. you can use a unix timestamp or description to indicate the time from now on, however, when you use the number of seconds, do not exceed 2592000 seconds (30 days ).

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

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

Note:

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

Parameters:

The setCompressThreshold method has two parameters. The first parameter indicates the critical point for processing data size, and the second parameter indicates the compression ratio. the default value is 0.2.

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

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 [, callback $ failure_callback])

Note:

Modify server parameters at runtime. The same function is memcache_set_server_params ().

Parameters:

$ Host server address

$ Port server port

$ Timeout connection duration

$ Retry_interval specifies the retry interval of the connection. the default value is 15. if the value is-1, no retry is performed.

$ Status controls the online status of the server

$ Failure_callback allows you to set a callback function to handle error messages.

Return value:

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

Example:


Function _ callback_memcache_failure ($ host, $ port ){
Print "memcache '$ host: $ port' failed ";
}

$ Memcache = new Memcache;

// Add a server in offline mode
$ Memcache-> addServer ('memcache _ host', 11211, false, 1, 1,-1, false );

// Set the server to online
$ Memcache-> setServerParams ('memcache _ host', 11211, 1, 15, true, '_ callback_memcache_failure ');

$ Memcache_obj = memcache_connect ('memcache _ host', 11211 );
Memcache_set_server_params ($ memcache_obj, 'memcache _ host', 11211, 1, 15, 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.