ecshop源码分析——静态缓存

来源:互联网 发布:青岛海关数据 编辑:程序博客网 时间:2024/06/05 11:32

读缓存:

function read_static_cache($cache_name){    if ((DEBUG_MODE & 2) == 2)    {        return false;    }    static $result = array();//注意这里静态变量的用法   局部的静态变量只会被初始化一次  且初始化的值只能为常量或字符值 详见:http://blog.csdn.net/u010620626/article/details/43965879    if (!empty($result[$cache_name]))    {        return $result[$cache_name];    }    $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';    if (file_exists($cache_file_path))    {        include_once($cache_file_path);        $result[$cache_name] = $data;        return $result[$cache_name];    }    else    {        return false;    }}
写缓存:

function write_static_cache($cache_name, $caches)
{
    if ((DEBUG_MODE & 2) == 2)
    {
        return false;
    }
    $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
    $content = "<?php\r\n";
    $content .= "\$data = " . var_export($caches, true) . ";\r\n";
    $content .= "?>";
    file_put_contents($cache_file_path, $content, LOCK_EX);
}

/includes/lib_goods.php中get_recommend_goods()中  $data = read_static_cache('recommend_goods');  //优先从缓存中读取数据  这种静态缓存的方式能很大程度的缓解数据库的压力

疑问:写下的静态缓存文件位于/temp/static_caches文件夹中   这些文件会在一定的时间后被系统自动的清除   不知道清除是如何实现的  希望有知道的朋友提示我下  谢谢

0 0