sql缓存文件的方法

function sql_cache($query){   //sql 缓存文件形式    2016.3.18.abel
    global $db;
    $file = '../cache/sql_'.md5($query).'.txt';
    $expire = 86400; // 24 小时

    if (file_exists($file) &&
        filemtime($file) > (time() - $expire)) {
            $records = unserialize(file_get_contents($file));
            return $records;
        } else {
            $products = $db->Execute($query);
            while (!$products->EOF) {
                $records[] = $products->fields;

                $products->MoveNext();
            }
            $OUTPUT = serialize($records);
            $fp = fopen($file,"w");
            fputs($fp, $OUTPUT);
            fclose($fp);
            return $records;
        } // end else

}

  $db是框架里的sql类。

posted @ 2016-03-18 15:12  清风飘零  阅读(382)  评论(0编辑  收藏  举报