首页 > 其他 > 详细

ecshop二次开发系统缓存优化之扩展数据缓存的必要性与方法

时间:2017-11-26 18:29:42      阅读:313      评论:0      收藏:0      [点我收藏+]

1、扩展数据缓存的必要性

  大家都知道ecshop系统使用的是静态模板缓存,在后台可以设置静态模板的缓存时间,只要缓存不过期,用户访问页面就相当于访问静态页面,速度可想而知,看似非常完美,但是ecshop 有一个方法被滥用了,那就是 clear_cache_files() ,该方法会把整个系统的静态模板都清除掉,商家或者系统后台管理员只要在后台修改一下商品,或者修改个其他的东西,就会调用该方法将所有静态缓存都清掉,所以如果有商家频繁的修改商品,那么静态模板缓存其实是形同虚设,系统每次都会重新执行动态程序,对数据库也会产生较大的压力,那么就有必要将一些数据更新要求不高的数据缓存下来

2、扩展方式:

/include/lib_base.php 直接添加如下3个方法
/**
 * 写入数据缓存
 *
 * @param  string   $key    数据文件的名称
 * @param  mix      $value  缓存数据
 * @param  string   $expire 过期时间,如果不传,怎永久有效
 * @param  integer  $type   缓存类型: 0-文件缓存  后期可扩展其他缓存
 */
function write_data_cache($key, $value, $expire=null, $type=0) {
    if(!isset($obj_filecache)) {
        include_once ROOT_PATH . ‘includes/cls_cachefile.php‘;
        static $obj_filecache;
        $obj_filecache = new CacheFile();
    }
    return $res = $obj_filecache->set($key, $value, $expire);
}

/**
 * 读取数据缓存
 *
 * @param  string   $key    数据文件的名称
 * @param  mix      $value  缓存数据
 * @param  integer  $type   缓存类型: 0-文件缓存  后期可扩展其他缓存
 */
function read_data_cache($key) {
    if(!isset($obj_filecache)) {
        include_once ROOT_PATH . ‘includes/cls_cachefile.php‘;
        static $obj_filecache;
        $obj_filecache = new CacheFile();
    }
     return $data = $obj_filecache->get($key);
}


/**
 * 清除数据缓存
 *
 * @param  string   $key    数据文件的名称
 */
function clear_data_cache($key = ‘‘) {
    if(!isset($obj_filecache)) {
        include_once ROOT_PATH . ‘includes/cls_cachefile.php‘;
        static $obj_filecache;
        $obj_filecache = new CacheFile();
    }
    if($key) {
        $res = $obj_filecache->rm($key);
    } else {
        // 没有key 则删除全部缓存
        $cache_path = $obj_filecache->cache_path;
        $res = del_dir($cache_path);
    }
    return $res;
}

在需要用到的地方直接调用相应方法即可

 

2017-11-26 17:42:48       By BJR

ecshop二次开发系统缓存优化之扩展数据缓存的必要性与方法

原文:http://www.cnblogs.com/widgetbox/p/7899648.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!