简单的PHP/cURL缓存


Simple PHP / cURL caching

我通过cURL获取远程页面的内容,我想缓存该页面,以便下次该页面不请求cURL,并从缓存加载。

缓存应在一周后过期。

一些伪代码。

make a temp folder 

before each request check if page exists(with name==hash of URL) in the temp folder,  
if not,  
  fetch the page, 
  hash the URL,  
  save page in temp with the Hashed url file name.
else if exists
  get the temp file content
return contents

更新:代码::

$curl_defaults = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => 0,
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b11) Gecko/20100101 Firefox/4.0b11',
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_AUTOREFERER => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_TIMEOUT => 20,
    CURLOPT_VERBOSE => 0,
    CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_SSL_VERIFYPEER => 0
);
$curl_headers = array();
$curl_headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$curl_headers[] = 'Connection: Keep-Alive';
$curl_headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
function get($url, $data = "") {
    if (strlen($url) < 7) return;
    //echo "'n<br> Sending GET :: $url'n<br>";
    if (is_array($data)) $data = implode("&", $data);
    if (strpos($url, "?") > 5) $url .= "&$data";
    else
        $url .= "?$data";
    global $curl_defaults;
    $rep = array(":", "/", ".", "?", "&", "+", "=");
    $fn = dirname(__FILE__) . "/cache/" . md5(str_replace($rep, "", $url)) . ".txt";
    if (file_exists($fn) && @filesize($fn)>1){   //Add a file time check Here
        //echo "'n<br>From Cache FileSize :: "  . filesize($fn);
        return file_get_contents($fn);
    }

    $ch = curl_init();
    curl_setopt_array($ch, $curl_defaults);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $html = curl_exec($ch);
    @unlink($fn);
    if(strlen($html) > 100)
        write2file($fn, $html);
    return $html;
}

你可能需要做出一些改变。

缓存目录:/缓存/

以下是我解决相同问题的方法:

 <?php
//Set maximum age of cache file before refreshing it
$cacheLife = 15; // in seconds
//Set remote URL
$remoteURL = 'http://bravozulu.bz/';
//Set cacheName (We're assuming it's going to reside in the same directory as the caller script).
$cacheName = '_eventCache.php';
//Build the path to the cache
$cacheFileName = str_replace('/','''', dirname(__FILE__)) . ''''.$cacheName;

//Create Remote Retrieval / Cache Function - Reuse is good.
function FN_CacheEventPage($sourceURL, $destinationFile){
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $sourceURL);
 $fp = fopen($destinationFile, 'w');
 curl_setopt($ch, CURLOPT_FILE, $fp);
 curl_exec ($ch);
 curl_close ($ch);
 fclose($fp);
 $ReturnFileContent = file_get_contents($destinationFile, FILE_USE_INCLUDE_PATH);
 return $ReturnFileContent;
}
//Check to see if the file exists and ut still has Life
if (!file_exists($cacheFileName) or (time() - filemtime($cacheFileName) >= $cacheLife)){
    $cacheFileContent = FN_CacheEventPage($remoteURL, $cacheFileName);
}
else{
// If the cache file isn't there, or is too old, build a new one!   
  $cacheFileContent = file_get_contents($cacheFileName, FILE_USE_INCLUDE_PATH);
}
include($cacheFileName);
?>