Skip to content

Commit

Permalink
Merge pull request #9 from mikeMTOL/master
Browse files Browse the repository at this point in the history
Fixed DB caching, moved to JSON communication
  • Loading branch information
dan-coulter committed Jul 7, 2014
2 parents 6a6a805 + 2820d69 commit b5955ce
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions phpFlickr.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,20 @@ function enableCache ($type, $connection, $cache_expire = 600, $table = 'flickr_
*/
mysqli_query($db, "
CREATE TABLE IF NOT EXISTS `$table` (
`request` CHAR( 35 ) NOT NULL ,
`response` MEDIUMTEXT NOT NULL ,
`expiration` DATETIME NOT NULL ,
INDEX ( `request` )
`request` varchar(128) NOT NULL,
`response` mediumtext NOT NULL,
`expiration` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY `request` (`request`)
)
");

$result = mysqli_query($db, "SELECT COUNT(*) FROM $table");
$result = mysqli_fetch_row($result);
if ( $result[0] > $this->max_cache_rows ) {
mysqli_query($db, "DELETE FROM $table WHERE expiration < DATE_SUB(NOW(), INTERVAL $cache_expire second)");
$result = mysqli_query($db, "SELECT COUNT(*) 'count' FROM $table");
if( $result ) {
$result = mysqli_fetch_assoc($result);
}

if ( $result && $result['count'] > $this->max_cache_rows ) {
mysqli_query($db, "DELETE FROM $table WHERE CURRENT_TIMESTAMP > expiration");
mysqli_query($db, 'OPTIMIZE TABLE ' . $this->cache_table);
}
$this->cache = 'db';
Expand Down Expand Up @@ -132,6 +135,7 @@ function getCached ($request)
//Checks the database or filesystem for a cached result to the request.
//If there is no cache result, it returns a value of false. If it finds one,
//it returns the unparsed XML.
unset($request['api_sig']);
foreach ( $request as $key => $value ) {
if ( empty($value) ) unset($request[$key]);
else $request[$key] = (string) $request[$key];
Expand All @@ -141,10 +145,10 @@ function getCached ($request)
$this->cache_key = $reqhash;
$this->cache_request = $request;
if ($this->cache == 'db') {
$result = mysqli_query($this->cache_db, "SELECT response FROM " . $this->cache_table . " WHERE request = '" . $reqhash . "' AND DATE_SUB(NOW(), INTERVAL " . (int) $this->cache_expire . " SECOND) < expiration");
if ( mysqli_num_rows($result) ) {
$result = mysqli_query($this->cache_db, "SELECT response FROM " . $this->cache_table . " WHERE request = '" . $reqhash . "' AND CURRENT_TIMESTAMP < expiration");
if ( $result && mysqli_num_rows($result) ) {
$result = mysqli_fetch_assoc($result);
return $result['response'];
return urldecode($result['response']);
} else {
return false;
}
Expand Down Expand Up @@ -174,15 +178,18 @@ function cache ($request, $response)
$reqhash = md5(serialize($request));
if ($this->cache == 'db') {
//$this->cache_db->query("DELETE FROM $this->cache_table WHERE request = '$reqhash'");
$result = mysqli_query($this->cache_db, "SELECT COUNT(*) FROM " . $this->cache_table . " WHERE request = '" . $reqhash . "'");
$result = mysqli_fetch_row($result);
if ( $result[0] ) {
$sql = "UPDATE " . $this->cache_table . " SET response = '" . str_replace("'", "''", $response) . "', expiration = '" . strftime("%Y-%m-%d %H:%M:%S") . "' WHERE request = '" . $reqhash . "'";
mysqli_query($this->cache_db, $sql);
} else {
$sql = "INSERT INTO " . $this->cache_table . " (request, response, expiration) VALUES ('$reqhash', '" . str_replace("'", "''", $response) . "', '" . strftime("%Y-%m-%d %H:%M:%S") . "')";
mysqli_query($this->cache_db, $sql);
$response = urlencode($response);
$sql = 'INSERT INTO '.$this->cache_table.' (request, response, expiration)
VALUES (\''.$reqhash.'\', \''.$response.'\', TIMESTAMPADD(SECOND,'.$this->cache_expire.',CURRENT_TIMESTAMP))
ON DUPLICATE KEY UPDATE response=\''.$response.'\',
expiration=TIMESTAMPADD(SECOND,'.$this->cache_expire.',CURRENT_TIMESTAMP) ';

$result = mysqli_query($this->cache_db, $sql);
if(!$result) {
echo mysqli_error($this->cache_db);
}

return $result;
} elseif ($this->cache == "fs") {
$file = $this->cache_dir . "/" . $reqhash . ".cache";
$fstream = fopen($file, "w");
Expand Down Expand Up @@ -270,7 +277,7 @@ function request ($command, $args = array(), $nocache = false)
}

//Process arguments, including method and login data.
$args = array_merge(array("method" => $command, "format" => "php_serial", "api_key" => $this->api_key), $args);
$args = array_merge(array("method" => $command, "format" => "json", "nojsoncallback" => "1", "api_key" => $this->api_key), $args);
if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token));
} elseif (!empty($_SESSION['phpFlickr_auth_token'])) {
Expand All @@ -279,7 +286,8 @@ function request ($command, $args = array(), $nocache = false)
ksort($args);
$auth_sig = "";
$this->last_request = $args;
if (!($this->response = $this->getCached($args)) || $nocache) {
$this->response = $this->getCached($args);
if (!($this->response) || $nocache) {
foreach ($args as $key => $data) {
if ( is_null($data) ) {
unset($args[$key]);
Expand All @@ -295,13 +303,14 @@ function request ($command, $args = array(), $nocache = false)
$this->cache($args, $this->response);
}


/*
* Uncomment this line (and comment out the next one) if you're doing large queries
* and you're concerned about time. This will, however, change the structure of
* the result, so be sure that you look at the results.
*/
//$this->parsed_response = unserialize($this->response);
$this->parsed_response = $this->clean_text_nodes(unserialize($this->response));
$this->parsed_response = json_decode($this->response, TRUE);
/* $this->parsed_response = $this->clean_text_nodes(json_decode($this->response, TRUE)); */
if ($this->parsed_response['stat'] == 'fail') {
if ($this->die_on_error) die("The Flickr API returned the following error: #{$this->parsed_response['code']} - {$this->parsed_response['message']}");
else {
Expand Down Expand Up @@ -1088,8 +1097,8 @@ function photos_search ($args = array()) {
*/

/* https://www.flickr.com/services/api/flickr.photos.search.html */
$this->request("flickr.photos.search", $args);
return $this->parsed_response ? $this->parsed_response['photos'] : false;
$result = $this->request("flickr.photos.search", $args);
return ($this->parsed_response) ? $this->parsed_response['photos'] : false;
}

function photos_setContentType ($photo_id, $content_type) {
Expand Down

0 comments on commit b5955ce

Please sign in to comment.