Php 如何将JSON对象缓存到单个文件中以加快页面加载时间

Php 如何将JSON对象缓存到单个文件中以加快页面加载时间,php,json,api,caching,youtube,Php,Json,Api,Caching,Youtube,我一直在寻找一种解决方案,将多个JSON对象缓存到服务器上的单个文件中(没有数据库)。这是因为我正在开发一个站点,该站点有来自用户YouTube频道的多个JSON请求,因此加载页面需要一段时间 我的PHP文件的一小段,它从YouTube发出JSON请求(在这个JSON-yt.PHP文件中还有很多JSON请求: // VNM Jar $realUserName = 'TheEnijar'; $data = file_get_contents('http://gdata.youtube.com/fe

我一直在寻找一种解决方案,将多个JSON对象缓存到服务器上的单个文件中(没有数据库)。这是因为我正在开发一个站点,该站点有来自用户YouTube频道的多个JSON请求,因此加载页面需要一段时间

我的PHP文件的一小段,它从YouTube发出JSON请求(在这个JSON-yt.PHP文件中还有很多JSON请求:

// VNM Jar
$realUserName = 'TheEnijar';
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . $realUserName . '?v=2&alt=json');
$data = json_decode($data, true);
$TheEnijar = $data['entry']['yt$statistics'];

// VNM Jinxed
$realUserName = 'OhhJinxed';
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . $realUserName . '?v=2&alt=json');
$data = json_decode($data, true);
$OhhJinxed = $data['entry']['yt$statistics'];

// VNM Pin
$realUserName = 'ImGreenii';
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . $realUserName . '?v=2&alt=json');
$data = json_decode($data, true);
$ImGreenii = $data['entry']['yt$statistics'];

// VNM Zq
$realUserName = 'Zqonalized';
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . $realUserName . '?v=2&alt=json');
$data = json_decode($data, true);
$Zqonalized = $data['entry']['yt$statistics'];

这可能吗?如果可能的话,有人能给我指出正确的方向,或者为我提供一个解决方案,在用户加载页面之前存储JSON请求,并向所有不同的YouTube频道发出JSON请求。

老实说,有很多不同的方法可以做到这一点。最简单的方法是为您的用户创建一个assoc数组数据,将其序列化并写入文件

$dataArray = array();

// ...
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . $realUserName . '?v=2&alt=json');
$dataArray['jinxed'] = $data;

$data = serialize($data);
file_put_contents('cache.txt', $data);
然后把它拉回来:

$data = unserialize(file_get_contents('cache.txt'));
但是,正如我所说的,缓存请求还有很多其他的方法,选择其中一种取决于您对缓存系统的其他需求


UPD:不要忘记缓存的信息有时会过时,因此您必须每隔一段时间更新缓存。要不保存任何附加值(如缓存时间),请使用
filemtime()
用于缓存文件的函数。

老实说,有很多不同的方法可以做到这一点。最简单的方法是为数据创建一个assoc数组,将其序列化并写入文件

$dataArray = array();

// ...
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . $realUserName . '?v=2&alt=json');
$dataArray['jinxed'] = $data;

$data = serialize($data);
file_put_contents('cache.txt', $data);
然后把它拉回来:

$data = unserialize(file_get_contents('cache.txt'));
但是,正如我所说的,缓存请求还有很多其他的方法,选择其中一种取决于您对缓存系统的其他需求


UPD:不要忘记缓存信息有时会过时,因此您必须每隔一定时间更新缓存。要不保存任何附加值(如缓存时间),请为缓存文件使用
filemtime()
函数。

您可以执行以下操作:

<?php

function getCachableContent($url){
    $hash = md5($url);
    $cacheFile = "/tmp/foo-cache/$hash";
    if ( file_exists($cacheFile) and filemtime($cacheFile) < time() - 300 ) {
        $data = file_get_contents($cacheFile);
    } else {
        $data = file_get_contents($url);
        file_put_contents($cacheFile, $data);
    }
    return json_decode($data);
}

$data = getCachableContent('http://gdata.youtube.com/feeds/api/users/' . $realUserName . '?v=2&alt=json');

您可以执行以下操作:

<?php

function getCachableContent($url){
    $hash = md5($url);
    $cacheFile = "/tmp/foo-cache/$hash";
    if ( file_exists($cacheFile) and filemtime($cacheFile) < time() - 300 ) {
        $data = file_get_contents($cacheFile);
    } else {
        $data = file_get_contents($url);
        file_put_contents($cacheFile, $data);
    }
    return json_decode($data);
}

$data = getCachableContent('http://gdata.youtube.com/feeds/api/users/' . $realUserName . '?v=2&alt=json');

谢谢你的回答,我看错了方向。谢谢你的回答,我看错了方向。