I am trying to adapt this code to help cache a json file. If I manually place the file in the cache folder the script runs fine. If I refresh the link the file is deleted and a new file is not downloaded.
I am hoping someone can see what I am doing wrong.
Thanks
laser
(note I tried postig this once sorry if it shows up twice)
I also removed my api key from the json url.
<?php
/**
* Request jobs from WU API
*
* Split the request into smaller request chunks (25 results each)
* and then consolidate them into a single array to meet the API
* requirements.
*/
function wu_api_request()
{
file_put_contents("/cache/KHPN.json",
file_get_contents('http://api.wunderground.com/api/xxxxxxxxxxx/geolookup/conditions/q/KHPN.json'));
}
/**
* API Request Caching
*
* Use server-side caching to store API request's as JSON at a set
* interval, rather than each pageload.
*
* @arg Argument description and usage info
*/
function json_cached_api_results( $cache_file = NULL, $expires = NULL ) {
global $request_type, $purge_cache, $limit_reached, $request_limit;
if( !$cache_file ) $cache_file = dirname(__FILE__) . '/cache/KHPN.json';
if( !$expires) $expires = time() - 2*30*30;
if( !file_exists($cache_file) ) die("Cache file is missing:
$cache_file");
// Check that the file is older than the expire time and that it's not
empty
if ( filectime($cache_file) < $expires ||
file_get_contents($cache_file) == '' || $purge_cache &&
intval($_SESSION['views']) <= $request_limit ) {
// File is too old, refresh cache
$api_results = wu_api_request();
$json_results = json_encode($api_results);
// Remove cache file on error to avoid writing wrong xml
if ( $api_results && $json_results )
file_put_contents($cache_file, $json_results);
else
unlink($cache_file);
} else {
// Check for the number of purge cache requests to avoid abuse
if( intval($_SESSION['views']) >= $request_limit )
$limit_reached = " <span class='error'>Request limit
reached ($request_limit). Please try purging the cache later.</span>";
// Fetch cache
$json_results = file_get_contents($cache_file);
$request_type = 'JSON';
}
return json_decode($json_results);
}
json_cached_api_results();
?>
Vivaldi 8.0.4033.46 by Razvan Serea
Vivaldi is a cross-platform web browser built for – and with – the web. A browser based on the Blink engine (same in Chrome and Chromium) that is fast, but also a browser that is rich in functionality, highly flexible and puts the user first. A browser that is made for you. Vivaldi is produced with love by a founding team of browser pioneers, including former CEO Jon Stephenson von Tetzchner, who co-founded and led Opera Software.
Vivaldi’s interface is very customizable. Vivaldi combines simplicity and fashion to create a basic, highly customizable interface that provides everything a internet user could need. The browser allows users to customize the appearance of UI elements such as background color, overall theme, address bar and tab positioning, and start pages. Vivaldi features the ability to "stack" and "tile" tabs, annotate web pages, add notes to bookmarks and much more.
Vivaldi 8.0.4033.46 fixes:
[Chromium] Update to 148.0.7778.263 ESR (includes security fixes from 149.0.7827.102/103)
Download: Vivaldi 64-bit | 139.0 MB (Freeware)
Download: Vivaldi 32-bit | ARM64
View: Vivaldi Home Page | Screenshot
Get alerted to all of our Software updates on Twitter at @NeowinSoftware
I'm surprised they haven't found a way to fix that. As much as I don't like software wasting memory, a file manager is the kind of thing that makes sense to keep running in active memory for super-fast recall. I suspect that is why MS makes their File Explorer part of the main explorer.exe shell, so that it is guaranteed to always be running.
Seriously 1.8gig....so what. Any computer, if you are going to do serious work on, in 2026 should have a mimimum of 16gig and I would say 32-64gig is the way to go.
So again, you'd consider "I'm happy with my LG TV" to be a valid response to an article about PlayStation? I get the "I don't like any of their products" but how does that in any way invoke Linux? It just feels very random.
Question
Laser
I am trying to adapt this code to help cache a json file. If I manually place the file in the cache folder the script runs fine. If I refresh the link the file is deleted and a new file is not downloaded.
I am hoping someone can see what I am doing wrong.
Thanks
laser
(note I tried postig this once sorry if it shows up twice)
I also removed my api key from the json url.
<?php /** * Request jobs from WU API * * Split the request into smaller request chunks (25 results each) * and then consolidate them into a single array to meet the API * requirements. */ function wu_api_request() { file_put_contents("/cache/KHPN.json", file_get_contents('http://api.wunderground.com/api/xxxxxxxxxxx/geolookup/conditions/q/KHPN.json')); } /** * API Request Caching * * Use server-side caching to store API request's as JSON at a set * interval, rather than each pageload. * * @arg Argument description and usage info */ function json_cached_api_results( $cache_file = NULL, $expires = NULL ) { global $request_type, $purge_cache, $limit_reached, $request_limit; if( !$cache_file ) $cache_file = dirname(__FILE__) . '/cache/KHPN.json'; if( !$expires) $expires = time() - 2*30*30; if( !file_exists($cache_file) ) die("Cache file is missing: $cache_file"); // Check that the file is older than the expire time and that it's not empty if ( filectime($cache_file) < $expires || file_get_contents($cache_file) == '' || $purge_cache && intval($_SESSION['views']) <= $request_limit ) { // File is too old, refresh cache $api_results = wu_api_request(); $json_results = json_encode($api_results); // Remove cache file on error to avoid writing wrong xml if ( $api_results && $json_results ) file_put_contents($cache_file, $json_results); else unlink($cache_file); } else { // Check for the number of purge cache requests to avoid abuse if( intval($_SESSION['views']) >= $request_limit ) $limit_reached = " <span class='error'>Request limit reached ($request_limit). Please try purging the cache later.</span>"; // Fetch cache $json_results = file_get_contents($cache_file); $request_type = 'JSON'; } return json_decode($json_results); } json_cached_api_results(); ?>Link to comment
https://www.neowin.net/forum/topic/1159552-help-needed-with-caching-a-json-file/Share on other sites
1 answer to this question
Recommended Posts