Maison >développement back-end >tutoriel php >Memcache缓存居然不如直接File文件缓存?
使用本地的环境测试10万次和 100万次 缓存的读写,测试环境和结果如下。
环境
<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><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> $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>); </code>
结果 1万次
<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>
结果 10万次
<code> 花费时间 内存耗费 <span>File</span><span>94</span>s <span>251.18</span>KB Memcache 超时<span>120</span>s 报错 </code>
Memcache 10万次测试失败,报错内容如下
<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>
结论
memcache
用来做缓存却还没有 直接File文件缓存快,之所以做这个测试,是因为面试的时候我回答自己并没有使用这个memcache 来做缓存,直接使用File文件缓存(单台服务器),结果直接被技术官认定为是很菜鸟
。但我通过这样的测试,虽然是在win下面,可为什么Memcahe性能还不如File ?
还是说我测试的方式存在错误? 求解 default.fu@foxmail.com
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了Memcache缓存居然不如直接File文件缓存?,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。