四时宝库

程序员的知识宝库

云南网站建设的Memcache缓存 vs 雲南網站建設直接File文件缓存

云南网站建设的Memcache缓存 vs 雲南網站建設直接File文件缓存


使用本地的环境测试10万次和 100万次 缓存的读写,测试环境和结果如下。

环境

Win7 x64 AMD7750双核 内存8G

Apache 2.4.9

PHP 5.5.12 ts vc11

memcache 2.2.7

云南网站建设的Memcache缓存 vs 雲南網站建設直接File文件缓存

代码

<?php

function convert($size)

{

$unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');

return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];

}

function cacheFile($key)

{

$dir = 'cache';

if (!is_dir($dir) && !mkdir($dir)) {

throw new Exception(" can't make dir $dir");

}

$filepath = $dir . DIRECTORY_SEPARATOR . sprintf('%x', crc32($key));

if (!(file_exists($filepath) && ($data = file_get_contents($filepath)) && !empty($data))) {

$data = date('Y-m-d H:i:s');

file_put_contents($filepath, $data);

}

return $data;

}

function cacheMem($key)

{

$mem = new Memcache();

$mem->connect('127.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; //10 万次

$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);

云南网站建设的Memcache缓存 vs 雲南網站建設直接File文件缓存

结果 1万次

花费时间 内存耗费

File 1.9 sec 250kb

Memcache 11 sec 250kb

结果 10万次

花费时间 内存耗费

File 94s 251.18KB

Memcache 超时120s 报错

Memcache 10万次测试失败,报错内容如下

Warning: Memcache::connect(): in D:localhostspeed.php on line 37

Warning: Memcache::get(): No servers added to memcache connection in D:localhostspeed.php

Warning: Memcache::set(): No servers added to memcache connection in D:localhostspeed.php on line 41

Fatal error: Maximum execution time of 120 seconds exceeded in D:localhostspeed.php on line 38


云南网站建设的Memcache缓存 vs 雲南網站建設直接File文件缓存

2015-8-21 20:56:33 改进

function cacheMem($key)

{

static $mem = null;

if ($mem === null) {

$mem = new Memcache();

$mem->connect('127.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;

}

云南网站建设的Memcache缓存 vs 雲南網站建設直接File文件缓存

测试10万

时间花费 内存消耗

file方式 22.27sec 251KB

memcache方式 17.10sec 264KB

云南网站建设的Memcache缓存 vs 雲南網站建設直接File文件缓存

测试30万

时间花费 内存消耗

file方式 超时120秒

memcache方式 62sec 264KB

云南网站建设的Memcache缓存 vs 雲南網站建設直接File文件缓存

结论

这样情况下,memcache确实比 file 快

文章转载请注明出处:https://www.ynxy999.com/Article/view/id/5045.shtml

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接