缓存数据服务器端以获取ajax请求

时间:2016-05-25 17:22:57

标签: php jquery json ajax caching

我正在使用TwitterAPIExchangeInstagram-PHP-API从我的服务器上的两个API请求数据,将它们连接成一个数组并将其构建为JSON,我可以使用简单的GET请求在客户端上使用它在jQuery中。

这一切都很好,但速度很慢。当我将静态JSON数据作为测试请求时,它要快得多。似乎我的服务器上的php脚本每次都运行这个数据(所以从一个空数组开始,使用库提供的包装来发出请求等)。我想做的是缓存/存储这些数据,并且每天只做一次或两次请求。

我的PHP知识不是最好的,所以我确信有一种相对简单的方法。

这是我服务器上的php脚本:

<?php

header('Content-type:application/json;charset=utf-8');

require_once('TwitterAPIExchange.php');
require_once('Instagram.php');

unset($allFeeds);
$allFeeds = array();

$settings = array(
'oauth_access_token' => "XXXX' => "XXXX",
'consumer_key' => "XXXX",
'consumer_secret' => "XXXX"
);

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=XXX';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);

$twitter_response = $twitter->setGetfield($getfield)->buildOauth($url,$requestMethod)->performRequest();


$jsonTweets = json_decode($twitter_response);

foreach ($jsonTweets as $tweet) {

 $tweetObj = new stdClass();
 $tweetObj->type = 'Twitter';
 $tweetObj->created_at = strtotime($tweet->created_at);
 $tweetObj->text = $tweet->text;
 $allFeeds[] = $tweetObj;
}

use MetzWeb\Instagram\Instagram;

$instagram = new Instagram(array(
 'apiKey'      => 'XXXX',
 'apiSecret'   => 'XXXX',
 'apiCallback' => 'XXXX'
));

$token = 'XXX';
$code = $token;
$instagram->setAccessToken($code);

$id_one = 'XXX';
$id_two = 'XXX';

$insta_response_one = $instagram->getUserMedia($id_one, 20);
$insta_response_two = $instagram->getUserMedia($id_two, 10);

foreach ($insta_response_one->data as $insta_one) {
 $instaObjOne = new stdClass();
 $instaObjOne->type = 'Instagram One';
 $instaObjOne->created_at = (int)$insta_one->created_time;

  if (isset($insta_one->caption->text)) {
   $instaObjOne->text = $insta_one->caption->text;
  }

$instaObjOne->img = $insta_one->images->standard_resolution->url;
$allFeeds[] = $instaObjOne;

}

foreach ($insta_response_two->data as $insta_two) {
 $instaObjTwo = new stdClass();
 $instaObjTwo->type = 'Instagram Two';
 $instaObjTwo->created_at = (int)$insta_two->created_time;
 if (isset($insta_two->caption->text)) {
   $instaObjTwo->text = $insta_two->caption->text;
 }
$instaObjTwo->img = $insta_two->images->standard_resolution->url;
 $allFeeds[] = $instaObjTwo;
}

function dateSort($a, $b) {
 return $b->created_at - $a->created_at;
}

usort($allFeeds, "dateSort");

$data = json_encode($allFeeds);

// cache $data here?

echo $data;

?>

我把它带到我的前端是这样的:

$.ajax({
 type: 'GET',
  url: 'path/to/script.php'
  dataType: 'json',
}).done(function(data) {
  // do stuff with data here
  });
}

希望有道理,谢谢

1 个答案:

答案 0 :(得分:1)

这是向代码添加缓存的最简单方法。

它使用file_put_contents('cache.txt',$data)来缓存请求数据,然后在文件的顶部检查当前时间并与上次修改文件进行比较。如果它在24小时前修改,它只使用echo file_get_contents('cache.txt);输出缓存文件的内容并停止脚本。

<?php

header('Content-type:application/json;charset=utf-8');

// Check if file was modified less than 24 hours ago
if ((time() - filemtime('cache.txt')) < 24 * 60 * 60) { 

    // Output contents of cache file and stop script
    echo file_get_contents('cache.txt');
    exit(); 
}

require_once('TwitterAPIExchange.php');
require_once('Instagram.php');

unset($allFeeds);
$allFeeds = array();

$settings = array(
'oauth_access_token' => "XXXX" => "XXXX",
'consumer_key' => "XXXX",
'consumer_secret' => "XXXX"
);

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=XXX';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);

$twitter_response = $twitter->setGetfield($getfield)->buildOauth($url,$requestMethod)->performRequest();


$jsonTweets = json_decode($twitter_response);

foreach ($jsonTweets as $tweet) {

 $tweetObj = new stdClass();
 $tweetObj->type = 'Twitter';
 $tweetObj->created_at = strtotime($tweet->created_at);
 $tweetObj->text = $tweet->text;
 $allFeeds[] = $tweetObj;
}

use MetzWeb\Instagram\Instagram;

$instagram = new Instagram(array(
 'apiKey'      => 'XXXX',
 'apiSecret'   => 'XXXX',
 'apiCallback' => 'XXXX'
));

$token = 'XXX';
$code = $token;
$instagram->setAccessToken($code);

$id_one = 'XXX';
$id_two = 'XXX';

$insta_response_one = $instagram->getUserMedia($id_one, 20);
$insta_response_two = $instagram->getUserMedia($id_two, 10);

foreach ($insta_response_one->data as $insta_one) {
 $instaObjOne = new stdClass();
 $instaObjOne->type = 'Instagram One';
 $instaObjOne->created_at = (int)$insta_one->created_time;

  if (isset($insta_one->caption->text)) {
   $instaObjOne->text = $insta_one->caption->text;
  }

$instaObjOne->img = $insta_one->images->standard_resolution->url;
$allFeeds[] = $instaObjOne;

}

foreach ($insta_response_two->data as $insta_two) {
 $instaObjTwo = new stdClass();
 $instaObjTwo->type = 'Instagram Two';
 $instaObjTwo->created_at = (int)$insta_two->created_time;
 if (isset($insta_two->caption->text)) {
   $instaObjTwo->text = $insta_two->caption->text;
 }
$instaObjTwo->img = $insta_two->images->standard_resolution->url;
 $allFeeds[] = $instaObjTwo;
}

function dateSort($a, $b) {
 return $b->created_at - $a->created_at;
}

usort($allFeeds, "dateSort");

$data = json_encode($allFeeds);

// Cache $data here
file_put_contents('cache.txt', $data);

echo $data;

?>