Home  >  Article  >  php教程  >  延迟更新_缓存优化

延迟更新_缓存优化

WBOY
WBOYOriginal
2016-06-07 11:45:251136browse

默认的延迟更新,用的是F快速缓存,有个缺点:所有文件都在一个目录下,对于统计页面非常多的站点,可能不太好。比如一个网站上百万个页面需要统计,那么产生的缓存文件数量将是海量的。。
这里做了一点修改,缓存方式改为cache()方法,支持多级目录缓存。
在配置文件中开启子目录缓存即可。
打开:ThinkPHP/Extends/Model/AdvModel.class.php
将lazyWrite函数替换为下面的。protected function lazyWrite($guid,$step,$lazyTime) {<br>         $cache= Cache::getInstance('',array("temp"=>TEMP_PATH.'lazy'));//缓存文件放在runtime/temp/lazy 目录下,可以自定义。<br>         if(false !== ($value = $cache->get($guid))) { // 存在缓存写入数据<br>             if(time()>$cache->get($guid.'_time')+$lazyTime) {<br>                 // 延时更新时间到了,删除缓存数据 并实际写入数据库<br>                 $cache->rm($guid);<br>                 $cache->rm($guid.'_time');<br>                 return $value+$step;<br>             }else{<br>                 // 追加数据到缓存<br>                 $cache->set($guid,$value+$step);<br>                 return false;<br>             }<br>         }else{ // 没有缓存数据<br>             <br>             $cache->set($guid,$step);<br>             // 计时开始<br>             $cache->set($guid.'_time',time());<br>             return false;<br>         }<br>     }

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:简单列表页全选删除Next article:IP地址定位函数