This article Reprinted from http://www.geekso.com
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. Figure 1 shows how Memcache works with the database.
Figure 1: How Memcache works with databases
How does Memcache work?
Figure 1 shows the process. I believe that anyone who has developed database applications will be familiar with it. 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
And install the latest Memcached package.
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.
Code
1 <?php
2 class MyCache {
3 private $cache;
4 function __construct() {
5 $this->cache = new Memcache(); // you can replace localhost by Memcached server IP addr and port no.
6 $this->cache->connect('localhost', 10987);
7 }
8
9 function get_data($key) {
10 $data = $this->cache->get($key);
11 if($data != null)
12 return $data;
13 else {
14 if($this->cache->getResultCode() == Memcached::RES_NOTFOUND) {//do the databse query here and fetch data
15
16 $this->cache->set($key,$data_returned_from_database);
17 }else {
18 error_log('No data for key '.$key);
19 }
20 }
21 }
22 }
23 $cache = MyCache(); $cache->get_data('foo');
24 ?>
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?
Translation: avenge
Link: http://www.developer.com/features/article.php/52691_3901666_1/Getting-Started-with-Memcached-Distributed-Memory-Caching.htm