Why is Memcache better than direct File cache?

Source: Internet
Author: User
: This article mainly introduces how to use Memcache to cache files rather than directly using File files ?, If you are interested in the PHP Tutorial, refer. Use a local environment to test 0.1 million and 1 million cached reads and writes. the test environment and results are as follows.

Environment

Win7 x64 AMD7750 dual-core memory 8 GApache 2.4.9PHP 5.5.12 ts vc11 memcache 2.2.7

Code


  Connect ('2017. 0.0.1 ', 11211); $ data = $ mem-> get ($ key); if (empty ($ data )) {$ data = date ('Y-m-d H: I: s'); $ mem-> set ($ key, $ data);} return $ data ;} $ t1 = microtime (true); $ I = 0; $ limit = 1000*100; // 0.1 million times $ data = null; while ($ I <$ limit) {// $ data = cacheFile ($ I); $ data = cacheMem ($ I); $ I ++ ;}$ timeUse = microtime (true)-$ t1; $ arr = ['cost' => sprintf ('%. 7fs ', $ timeUse), 'mem' => convert (memory_get_usage ()]; var_dump ($ arr );

Result 10 thousand times

Time-consuming memory: File 1.9sec250kbMemcache 11sec250kb

Result 0.1 million times

Time-consuming memory File94s 251.18 KBMemcache timeout 120 s error

Memcache failed to perform the test for 0.1 million times. The error message is as follows:

Warning: Memcache::connect(): in D:\localhost\speed.php online37Warning: Memcache::get(): No servers added to memcache connection in D:\localhost\speed.phpWarning: Memcache::set(): No servers added to memcache connection in D:\localhost\speed.php online41Fatal error: Maximum execution timeof120seconds exceeded in D:\localhost\speed.php online38

Conclusion

memcacheThe cache is used for caching but File files are not directly cached. the reason for this test is that during the interview, I replied that I did not use this memcache for caching, directly use the File cache (a single server), and the result is directly recognizedCainiao. But I passed this test, although it is under win, why is Memcahe performance inferior to File?
Or is there an error in my testing method? Solving default.fu@foxmail.com

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

As mentioned above, why is Memcache better than direct File cache ?, Including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.