Home >Backend Development >PHP Tutorial >Memcache caching is actually not as good as direct File file caching?
Use the local environment to test cache read and write 100,000 times and 1 million times. The test environment and results are as follows.
Environment
<code>W<span>in</span>7 x64 AMD7750双核 内存<span>8</span>G Apache <span>2.4</span>.<span>9</span> PHP <span>5.5</span>.<span>12</span> ts vc11 memcache <span>2.2</span>.<span>7</span></code>
Code
<code><span><?php</span><span><span>function</span><span>convert</span><span>(<span>$size</span>)</span> {</span><span>$unit</span> = <span>array</span>(<span>'b'</span>, <span>'kb'</span>, <span>'mb'</span>, <span>'gb'</span>, <span>'tb'</span>, <span>'pb'</span>); <span>return</span> @round(<span>$size</span> / pow(<span>1024</span>, (<span>$i</span> = floor(log(<span>$size</span>, <span>1024</span>)))), <span>2</span>) . <span>' '</span> . <span>$unit</span>[<span>$i</span>]; } <span><span>function</span><span>cacheFile</span><span>(<span>$key</span>)</span> {</span><span>$dir</span> = <span>'cache'</span>; <span>if</span> (!is_dir(<span>$dir</span>) && !mkdir(<span>$dir</span>)) { <span>throw</span><span>new</span><span>Exception</span>(<span>" can't make dir $dir"</span>); } <span>$filepath</span> = <span>$dir</span> . DIRECTORY_SEPARATOR . sprintf(<span>'%x'</span>, crc32(<span>$key</span>)); <span>if</span> (!(file_exists(<span>$filepath</span>) && (<span>$data</span> = file_get_contents(<span>$filepath</span>)) && !<span>empty</span>(<span>$data</span>))) { <span>$data</span> = date(<span>'Y-m-d H:i:s'</span>); file_put_contents(<span>$filepath</span>, <span>$data</span>); } <span>return</span><span>$data</span>; } <span><span>function</span><span>cacheMem</span><span>(<span>$key</span>)</span> {</span><span>$mem</span> = <span>new</span> Memcache(); <span>$mem</span>->connect(<span>'127.0.0.1'</span>, <span>11211</span>); <span>$data</span> = <span>$mem</span>->get(<span>$key</span>); <span>if</span> (<span>empty</span>(<span>$data</span>)) { <span>$data</span> = date(<span>'Y-m-d H:i:s'</span>); <span>$mem</span>->set(<span>$key</span>, <span>$data</span>); } <span>return</span><span>$data</span>; } <span>$t1</span> = microtime(<span>true</span>); <span>$i</span> = <span>0</span>; <span>$limit</span> = <span>1000</span> * <span>100</span>; <span>//10 万次</span><span>$data</span> = <span>null</span>; <span>while</span> (<span>$i</span> < <span>$limit</span>) { <span>// $data = cacheFile($i);</span><span>$data</span> = cacheMem(<span>$i</span>); <span>$i</span>++; } <span>$timeUse</span> = microtime(<span>true</span>) - <span>$t1</span>; <span>$arr</span> = [ <span>'cost'</span> => sprintf(<span>'%.7fs'</span>, <span>$timeUse</span>), <span>'mem'</span> => convert(memory_get_usage()) ]; var_dump(<span>$arr</span>); </span></code>
Result 10,000 times
<code> 花费时间 内存耗费 File <span>1.9</span><span>sec</span><span>250</span>kb Memcache <span>11</span><span>sec</span><span>250</span>kb</code>
Result 100,000 times
<code> 花费时间 内存耗费 <span>File</span><span>94</span>s <span>251.18</span>KB Memcache 超时<span>120</span>s 报错 </code>
Memcache 100,000 times the test failed, the error message is as follows
<code>Warning: Memcache::connect(): <span>in</span> D:\localhost\speed.php <span><span>on</span><span>line</span><span>37</span></span> Warning: Memcache::<span>get</span>(): No servers added <span>to</span> memcache connection <span>in</span> D:\localhost\speed.php Warning: Memcache::<span>set</span>(): No servers added <span>to</span> memcache connection <span>in</span> D:\localhost\speed.php <span><span>on</span><span>line</span><span>41</span></span> Fatal error: Maximum execution <span>time</span><span>of</span><span>120</span><span>seconds</span> exceeded <span>in</span> D:\localhost\speed.php <span><span>on</span><span>line</span><span>38</span></span></code>
Conclusion
memcache
is used for caching but not yet Direct File file caching is fast. The reason why I did this test is because during the interview, I answered that I did not use this memcache for caching, but directly used File file caching (single server). The result was directly recognized by the technical officer as Very good Rookie
. But I passed this test, even though it is under win, why is the performance of Memcahe not as good as File?
Or is there something wrong with the way I tested? Solution default.fu@foxmail.com
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above has introduced that Memcache caching is not as good as direct File file caching? , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.