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
Expand Up @@ -91,17 +91,20 @@ function enableCache ($type, $connection, $cache_expire = 600, $table = 'flickr_
*/ */
mysqli_query($db, " mysqli_query($db, "
CREATE TABLE IF NOT EXISTS `$table` ( CREATE TABLE IF NOT EXISTS `$table` (
`request` CHAR( 35 ) NOT NULL , `request` varchar(128) NOT NULL,
`response` MEDIUMTEXT NOT NULL , `response` mediumtext NOT NULL,
`expiration` DATETIME NOT NULL , `expiration` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX ( `request` ) UNIQUE KEY `request` (`request`)
) )
"); ");


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

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

return $result;
} elseif ($this->cache == "fs") { } elseif ($this->cache == "fs") {
$file = $this->cache_dir . "/" . $reqhash . ".cache"; $file = $this->cache_dir . "/" . $reqhash . ".cache";
$fstream = fopen($file, "w"); $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. //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)) { if (!empty($this->token)) {
$args = array_merge($args, array("auth_token" => $this->token)); $args = array_merge($args, array("auth_token" => $this->token));
} elseif (!empty($_SESSION['phpFlickr_auth_token'])) { } elseif (!empty($_SESSION['phpFlickr_auth_token'])) {
Expand All @@ -279,7 +286,8 @@ function request ($command, $args = array(), $nocache = false)
ksort($args); ksort($args);
$auth_sig = ""; $auth_sig = "";
$this->last_request = $args; $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) { foreach ($args as $key => $data) {
if ( is_null($data) ) { if ( is_null($data) ) {
unset($args[$key]); unset($args[$key]);
Expand All @@ -295,13 +303,14 @@ function request ($command, $args = array(), $nocache = false)
$this->cache($args, $this->response); $this->cache($args, $this->response);
} }



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


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


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

0 comments on commit b5955ce

Please sign in to comment.