Memcache Introduction and usage scenarios

1. What is the memcache

Memcached is an open source, high-performance, distributed memory object caching system, can be applied to a variety of scenarios needs to be cached, its main purpose is to accelerate web applications by reducing the access to the Database. Many Web applications will save the data to RDBMS, application server and data read from the display in the browser. But with the increasing amount of data, centralized access, there will be a heavier burden DB, the database in response to the deterioration of the site show a significant impact on delay.

2. Why use memcache?

memcache general purpose, by the results of a database query cache, reducing the number of database access, to improve the speed of dynamic Web applications, improve scalability. It is based on a direct data memory "key on the" memory for storing a database call, the API call or page reference results, such as strings, objects.

3. memcache client

Memcache is the name of the project, but it is memcached server-side main program file name, which is a project name, a file name is the main program, Memcached Client his client connection program

Memcached Client There are three kinds:
Memcached Client for the Java,
Spymemcached,
xmemcached

These three Client there has been controversies:
Memcached Client for the Java more stable than SpyMemcached, earlier and more widely;
Spymemcached more efficient than the Client for the Java Memcached;
xmemcached better than SpyMemcache concurrent effect.

4.memcached design ideas

4.1, simple key / value storage: server does not care about the meaning and structure of the data itself, as long as the data can be serialized. Store items consists of four "key, expiration date, and optional flags DATA";
4.2, functions are implemented depends on the client half and half on the server: The client is responsible for sending items to the storage server, from the server using the appropriate action to get data and can not connect to the server; the server is responsible for receiving, storing data, and is responsible for the timeout expired data items;
4.3, O (1) the efficiency of O (1) standard: constant, o (1) such that the characteristics of 10,000 and a connection query speed connection is the same. (memcached depends on libevent implement in order to achieve O (1))
4.4, extended data clean-up: By default, Memcached is a LRU cache, while it presses the pre-booking long-overdue clean-up data; but in fact, memcached does not delete any cached data, but no longer seen customers after their expiration; moreover, memcached does not really press deadline to clear the cache, but only long check it when get command arrives;

5.memcached and data access protocol:

The so-called protocols, syntax rules can be understood as its operation (data access), the common data access commands and parameters are as follows:

5.1, set: the value (data value) stored in the specified key (key).
Here Insert Picture Description
5.2, get: indicates a value corresponding key extracted from the memcached
Here Insert Picture Description
5.3, Delete: to delete an existing key (key).
Here Insert Picture Description
5.4, all commands statistics:

1 storage class command: the SET, the Add, the replace, the append, preprepend
2, statistics class command: stats, stats items, Slabs stats, stats sizes
3, clean up the class command: delete, fush_all

memcached has two advantages
a read and write performance is better, faster response than some of the file cache when high concurrency, after all, memcached is to write data to memory, read and write faster than the hard disk to be read from; the other memcached is a component supports clusters with multiple servers can be cached, and shut automatically load balance. When more here memcached server should be used in cache on each server is written different information, so, put the cache data separation, greatly increasing the available memory, avoiding the waste of server memory, isolated You can use the ID into the cache server in the key in the write cache.
memcached also has two major drawbacks
a buffer is limited, general computer memcached caching reach 2G, basically has reached the limit, it will affect the stability of computer services, the situation memcached caching data loss occurs. Another is that when the server is abnormal circumstances, such as power outages, downtime and so on, the cached data will be lost, because the cache data exists in memory. Here it should be noted, memcached is the sole source of information source data as the data but can not, he can not be used as database only as a fast memory access, when memcached downtime, no cached information when the need to make the program the transfer of data from a database as elsewhere. Now the database normally used for cloud database, with respect to the memcached server should be independent, while downtime situation usually does not occur.

6.memcached more commonly used in these places:

6.1, the cache will be a number of small but frequently accessed data or files, and access to a large amount of dynamic pages: general information site for Big Data, such as bidding sites, real estate sites, such as site visits generally larger, for website articles, etc., these are basically a shape, not substantially modified, it does not apply to memcached, you can use the direct file caching. But such sites are generally home visits are more frequent, it is possible to cache information classified on the site to memcached, the same recommendation information on the home page can also be cached in memcached, thus greatly reducing the pressure on the server .
6.2, good results can be cached calculation: For about the last example in such bidding website, under certain classified information may reach several million, at this time, each time you access a list of all need to get about the total number of pieces, if this the database engine is innodb, then get the total number of time may be more than 2s, two seconds for the customer waiting time has undoubtedly relatively long, and general information on bidding sites are written in the first day, the next day release, or write in the evening, and not particularly high real-time requirements, or that the existing data before writing the first mean much, and that this time, we can put the total number of cached in memcached daily updates about the total number of zeros in memcached, thus greatly speeding up access speed.
6.3, can be cached session data (provisional data): for example, social networking sites, online time is longer than the average user, this time if there is data in the database, then each time data is read from, the pressure is undoubtedly very large database herein may be employed memcached cluster, the session id, or other data in a user into different in memcached, thus reducing the write operation of the database, to note here is that: session cache must be updated, consider session valid duration, and the user activity state, to extend the effective duration of the session.
6.4, you can cache information hottest degrees: The real estate site, for example, such as a district listings, search View of particularly hot, sql statement the results of this search can be written in the session, which would also greatly reduce the server pressure, but also improves the customer experience degrees.

Published an original article · won praise 0 · Views 48

Guess you like

Origin blog.csdn.net/RainXu0429/article/details/104689033