使用PHP cURL缓存

iyfamqjs  于 2022-11-13  发布在  PHP
关注(0)|答案(7)|浏览(288)

我正在使用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;

这可能吗?谢谢。

omvjsjqw

omvjsjqw1#

您需要编写或下载一个php缓存库(如extensible php caching library等),并调整您当前的代码,以首先查看缓存。
假设您的缓存库有两个函数,分别称为:

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;
v440hwme

v440hwme2#

您可以使用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
 }
xsuvu9jc

xsuvu9jc3#

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;
}
tvmytwxo

tvmytwxo4#

使用Nette Cache。所有你需要的解决方案,简单易用,当然-线程安全。

toiithl6

toiithl65#

如果你不反对文件系统访问,你可以把它存储在一个文件中,然后在服务器上使用一个脚本,检查文件的时间戳是否与当前时间一致,如果文件太旧,就删除它。
如果你不能访问服务器的所有方面,你可以使用上面的想法,并存储一个信息的时间戳。每次页面被请求时,检查时间戳。
如果遇到fs瓶颈问题,可以使用完全存储在RAM中的MySQL数据库。

mfpqipee

mfpqipee6#

我做了一个很酷的简单函数来存储从你的curl中获得的数据1小时或1天的休息Antwan货车Houdt的评论(大声对他说)。.首先在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);
}

不用谢

x4shl7ld

x4shl7ld7#

避免缓存的最佳方法是将时间或任何其他随机元素应用于url,如下所示:
$url .= '?ts=' . time();
所以比如说
http://example.com/content.php
你会
http://example.com/content.php?ts=1212434353

相关问题