Some recent learning about memcache

Source: Internet
Author: User
First, memcache is a high-performance distributed memory object Cache System for dynamic web applications to reduce database load. It caches data and objects in the memory to reduce the number of reads to the database, so as to provide dynamic and database-driven website speeds, with special emphasis on the following: memcache is a cache used to store common information. With it, you do not need to load and process information from slow resources, such as disks or databases. References: http://baike.baidu.com/view/794242.htm

Memcache Configuration:
 Installing and configuring memcache may cause minor problems.  1 Download a stable version of memcache. Click here to download it. find a PECL compressed package of the latest version, put it on a disk, and start it on the CMD console. You must be an administrator. Otherwise, an error is reported (I reported an error at the beginning--). The installation command is as follows: C :/ Memcache.exe-D install and then call C: / Memcache.exe-d start to start the service  2 Step 2: Find the corresponding php_memcache.dll and download it based on your PHP version (important: If you start to download it, you will not be able to use memcache and report various errors). My PHP version is 5. 4 . 4 Version (http://huangqiqing123.iteye.com/blog/1682857), and put the ext in PHP and add a sentence under the php.info configuration file: Extension = Php_memcache.dll  3 Finally, you can test the use of memcache, restart Apache, print the phpinfo () information, search for memcache, and create another PHP file: <? PHP $ mem = New Memcache; // instantiate$ Mem-> connect ('192. 168.0.124 ', 192) or die ("link failed ");//192.168.0.124 (server address, localhost available locally) 11211 (port number)
Echo $ mem-> getversion ();//To obtain the version information, use echo output?>
 
My printed result is:1.2.6 
Simple Example of memcache:
 Hello World $ MEMS = New  Memcache; $ MEMS -> Connect ('localhost', 11211) or die ("link failed" ); $ MEMS -> Set ('test', 'Hello world! '); Echo $ MEMS -> Get ('name'); practical application: first create a mymemcache. php,CodeClass mymemcache extends memcache {  Function  _ Construct () {memcache: addserver ( 'Localhost', 11211) or die ("link failed" ) ;}}$ MEMS = New  Mymemcache (); then include it as needed. For example, in the memcache_set page: require_once 'Memcache. php' ; $ MEMS -> Set ('name', 'mywang'); Echo $ MEMS -> Get ('name' ); On the memcache_get page: require_once 'Memcache. php' ;  If ($ MEMS-> get ('name' ) {Echo $ MEMS -> Get ('name' );}  Else  {  //  Select name from DB  //  Echo $ name;  //  $ MEMS-> set ('name', 'xxx ');
}
Memcache Common Methods:
 
For the PHP memcache API address, click my usual method: memcache: add to add an element memcache: addserver-Add a server memcache: to the server pool under a new key ::Delete-Delete an element memcache: Fetch-capture the next result memcache: Get-retrieve an element memcache: replace-replace the element memcache with an existing key :: set-store an element memcache: flush-all elements in the Void cache memcache: getresultmessage-return the result description message of the last operation memcache :: getresultcode-return the result code of the last operation memcache: getdelayedbykey-request multiple element examples from the specified server to recommend the code for theoretical analysis and recommendation (important)
Memcache usage:
Generally, these steps are:Execute one or more queries to load information formatting from the database suitable for displaying (or further processing) information using or displaying formatted dataWhen using memcache, you canProgramThe logic of is slightly modified:Try to load information from the cache if any, and use the cached version of the information if it does not exist: execute one or more queries to load information formatting from the database. information suitable for display or further processing is stored in the cache. The formatted data reduces the number of times that information is retrieved from the database, this improves the response speed of web pages.
Differences between memcache and memcached:
 
1. Currently, most PHP environments use a memcache version that does not contain D. This version was developed earlier and is developed completely within the PHP framework. The corresponding memcached with D is based on libmemcached. Therefore, memcached provides more comprehensive functions. 2. memcache is native and supports both Oo and non-oo interfaces. Memcached uses libmemcached and only supports the OO interface. 3. memcached also has a lot to praise, that is, the flag is not set during the operation, but has a unified setoption (). Memcached implements more memcached protocols. 4. memcached supports binary protocol, but memcache does not. This means that memcached has higher performance. However, memcached does not currently support persistent connections.
Reference link: memcache Elasticity and availability:
One of the most common questions about memcache is: "What happens if the cache is unavailable ?" As stated in the previous chapter, the information in the cache should not be the only resource of the information. Data stored in the cache must be loaded from other locations. Although the failure to access information from the cache slows down the performance of the application, it should not stop the application from running. The following scenarios may occur: If the memcache service goes down, the application should roll back to loading information from the original data source and display the information in the format required. This application should continue to try to load and store information in memcache. Once the memcache server becomes available, applications should automatically try to store data. There is no need to forcibly reload cached data. You can use standard access to load and populate the cache with information. In the end, the cache will be refilled by the most commonly used data. I reiterate that memcache is the information cache but not the only data source. The unavailability of the memcache server should not end the application, although this means that the performance will decrease before the memcache server returns to normal. In fact, the memcache server is relatively simple, and although it is not absolutely fault-free, the result of its simplicity is that it rarely fails.
Limitations of memcache:
 
1. memcached is not a database, which means that memcache cannot store data permanently and can only exist in the entire lifecycle of the page. 2. memcached is not safe. To ensure optimal performance, memcached does not provide any form of security, neither authentication nor encryption. 3. Although memcached can be used to store data rows loaded from the database, it is actually a query cache, and most databases provide their own query cache mechanism. Other objects, such as images or files in a file system, are the same. Many applications and web servers have some good solutions for such work.
Which data should be cached using memcache and which should not:

1. If the value is too large, it is not suitable. By default, memcache supports only 1 MB of value. In practice, we do not recommend storing large data in memcache because of the serialization and deserialization process, don't underestimate the CPU it consumes

2. It is not suitable if expiration is not allowed. By default, memcache expires for a maximum of 30 days. When the memory usage limit is reached, memcache recycles the minimum amount of data used. If we want to treat it as a static variable, we need to consider this problem and there must be a process of re-initializing the data.

3. memcached is a "distributed" Memory Object cache system. That is to say, applications that do not need to be "distributed" and do not need to be shared, or are simply as small as those with only one server, memcached does not bring any benefits. On the contrary, memcached slows down system efficiency because network connections also require resources, even local UNIX connections. According to my previous test data, the local read/write speed of memcached is dozens of times slower than the direct PHP memory array, while the APC and shared memory modes are similar to direct arrays. It can be seen that using memcached is not cost-effective if it is only a local cache. Memcached is often used as the database front-end cache. Because it has much less overhead than the database, such as SQL parsing and disk operations, and uses memory to manage data, it can provide better performance than directly reading the database, in large systems, the same data is frequently accessed. memcached can greatly reduce the database pressure and improve the system execution efficiency.

4. memcached can be used to store data in various formats, including images, videos, files, and database retrieval results. In addition, memcached has been used in the production environment, it is a waste of data to buffer large data volumes. The same amount of memory cannot store a few data records, so the hit rate will be significantly reduced.

5. Because memcached is static data, it is necessary to clear and extract data from the database at regular intervals, and the refresh time can be determined based on different data requirements.

6. What functions does memcache mainly use?

In fact, I think we can always consider whether distributed caching can be applied to the cache in the memory, however, the main purpose is to block the read requests at the front end or in the middle to release the pressure on the Web server app server and DB.

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.