Original: Memcache installation and simple use under Windows
Memcache is to solve a large number of Web site access, database pressure multiplication of one of the solutions, because of its simple and practical, many sites are now using memcache, but one of memcache shortcomings is the lack of security verification, so generally we will put some traffic, But the data that does not need to be verified is put here, need to use when come here to fetch, have reduced a lot of burden to the database. Generally set an update time, 1 hours or so update.
Windows install and test memcache is the most convenient, Linux just need the corresponding compilation package on the line, need to include Memcache program Files memcached and memcache extension module under PHP php_ Memcache.dll, looking for a lot of online, with these two can start the installation process.
1. Enter the ' c:\memcached\memcached.exe-d install ' installation in the terminal (also known as the cmd Command interface)
2. Re-enter: ' c:\memcached\memcached.exe-d start ' starts. Note: memcached will start automatically every time a service is turned on for Windows. This way the server side is already installed.
3. Download Php_memcache.dll, please find the corresponding PHP version of the file yourself
4. Add a line of ' Extension=php_memcache.dll ' to C:\winnt\php.ini
5. Restart Apache, then check the phpinfo, if there is memcache, then the installation is successful!
The application method is very simple, in the PHP file New Memcache object, then calls the method is enough, the Memcache method is not many, commonly used on those few.
First connect the Memcache server and establish the connection:
$mem = new Memcache;
$mem->connect (' Memcache_host ', 11211);
11211 is the port that is set up, Memcache_host here to put the connected IP.
Set the data by using the Set method:
$mem->set (' key ', ' This is a test! ', 0, 60);
60 is the expiration time, in seconds.
Get data with Get method:,
$val = $mem->get ("MyKey");
Replace data with the Replace method:
$mem ->replace (' key1′, ' This is replace value ', 0 , - );
Delete data using the Delete method:
$ Mem ->delete (' key1′);
You can also store arrays:
$arr = Array (' aaa ', ' bbb ', ' CCC ', ' DDD ') );
$mem ->set (' key2′, $arr, 0, 60 );
An example of practical use:
- <?php
- $sql = ' SELECT * from users ' ;
- $key = MD5 ($sql); //memcached Object identifiers
- if ( !( $datas = $MC ->get ($key))) {
- //The cache data is not obtained in memcached, then the recordset is obtained using a database query.
- echo "n" str_pad ( "Read datas from mysql. ' ,&NBSP;60,&NBSP; ).
- $conn = mysql_connect (' localhost ', ' test ', ' Test ');
- mysql_select_db (' test ');
- $result = mysql_query ($sql);
- while ($row = mysql_fetch_object ($result))
- $datas[] = $row;
- //The result set data obtained in the database is saved to memcached for use on the next visit.
- $mc->add ($key, $datas);
- } else {
- echo &NBSP; str_pad ( "Read datas from memcached. ' ,&NBSP;60,&NBSP; ).
- }
- var_dump ($datas);
- ?>
Memcache installation and simple use under Windows