How to use Memcache to cache mysql database operations and analysis of the cache process _ MySQL

Source: Internet
Author: User
Analysis on the principle and cache process of using Memcache to cache mysql databases

For large websites such as facebook and ebay, if Memcache is not used as the intermediate cache layer, data access cannot be avoided. for general websites, as long as they have independent servers, you can configure Memcache to speed up website access and reduce database pressure. here we will mainly discuss the process relationship between Memcache and MySQL database interaction, and understand the role of the intermediate cache layer of Memcache, this gives you an in-depth understanding of the principles of the Memcache mechanism.

Interaction between Memcache and MySQL

For example, the traditional query method is to directly query the database, and the database returns the results to the query statement. when there is a Memcache intermediate cache layer, the database queries the Memcache Cache data, the following describes how Memcache operates data:

1. to query data (select), first use the specified Key to query data in the cache layer of Memcache. if the corresponding data exists, the data result is obtained directly, you do not need to query the database during the query process. If it does not exist, query the MySQL database, store the query results in the cached data of Memcache in the form of a key value, and then return the results to the query statement.
2. update the data. first, update the data and then delete the related memcache data (delete ).
3. add data. first, delete the cached data and then add data.
4. delete data, delete data, and delete Memcache data.

The Memcache method for data operations on MySQL is as follows:

1. get: get (key)
2. settings: set (key, value [, expiry])
3. delete: delete (key [, time])

Instance:
Assume that a post is used to obtain the value of the Post ID 2009. the interaction process between Memcache and MySQL is as follows:
The code is as follows:
$ Key = 'biuuu _ thread _ '; // key prefix
$ Cachetime = 100; // cache validity period (seconds)
$ Id = 2009;

Basic Memcache operation process

1. query: $ result = get ($ key. $ id); if $ result is empty, query the MySQL database and set ($ key. $ id, $ value, 0, $ cachetime)
2. Update: delete ($ key. $ id );
3. Add: delete ($ key. $ id );
4. delete: delete ($ key. $ id );

Through direct graphs and instances, you should have a basic understanding of the interaction between Memcache and MySQL. In fact, it is enough for applications, but if it involves complicated situations such as namespaces, the operation methods of Memcache may vary, but no matter what kind of operation, basic operation commands are still inseparable. because this is the basic part of Memcache, it mainly discusses the interactive operation process between Memcache and mysql, after the principles of Memcache are clear, the basic operations of Memcache are so simple.

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.