Introduction to the distributed cache system Memcached

Source: Internet
Author: User
Tags php example
The introduction to Memcached on Wikipedia is a distributed cache system. but what does Memcachd mean and what does it do? Cache is generally used to store frequently accessed data and resources.

The introduction to Memcached on Wikipedia is a distributed cache system. but what does Memcachd mean and what does it do? Cache is generally used to save frequently accessed data and resources (for example, a browser caches accessed Web sessions ), because it is much faster to access data through the cache than to access the same data from the disk. Therefore, Memcached, as its name implies, means that all cached content is in the memory of the server. The cached data in the memory can be accessed through APIs. Data exists in key/value pairs, just like a large Hash table.

Distributed is the main feature of Memcached, so you can install Memcached on multiple servers to build a larger cache server. In this way, Memcached can help us minimize the database pressure, so that we can build faster and more scalable WEB applications. Demonstrate how Memcache works with the database.

 

: How Memcache works with databases


How does Memcache work?

I believe that there will be no stranger to those who have developed database applications. Let's explain step by step what happened in the figure:

Check whether the data requested by the user exists in the cache. If yes, you only need to directly return the requested data, which is totally irrelevant to the database.
If the requested data cannot be found in the cache, query the database again. When the request data is returned, the data is stored in the cache.
Keep the cache fresh. when data changes (for example, data is modified or deleted), the cache information must be updated synchronously, make sure that the user does not get the old data in the cache.

Obviously, Memcached can play a major role in high-concurrency data queries and massive data output. Because accessing memory data in Memcached is much faster than the disk data in the database.


How to install Memcache?
If you want to try Memcached on your server, the first step is to install Memcached components on your server. Fortunately, Memcached has been pre-installed on many server releases. You can run the following command in Shell to check whether your server has been preinstalled with Memcached:
?
1 memcached-h
If Memcached has been installed, the above Command will output the installed version number and some help information. Otherwise, an error will be returned.
The following uses the CentOs release as an example to describe how to install Memcached.
Yum install memcached
The above Command will search and install the latest Memcached package online.

When is Memcache used or not used?

When should you use Memcache and avoid using it? Now you know that Memcahced is designed to reduce the pressure on the database. But you 'd better develop a good policy to find a way for Memcached to cache the queries that affect performance as much as possible. You can try to make some execution time logs for all queries in the application to help you analyze the content that is to be cached.
Now suppose you are running an e-commerce website. You can cache product introduction, shipping information, or other data that requires complex queries in Memcached. When a product page is loaded, the data mentioned above will skip the database query and be obtained directly from the cache. The cache can greatly change the overall performance of your website. you only need to remember to update these caches when you update products in the background.
In other cases, caching data is not a good idea. for example, when a data is frequently updated, we need to update the cache at the same time for every data update, the cache hit rate is not high, leading to some additional performance sacrifices. In this case, it may be better to directly query the database.

Security of Memcached
If you understand the Memcached workflow, you may have noticed that there is no permission control flow during the cache access process. If your data is not very important, you don't have to worry about security issues. If you need it, the following points can help you fully use it:

Unique Key: because data in Memcached exists in a large array, you should use a unique key. The only way to access your data is by using the key when you save the data, there is no other way to query it.
Ensure the security of your Memcached: because Memcached does not have an authentication mechanism, you should use the firewall to query Memcached servers. You can set rules on the firewall, which servers are allowed to be accessed and which are not allowed to be accessed.
Encrypt your data: you can save the data and Key in Memcached encrypted mode. This takes some extra CPU time, but for your data security, this method is worth trying if it is allowed.


Memcached code instance
Let's look at a real PHP example. The following code retrieves data from the cache. if the data does not exist, it automatically queries the required data from the database and saves the queried data to the cache.
To use the above code on your website, first confirm that you have installed the PHP extension of Memcached. You can install the corresponding development kit through PECL.
?
1234567891011121314151617181920212223242526272829303132 class MyCache {private $ cache; function _ construct () {$ this-> cache = new Memcache (); // you can replace localhost by Memcached server IP addr and port no. $ this-> cache-> connect ('localhost', 10987);} function get_data ($ key) {$ data = $ this-> cache-> get ($ key ); if ($ data! = Null) return $ data; else {if ($ this-> cache-> getResultCode () = Memcached: RES_NOTFOUND) {// do the databse query here and fetch data $ this-> cache-> set ($ key, $ data_returned_from_database);} else {error_log ('No data for key '. $ key) ;}}$ cache = MyCache (); $ cache-> get_data ('Foo ');


Summary
Memcached is a powerful tool that helps you build large WEB applications such as Wikipedia, Flickr, and Digg. With Memcached, you can easily improve the performance of your website. What are you waiting?
This article is reproduced from <这一客> Http://www.geekso.com

 

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.