\First, install the memcache extension
First, let's check if our current PHP environment supports memcache extension through phpinfo () function. Create a new file info.php in the root directory of the server and write in the file.
<? php
phpinfo ();
Then enter http: //localhost/info.php in your browser to access, and then find out if there is a memcache extension. Generally, our server does not have the memcache extension installed by default, so we still have to install it ourselves. We first download the php_memcache.dll file from the Internet, and copy the file to the php extension directory (mine is php5 / ext /). Adding this file to the extension directory has not been completed. We need to configure the php.ini file in php Add extension = php_memcache.dll to the file, the php environment will automatically find the php extension directory and add this extension to the php environment. At this time, we restart apache and then visit http: //localhost/info.php to see
memcache.png This shows that our memcache extension is installed! We look at the php manual again and find that there are two ways to use the memcache extension, the first is a process-oriented use, and the other is an object-oriented use, and we generally use the object-oriented way.
Second, the use of memcache
Paste the code directly!
<? php
// Instantiate the memcache class
$ mem = new Memcache;
// connect memcache server (parameters are server IP, port), pconnect--represents continuous connection
$ mem-> connect ('localhost', 11211);
// addserver means add memcache server, multiple memcache servers can implement distributed cache
//$mem->addsever('www.pccxin.com ', 11211);
//$mem->addsever('www.frontu.net ', 11211);
// Add elements to the memcache server
// bool Memcache :: add (string $ key, mixed $ var [, int $ flag [, int $ expire]])
// The parameters are the key name, value (string | array | object), use the MEMCACHE_COMPRESSED tag to compress the data (using zlib), save time (in seconds)
$ mem-> add ('mystr', 'This is my first memcache test!', MEMCACHE_COMPRESSED, 3600);
// $ mem-> add ('mystr', 'This is my first memcache test!', MEMCACHE_COMPRESSED, 3600); // add will not be added repeatedly. If you want to change the value, use replace (), or set
$ mem-> set ('mystr', 'This is my second memcache test!', MEMCACHE_COMPRESSED, 3600); // Save data to the server
// delete an element from the server
// $ mem-> delete ('mystr');
// Clean (delete) all elements that have been stored
// $ mem-> flush ();
// Get the data in memcache
echo $ mem-> get ('mystr'). '<br />';
// Add elements to the memcache server
$ mem-> add ('myarr', array ('1' => 'aaa', '2' => 'bb', '3' => 'cc'), MEMCACHE_COMPRESSED, 3600);
var_dump ($ mem-> get ('myarr'));
echo '<br />';
class Person {
var $ name = 'shawnking';
var $ sex = 'Male';
}
// Add elements to the memcache server
$ mem-> add ('myobj', new Person);
var_dump ($ mem-> get ('myobj'));
echo '<br />';
// Get version information of memcache
echo 'Version:', $ mem-> getVersion ();
// Get the parameter information of memcache
echo '<pre>';
print_r ($ mem-> getStats ());
echo '</ pre>';
// Close the connection to the memcached server. This function does not close the persistent connection. The persistent connection is only closed when the web server is shut down / restarted. Correspondingly, you can also use
$ mem-> close ();
Where does PHP use memcache
a, the data read from the database (select) using memcache processing
Normally, when we visit a page of php, we will connect to the database once and read the data in the database. If the database cannot bear the pressure when the traffic is large, we use memcache. The data will be stored in the memcache server, and an expiration time is set, so that you do not need to go to the database to read the data before the expiration time. This can greatly improve the performance of the website. Up very fast). Below I posted the sample code for using memcache in the database:
<? php
// Instantiate a memcache object
$ mem = new Memcache;
// connect memcache server
$ mem-> connect ('localhost', 11211);
/ **
* Note:
* 1. The same project is installed twice, and the key must have a prefix
* $ key = 'a_test';
* $ key = 'b_test';
* 2. Use the sql statement as a subscript, so that only one copy of the data of the same sql statement is stored in memcache
* /
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.