Using the Memcache Cache Mysql database operation principle and caching process analysis _mysql

Source: Internet
Author: User

For large web sites such as Facebook,ebay, if there is no memcache as an intermediate cache layer, data access is not possible do, for the general web site, as long as the independent server, can be configured memcache improve the speed of Web site access and reduce database pressure, Here we discuss the process relationship between Memcache and MySQL database interaction process, understand the Memcache buffer layer function, and understand the memcache mechanism principle deeply.

Memcache and MySQL Interactive flowchart

As shown above, the traditional query method is to query the database directly, the database will return the results to the query statement, and when there is memcache intermediate cache layer, the query is memcache cache data, the following detailed understanding of the Memcache various types of data operation principles:

1. Query data (select), first through the specified key query (get) memcache intermediate cache layer data, if there is relative data, then directly obtain data results, the query process does not need to query the database. If not, the MySQL database is queried and the query results are stored in the Memcache cached data in the form of a key corresponding to value, and the result is returned to the query statement.
2. Update the data, first update the data, and then delete the associated memcache data (delete).
3. Add the data (add), first delete the related cache data, and then add the data.
4. Remove data (delete), delete data, and delete memcache data.

For MySQL data operation, mainly involves the Memcache method as follows:

1. Getting: Get (Key)
2. Set: Set (key, value [, expiry])
3. Delete: Delete (key [, TIME])

Instance:
Suppose a post, get the value of the post ID 2009, its memcache and MySQL interactive process is as follows:

Copy Code code as follows:

$key = ' biuuu_thread_ ';//key prefix
$cachetime = 100;//Cache effective time (seconds)
$id = 2009;

Memcache Basic Operation Process

1. Query: $result = Get ($key. $id); If $result is empty, query the MySQL database, then set ($key. $id, $value, 0, $cachetime)
2. Update: Delete ($key. $id);
3. Add: Delete ($key. $id);
4. Delete: Delete ($key. $id);

Through the visual graph and the example should have the memcache and the MySQL interaction has a foundation grasps, actually for the application, basically already is enough, but if involves the namespace and so on relatively complex situation, the memcache operation Way may be different, but regardless of what kind of operation, Still can not do without the basic Operation command, because this is the Memcache Foundation, mainly discusses Memcache and the MySQL interactive operation Flow, when the memcache principle is clear, memcache basic operation is so simple.

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.