PHP memcache (Classic PHP cache)

Source: Internet
Author: User
Tags pconnect php memcached
The memcache function library is mainly used in the PECL (phpextensioncommunitylibrary ).
It is used to set up a temporary storage area for large-capacity memory data, which plays an obvious role in distributed mode. Otherwise, it is not recommended. I reported an error when installing and running on Ubuntu:
/Usr/local/memcached/bin/memcached: errorwhilelo


The memcache function library is used in the PECL (PhP extension Community Library) to build large-capacity memory data.

The temporary storage area plays an obvious role in the distributed mode. Otherwise, it is not recommended. I installed it on Ubuntu.

When running, an error is reported:
/Usr/local/memcached/bin/memcached: Error while loading shared libraries: libevent-1.4.so.2:

Cannot open shared object file: no such file or directory

Follow the methods in libeven, memcached, and libmemcache installation to use:

Sudo ln-S/usr/local/lib/libevent-1.4.so.2/usr/lib/libevent-1.4.so.2
This bug can be fixed.

Run the new PHP memcached module to log out ";" in/etc/PhP5/CONF. d/memcached. ini and restart Apache.

, Call phpinfo () to display memcached information.

Run:
<? PHP
$ Memcache = new memcache;
$ Memcache-> connect ('localhost', 11211) or die ("cocould not connect ");
$ Version = $ memcache-> getversion ();
Echo "server's version:". $ version ."
\ N ";
?>
<? PHP
$ Memcache = new memcache;
$ Memcache-> connect ('localhost', 11211) or die ("cocould not connect ");
Print_r ($ memcache-> getstats ());
/**
* Array
*(
* [Pid] = & gt; 8052
* [Uptime] = & gt; 9205
* [Time] = & gt; 1205898428
* [Version] => 1.2.5
* [Pointer_size] => 32
* [Rusage_user] = & gt; 0.008000
* [Rusage_system] = & gt; 0.000000
* [Curr_items] => 1
* [Total_items] => 17
* [Bytes] => 57
* [Curr_connections] => 2
* [Total_connections] => 15
* [Connection_structures] => 3
* [Cmd_get] => 9
* [Cmd_set] => 23
* [Get_hits] => 5
* [Get_misses] => 4
* [Evictions] => 0
* [Bytes_read] = & gt; 671
* [Bytes_written] = & gt; 850
* [Limit_maxbytes] = & gt; 10485760
* [Threads] => 1
*)
*/
?>
<? PHP
$ Memcache = new memcache;
$ Memcache-> connect ('localhost', 11211) or die ("cocould not connect ");
$ Memcache-> set ('name', 'Leo ', 0, 30 );
If (! $ Memcache-> Add ('name', 'Susan ', 0, 30 ))
{
Echo 'Susan is exist ';
};
$ Memcache-> Replace ('name', 'on', 0,300 );
Echo $ memcache-> get ('name ');
$ Memcache-> Delete ('name', 5 );
?>
<? PHP
Function _ callback_memcache_failure ($ host, $ port ){
Print "memcache '$ HOST: $ port' failed ";
}
$ Memcache = new memcache;
$ Memcache-> addserver ('192. 168.1.116', 192 );
$ Memcache-> setserverparams ('192. 168.1.116', 192, 1, 15, true,

'_ Callback_memcache_failure ');
Echo $ memcache-> getserverstatus ('192. 168.1.116', 192 );
?>
<? PHP
$ 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
?>

 

/Usr/local/bin/memcached-D-M 10-u root-l 127.0.0.1-P 11211-C 256-P
/Tmp/memcached. PID
Memcached service officially started

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-R: overwrites 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

We recommend that you use an object-oriented method to test this library:

Memcache: The getversion method returns the version information of the running memcache.

Memcache: The getstats method is used to return some running statistics of the server. Memcache: The getstats method has three parameters.

Number. The first parameter indicates the type to be returned: reset, malloc, maps, cachedump, slabs, items, sizes; the second parameter

The parameter and the third parameter are used when the first parameter is set to "cachedump. Memcache: getextendedstats Method

The function is to obtain the running system statistics of all processes in the process pool.

Memcache: the connect method is used to create a memcache object. Memcache: The pconnect method is used to create

Persistent connection object of memcache. Memcache: the close method is used to close a memcache object.

The memcache: Set Method adds a value. The memcache: Set Method has four parameters. The first parameter is the key and the second parameter.

The number is value, and the third parameter is optional, indicating whether to compress and save. The fourth parameter is optional, which is used to set an expiration time for automatic destruction.

. The function of the memcache: add method is similar to that of the memcache: Set method. The difference is that if the return value of the memcache: add method is

False indicates that the key already exists, while the memcache: Set Method directly overwrites the key. Memcache: The get method is used to obtain

A key value. The memcache: Get method has a parameter, indicating the key. Memcache: The replace method is used for an existing

Key for overwriting. The memcache: replace method has four parameters, which act the same as the memcache: Set Method.

The memcache: delete method is used to delete a key value. The memcache: delete method has two parameters. The first parameter indicates the key.

The second parameter is optional, indicating the deletion delay time.

The memcache_debug () function controls the debugging function, provided that PHP uses the-enable-debug option during compilation. No

This function does not work.

The memcache: addserver method adds an available server address. The memcache: addserver method has eight parameters.

Number, except for the first parameter, it is optional. The first parameter indicates the server address, the second parameter indicates the port, and the third parameter indicates the server address.

This parameter indicates whether a persistent connection is established. The fourth parameter indicates the weight of the server on all servers. The fifth parameter table

Indicates the connection duration. The sixth parameter indicates the connection Retry Interval. The default value is 15. The value-1 indicates no retry.

Parameters are used to control the online status of the server. 8th parameters allow setting a return function to handle error messages.
The memcache: setserverparams method is used to modify server parameters at runtime. memcache: setserverparams Method

There are six parameters. The memcache: addserver method lacks the third and fourth parameters. Memcache: getserverstatus Method

It is used to obtain the parameters of the running server. The two parameters represent the address and port respectively.

Memcache: The flush method is used to clear all cached data, but does not reduce the memory space used.

The memcache: increment method is used to add the value of a saved key.

It is used to subtract the value of a saved key.

See: http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2010/0126/3868.html

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.