PHP Cache Technology: One of the memcache Functions

Source: Internet
Author: User
Tags pconnect server port

 

Transferred from www.5iphp.com

The memcache function library is used in the PECL (PhP extension Community Library) to build a temporary storage area for large-capacity memory data. Its role is evident in distributed systems, otherwise, it is not recommended.

 

The list of all memcache functions is as follows:

Reference http://www.php.net/manual/zh/function.Memcache-add.php

 

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

Memcache: addserver-add an available server address

Memcache: Close-close a memcache object

Memcache: connect-create a memcache object

Memcache_debug-control debugging Function

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-get the parameters of the running server

Memcache: getstats-return some running statistics of the server

Memcache: getversion-returns the version information of the running memcache.

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-compresses data larger than a certain size

Memcache: setserverparams-Modify server parameters at runtime

 

Memcache: Add usage

BoolMemcache: 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: used or notMemcache_compressedTo compress the stored values,TrueIndicates compression,FalseIndicates 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:

<? PHP

$ Memcache_obj = memcache_connect ("localhost", 11211 );

/* Process-oriented API */
Memcache_add ($ memcache_obj, 'var _ key', 'test variable', false, 30 );

/* Object-oriented API */
$ Memcache_obj-> Add ('var _ key', 'test variable', false, 30 );

?>

 

Memcache: addserver usage

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

<? PHP

/* Object-oriented API */

$ Memcache = new memcache;
$ Memcache-> addserver ('memcache _ host', 11211 );
$ Memcache-> addserver ('memcache _ host2', 11211 );

/* Process-oriented API */

$ Memcache_obj = memcache_connect ('memcache _ host', 11211 );
Memcache_add_server ($ memcache_obj, 'memcache _ host2', 11211 );

?>

 

 

Memcache: Close usage

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

<? PHP

/* Process-oriented API */
$ Memcache_obj = memcache_connect ('memcache _ host', 11211 );
/*

Execute some code ..
*/
Memcache_close ($ memcache_obj );

/* Object-oriented API */
$ Memcache_obj = new memcache;
$ Memcache_obj-> connect ('memcache _ host', 11211 );
/*
Execute some code ..
*/
$ Memcache_obj-> close ();

?>

 

 

Memcache: connect usage

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

<? PHP

/* Process-oriented API */

$ Memcache_obj = memcache_connect ('memcache _ host', 11211 );

/* Object-oriented API */

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

?>

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.