Permalink
Browse files

Fixed DB caching, moved to JSON communication

DB caching wasn’t working, changed the SQL commands to utilize ON
DUPLICATE statement.
Moved from php_serialization to json for API, meter for moving forward.
  • Loading branch information...
mikeMTOL committed May 25, 2014
1 parent 6a6a805 commit fe6f493de1aabe0a34d50fa7bab489494716942f
Showing with 40 additions and 24 deletions.
  1. +40 −24 phpFlickr.php
View
@@ -91,16 +91,19 @@ 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 ) {
if($result) {
$result = mysqli_fetch_row($result);
}
if ( $result && $result[0] > $this->max_cache_rows ) {
mysqli_query($db, "DELETE FROM $table WHERE expiration < DATE_SUB(NOW(), INTERVAL $cache_expire second)");
mysqli_query($db, 'OPTIMIZE TABLE ' . $this->cache_table);
}
@@ -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];
@@ -141,10 +145,12 @@ 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) ) {
$table = $this->cache_table;
$sql = "SELECT response FROM $table WHERE request = '$reqhash' AND ";
$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;
}
@@ -173,16 +179,23 @@ 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);
}
$table = $this->cache_table;
$response = urlencode($response);
$expire = $this->cache_expire;
$sql = "INSERT INTO $table (request, response, expiration)
VALUES ('$reqhash', '$response', TIMESTAMPADD(SECOND,$expire,CURRENT_TIMESTAMP))
ON DUPLICATE KEY UPDATE response='$response',
expiration=TIMESTAMPADD(SECOND,$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");
@@ -220,6 +233,7 @@ function post ($data, $type = null) {
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
} else {
// Use sockets.
foreach ( $data as $key => $value ) {
@@ -270,7 +284,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'])) {
@@ -279,7 +293,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]);
@@ -295,13 +310,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)); */

This comment has been minimized.

Show comment
Hide comment
@barryvdh

barryvdh Jul 18, 2014

Why did you change the default here, to not clean the text?

@barryvdh

barryvdh Jul 18, 2014

Why did you change the default here, to not clean the text?

This comment has been minimized.

Show comment
Hide comment
@igneosaur

igneosaur Oct 20, 2014

This will break any apps that update their phpFlickr submodule if they have already been built using versions of phpFlickr before this commit.

@igneosaur

igneosaur Oct 20, 2014

This will break any apps that update their phpFlickr submodule if they have already been built using versions of phpFlickr before this commit.

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 {
@@ -1088,8 +1104,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) {

1 comment on commit fe6f493

@mikeMTOL

This comment has been minimized.

Show comment
Hide comment
@mikeMTOL

mikeMTOL Oct 20, 2014

Contributor

If I may add my $.02. If you're making a pull request can you add a switch/setting to toggle between old way and the json way.

Default the setting to the old way as to not break existing users but keep the json (which is a more popular way these days) for future compatibility.

Let me know if you require any help.

Contributor

mikeMTOL commented on fe6f493 Oct 20, 2014

If I may add my $.02. If you're making a pull request can you add a switch/setting to toggle between old way and the json way.

Default the setting to the old way as to not break existing users but keep the json (which is a more popular way these days) for future compatibility.

Let me know if you require any help.

Please sign in to comment.