Memcache installation and simple use under Windows

Source: Internet
Author: User
Tags vars

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:

  1. <?php
  2. $sql  =  ' SELECT * from users '  ;
  3. $key   = MD5 ($sql); //memcached Object identifiers
  4. if  ( !( $datas  =  $MC ->get ($key))) {  
  5. //The cache data is not obtained in memcached, then the recordset is obtained using a database query.   
  6.      echo "n" str_pad ( "Read  datas from mysql. ' ,&NBSP;60,&NBSP; ).
  7. $conn = mysql_connect (' localhost ', ' test ', '  Test ');
  8. mysql_select_db (' test ');
  9. $result = mysql_query ($sql);
  10. while ($row = mysql_fetch_object ($result))
  11. $datas[] = $row;
  12. //The result set data obtained in the database is saved to memcached for use on the next visit.   
  13. $mc->add ($key, $datas);
  14. } else {
  15.       echo &NBSP; str_pad ( "Read  datas from memcached. ' ,&NBSP;60,&NBSP; ).
  16. }
  17. var_dump ($datas);
  18. ?>

Memcache installation and simple use under Windows

Related Article

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.