Memcache database Information Store to database reduce IO operations

Source: Internet
Author: User
Tags memcached

In the actual application, the result set of the database query is usually saved to memcached, the next access is obtained directly from the memcached, and the database query operation is not done.This can reduce the burden on the database to a large extent. SQL statements are typicallyMD5The value after () as a unique identifierKey. Below is an example of using memcached to cache a result set of a database query (this snippet immediately follows the example code):Copy the code code as follows: <?PHP
Initializing the memcache operation$sql= ' SELECT * from users ';$key=MD5($sql);//memcached Object Identifiersif( ! ($datas=$MC->get ($key)) ) {//If the cached data is not obtained in memcached, the recordset is obtained using a database query. Echo"N".Str_pad(' Read datas from MySQL. ', 60, ' _ '). " N;$conn=mysql_connect(' localhost ', ' test ', ' test ');mysql_select_db(' Test ');$result=mysql_query($sql); while($row=Mysql_fetch_object($result))$datas[] =$row;//saves the result set data obtained in the database to memcached for use on the next visit. $MC->add ($key,$datas);} Else {Echo"N".Str_pad(' Read datas from memcached. ', 60, ' _ '). " N;}Var_dump($datas);?>can see, after using memcached, can reduce database connection, query operation, database load down,The script is also running at a higher speed.

Memcache database Information Store to database reduce IO operations

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.