Principles and tutorials of PHP connection and Memcached manipulation

Source: Internet
Author: User

Principles and tutorials of PHP connection and Memcached manipulation

Memcahced is an open source distributed memory object cache system. Memcached is often used for a slightly larger project to reduce the burden on the database and accelerate the response speed of web applications. There are a lot of methods for installing and using Memcached on the Internet. What I want to talk about in this article is that memcached is actually very simple and not as mysterious as I think, we can simply understand that it is a cache server application, just like you have installed Mysql. After installation, you can use the account and password IP addresses to connect to it.

The home page briefly introduces the principle of memcached

When a user sends a request for the first time, the PHP program writes the accessed data to the Memcached system while accessing the database.

The user sends a req request, and the application sends a data request to the database. The database caches the data to the Memcached server while returning the data to the application.

When the second user request arrives, it will directly read the cache of the Memcached server, rather than the content in the database, thus reducing the burden on the server.

This figure shows that the application directly reads data from Memcached (Mc) for the second request.

The following uses an example to share the basic usage of memcached (a development instance similar to a link ). I believe that through this instance, you can clearly understand this stuff.

The following example assumes that you have installed the memcached service. If it is not installed, refer to this site:

What is memcached? How to Use memcache?

Install memcache in windows

(1) Create a database

The database table used in the instance contains an auto-increment id, a title, and a link field:

CREATE TABLE demos(id INT PRIMARY KEY AUTO_INCREMENT,title VARCHAR(300), link VARCHAR(300), );

(2) program section (Program comments make it easy for you to understand the use of memcached)

<? Phpinclude ('db. php '); $ memcache = new Memcache; $ memcache-> connect ('localhost', 11211) or die ("cocould not connect"); // cache server, all are key-value pairs. Here we set the unique key $ key = md5 ('www .crazyant.net '); $ cache_result = array (); // based on the key, get its value from the Cache Server $ cache_result = $ memcache-> get ($ key); // if the value corresponding to this key exists, this indicates that the cached content exists if ($ cache_result) {// then we can directly retrieve the cached content $ demos_result = $ cache_result ;} else {// if the value data corresponding to this key is not in the cache, it indicates that the request arrived for the first time. // first, we need to retrieve this value from the database $ v = mysql _ Query ("select * from demos order by id desc"); while ($ row = mysql_fetch_array ($ v )) {// The retrieved content is what we need $ demos_result [] = $ row;} // Finally, put the content retrieved from the database to the Memcached cache server, here is the essence of the cache $ memcache-> set ($ key, $ demos_result, MEMCACHE_COMPRESSED, 1200);} // all the previous operations, the expected data foreach ($ demos_result as $ row) {echo '<a href = '. $ row ['link']. '> '. $ row ['title']. '</a>' ;}?>

The following is the code file db. php used to connect to the database.

<?php$mysql_hostname="localhost";$mysql_user="username";$mysql_password="password";$mysql_database="database";$bd=mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");?>
Articles you may be interested in
  • Two Methods for connecting PHP to access database
  • How to view the current running status of memcache through the command line
  • PHP allows multiple second-level domain names on the same server to share SESSION data
  • Install and configure memcache in windows
  • A classic php tutorial book recommended for php beginners
  • Php obtains all the files in the directory and saves the results to the array program.
  • Php function for getting the first letter of Chinese characters and Pinyin (actually usable)
  • Download from Brother's php advanced programming video tutorial

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.