使用PHP cURL进行缓存

10

我正在使用 PHP 的 cURL 功能从另一个网站获取信息并将其插入到我的页面中。我想知道是否可以在我的服务器上缓存所获取的信息?例如,当访问者请求一个页面时,信息被获取并缓存在我的服务器上,持续 24 小时。接下来,在这 24 小时内,该页面完全通过本地服务提供。当 24 小时过期时,再次请求该信息时,以同样的方式重新获取并缓存。

我目前使用的代码如下:

$url = $fullURL;
$ch = curl_init();    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
$result = curl_exec($ch); 
curl_close($ch); 
echo $result;

这可行吗?谢谢。

7个回答

8
你需要编写或下载一个 PHP 缓存库(例如 可扩展的 PHP 缓存库),并调整你的当前代码,首先查看缓存。
假设你的缓存库有两个名为的函数:
save_cache($result, $cache_key, $timestamp)

并且

get_cache($cache_key, $timestamp)

通过save_cache()函数,您可以将$result保存到缓存中,使用get_cache()函数可以检索数据。

$cache_key应该是md5($fullURL),这是一个唯一的标识符,用于告诉缓存库您要检索什么。

$timestamp表示您希望缓存有效的分钟数/小时数,具体取决于您的缓存库接受的时间单位类型。

现在,您可以在代码中编写类似以下逻辑:

$cache_key = md5($fullURL);
$timestamp = 24 // assuming your caching library accept hours as timestamp

$result = get_cache($cache_key, $timestamp); 
if(!$result){
   echo "This url is NOT cached, let's get it and cache it";
  // do the curl and get $result
  // save the cache:
  save_cache($result, $cache_key, $timestamp);
}
else {
  echo "This url is cached";
}
echo $result;

3
嗨,感谢你的逻辑思维。最终我下载了 Pear Cache_Lite 库,它看起来可以顺利运行。谢谢。 - Matt
1
如果缓存耗时的内容,请考虑在使缓存失效之前获取新内容(这样当300个访问同时尝试获取新版本时,不会对您的服务器造成影响)。在下载新版本时,只需提供旧版本即可。对于缓存的SQL查询也是如此。 - Marki555

4
您可以使用memcache(会话)进行缓存,也可以使用服务器上的文件进行缓存,还可以使用数据库,例如mySQL进行缓存。
file_put_contents("cache/cachedata.txt",$data);

如果您想要写入文件到某个文件夹中,您需要设置该文件夹的权限,否则可能会出现一些错误。

如果您想要从缓存中读取:

if( file_exists("cache/cachedata.txt") )
{ $data = file_get_contents("cache/cachedate.txt"); }
else
{ // curl here, we have no cache
 }

2
Honza的建议使用Nette cache对我非常有用,这是我编写的使用它的代码。如果成功,我的函数将返回HTTP结果,否则返回false。您将需要更改一些路径字符串。
use Nette\Caching\Cache;
use Nette\Caching\Storages\FileStorage;
Require("/Nette/loader.php");

function cached_httpGet($url) {
    $storage = new FileStorage("/nette-cache");
    $cache = new Cache($storage);
    $result = $cache->load($url);
    if ($result) {
        echo "Cached: $url";
    }
    else {
        echo "Fetching: $url";
        $ch = curl_init();    
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_URL, $url); 
        $result = curl_exec($ch); 
        if (curl_errno($ch)) {
            echo "ERROR " .  curl_error($ch) . " loading: $url";
            return false;
        } else
            $cache->save($url, $result, array(Cache::EXPIRE => '1 day'));
        curl_close($ch); 
    }
    return $result;
}

1
使用Nette Cache。它是一个简单易用的解决方案,当然也是线程安全的。

0
我编写了一个相当酷的简单函数,用于存储从Antwan van Houdt的评论(向他致敬)中获取的数据,时间可以设置为1小时或1天。首先,在public_html文件夹中创建名为“zcache”的文件夹,并确保其权限为“755”。
1小时:
if( file_exists('./zcache/zcache-'.date("Y-m-d-H").'.html') )
{ $result = file_get_contents('./zcache/zcache-'.date("Y-m-d-H").'.html'); }
else
{ 
// put your curl here 
    file_put_contents('./zcache/zcache-'.date("Y-m-d-H").'.html',$result);
}

1天:

if( file_exists('./zcache/zcache-'.date("Y-m-d").'.html') )
{ $result = file_get_contents('./zcache/zcache-'.date("Y-m-d").'.html'); }
else
{ 
// put your curl here 
    file_put_contents('./zcache/zcache-'.date("Y-m-d").'.html',$result);
}

不客气


0

如果您没有反对文件系统访问,可以将其存储在文件中。然后,可能会在服务器上使用脚本来检查文件的时间戳是否与当前时间相匹配,并在时间过长时删除它。

如果您无法访问服务器的所有方面,则可以使用上述想法并存储信息的时间戳。每次请求页面时,请根据时间戳进行检查。

如果您遇到文件系统瓶颈问题,可以使用完全存储在RAM中的MySQL数据库。


-3
避免缓存的最佳方法是将时间或任何其他随机元素应用于URL,例如:
$url .= '?ts=' . time();

因此,例如,您可以将
http://example.com/content.php
更改为
http://example.com/content.php?ts=1212434353


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接