Skip to content

Commit

Permalink
Switched caching system over to JSON encode/decode. It's just about a…
Browse files Browse the repository at this point in the history
…s fast, if not faster, easier to read, and very portable.
  • Loading branch information
jaywilliams committed Dec 14, 2009
1 parent 6acf070 commit 09e84ff
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lastfmcache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Handle the JSON formatted Last.fm API, and Cache the results to files.
*
* Version:
* 2009.12.11
* 2009.12.13
*
* Copyright:
* 2009 Jay Williams
Expand Down Expand Up @@ -184,8 +184,8 @@ public function request($url)
// If cache exists, and is still valid, load it
if($this->cache_mode && file_exists($cache) && (time() - filemtime($cache)) < $this->cache_ttl)
{
$response = (array) $this->parse_response(file_get_contents($cache));
$response['_cached'] = true; // Add notice that this is a cached file
$response = (object) json_decode(file_get_contents($cache));
$response->_cached = true; // Add notice that this is a cached file

return $response;
}
Expand All @@ -197,15 +197,15 @@ public function request($url)
$http->set_useragent(LASTFM_USERAGENT);
$http->send_request();

$response = (array) $this->parse_response($http->get_response_body());
$response = (object) $this->parse_response($http->get_response_body());

if ($this->header_mode)
$response['_header'] = $http->get_response_header();
$response->_header = $http->get_response_header();

// Cache only successfuly requests
if ($this->cache_mode && $response['stat'] == 'ok')
if ($this->cache_mode && !isset($response->error))
{
file_put_contents($cache . '_tmp', serialize($response));
file_put_contents($cache . '_tmp', json_encode($response));
rename($cache . '_tmp', $cache);
}

Expand Down

0 comments on commit 09e84ff

Please sign in to comment.