dd

延迟更新_缓存优化

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