php编写的文件型缓存解决方案 http://code.google.com/p/secache/ * 纯php实现, 无须任何扩展,支持php4 / 5 * 使用lru算法自动清理过期内容 * 可以安全用于多进程并发 * 最大支持1G缓存文件 * 使用hash定位,读取迅速 内置测试代码 <?php require('../secache.php'); $cache = new secache; $cache->workat('cachedata'); $times = 3000; $succ = 0; $fail = 0; function microtime_float(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } $begin_time = microtime_float(); for($i=0;$i<$times;$i++){ $key = md5($i); //You must *HASH* it by your self $value = str_repeat('No. <strong>'.$i.'</strong> is <em style="color:red">great</em>! ',rand(1,10)); // must be a *STRING* $cache->store($key,$value); } echo '<h2>Insert x '.$times .' | time = ' .( microtime_float() - $begin_time) .' ms</h2>'; //echo '<hr /><h2>test read</h2>'; $begin_time = microtime_float(); for($i=0;$i<$times;$i+=rand(1,10)){ $key = md5($i); //You must *HASH* it by your self if($cache->fetch($key,$value)){ //echo '<p>'.$key.'=>'.$value.'</p>'; $succ++; }else{ //echo '<p>Data get failed! <b>'.$key.'</b></p>'; $fail++; } } echo '<h2>Read x '.($succ+$fail).' | time = ' .( microtime_float() - $begin_time) .' ms</h2>'; echo '<h3>Success Ratio = '.round($succ*100/($succ+$fail),2).'%</h3>'; ?> 1000条数据 Insert x 1000 | time = 0.43291115760803 ms Read x 177 | time = 0.051084995269775 ms Success Ratio = 100% 2000条数据 Insert x 2000 | time = 0.87314915657043 ms Read x 349 | time = 0.10044813156128 ms Success Ratio = 100% 3000条数据 Insert x 3000 | time = 1.6375269889832 ms Read x 547 | time = 0.15729689598083 ms Success Ratio = 98.35% 5000条数据 Insert x 5000 | time = 3.1987061500549 ms Read x 921 | time = 0.20143699645996 ms Success Ratio = 76.55% 10000条数据 Insert x 10000 | time = 6.6344339847565 ms Read x 1787 | time = 0.21589493751526 ms Success Ratio = 37.6% 随着数据量的增大,读取次数增多,命中率下降明显。 测试环境: 电脑概览 电脑型号 联想 0578A51 操作系统 Microsoft Windows XP Professional (32位/Service Pack 3) CPU (英特尔)Intel(R) Core(TM) i5 CPU M 450 @ 2.40GHz(2394 MHz) 主板 联想 0578A51 内存 2.00 GB ( 1066 MHz) 主硬盘 320 GB (西部数据 WDC WD3200BEVT-08A23T1 已使用时间: 3478小时) 显卡 ATI Mobility Radeon HD 545v (512MB) 显示器 LP140WH1-TLA1 LP140WH1-TLA1 声卡 未知声卡 网卡 Realtek PCIe GBE Family Controller |
|