Skip to content

Commit b5955ce

Browse files
committed
Merge pull request #9 from mikeMTOL/master
Fixed DB caching, moved to JSON communication
2 parents 6a6a805 + 2820d69 commit b5955ce

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

phpFlickr.php

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,20 @@ function enableCache ($type, $connection, $cache_expire = 600, $table = 'flickr_
9191
*/
9292
mysqli_query($db, "
9393
CREATE TABLE IF NOT EXISTS `$table` (
94-
`request` CHAR( 35 ) NOT NULL ,
95-
`response` MEDIUMTEXT NOT NULL ,
96-
`expiration` DATETIME NOT NULL ,
97-
INDEX ( `request` )
94+
`request` varchar(128) NOT NULL,
95+
`response` mediumtext NOT NULL,
96+
`expiration` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
97+
UNIQUE KEY `request` (`request`)
9898
)
9999
");
100100

101-
$result = mysqli_query($db, "SELECT COUNT(*) FROM $table");
102-
$result = mysqli_fetch_row($result);
103-
if ( $result[0] > $this->max_cache_rows ) {
104-
mysqli_query($db, "DELETE FROM $table WHERE expiration < DATE_SUB(NOW(), INTERVAL $cache_expire second)");
101+
$result = mysqli_query($db, "SELECT COUNT(*) 'count' FROM $table");
102+
if( $result ) {
103+
$result = mysqli_fetch_assoc($result);
104+
}
105+
106+
if ( $result && $result['count'] > $this->max_cache_rows ) {
107+
mysqli_query($db, "DELETE FROM $table WHERE CURRENT_TIMESTAMP > expiration");
105108
mysqli_query($db, 'OPTIMIZE TABLE ' . $this->cache_table);
106109
}
107110
$this->cache = 'db';
@@ -132,6 +135,7 @@ function getCached ($request)
132135
//Checks the database or filesystem for a cached result to the request.
133136
//If there is no cache result, it returns a value of false. If it finds one,
134137
//it returns the unparsed XML.
138+
unset($request['api_sig']);
135139
foreach ( $request as $key => $value ) {
136140
if ( empty($value) ) unset($request[$key]);
137141
else $request[$key] = (string) $request[$key];
@@ -141,10 +145,10 @@ function getCached ($request)
141145
$this->cache_key = $reqhash;
142146
$this->cache_request = $request;
143147
if ($this->cache == 'db') {
144-
$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");
145-
if ( mysqli_num_rows($result) ) {
148+
$result = mysqli_query($this->cache_db, "SELECT response FROM " . $this->cache_table . " WHERE request = '" . $reqhash . "' AND CURRENT_TIMESTAMP < expiration");
149+
if ( $result && mysqli_num_rows($result) ) {
146150
$result = mysqli_fetch_assoc($result);
147-
return $result['response'];
151+
return urldecode($result['response']);
148152
} else {
149153
return false;
150154
}
@@ -174,15 +178,18 @@ function cache ($request, $response)
174178
$reqhash = md5(serialize($request));
175179
if ($this->cache == 'db') {
176180
//$this->cache_db->query("DELETE FROM $this->cache_table WHERE request = '$reqhash'");
177-
$result = mysqli_query($this->cache_db, "SELECT COUNT(*) FROM " . $this->cache_table . " WHERE request = '" . $reqhash . "'");
178-
$result = mysqli_fetch_row($result);
179-
if ( $result[0] ) {
180-
$sql = "UPDATE " . $this->cache_table . " SET response = '" . str_replace("'", "''", $response) . "', expiration = '" . strftime("%Y-%m-%d %H:%M:%S") . "' WHERE request = '" . $reqhash . "'";
181-
mysqli_query($this->cache_db, $sql);
182-
} else {
183-
$sql = "INSERT INTO " . $this->cache_table . " (request, response, expiration) VALUES ('$reqhash', '" . str_replace("'", "''", $response) . "', '" . strftime("%Y-%m-%d %H:%M:%S") . "')";
184-
mysqli_query($this->cache_db, $sql);
181+
$response = urlencode($response);
182+
$sql = 'INSERT INTO '.$this->cache_table.' (request, response, expiration)
183+
VALUES (\''.$reqhash.'\', \''.$response.'\', TIMESTAMPADD(SECOND,'.$this->cache_expire.',CURRENT_TIMESTAMP))
184+
ON DUPLICATE KEY UPDATE response=\''.$response.'\',
185+
expiration=TIMESTAMPADD(SECOND,'.$this->cache_expire.',CURRENT_TIMESTAMP) ';
186+
187+
$result = mysqli_query($this->cache_db, $sql);
188+
if(!$result) {
189+
echo mysqli_error($this->cache_db);
185190
}
191+
192+
return $result;
186193
} elseif ($this->cache == "fs") {
187194
$file = $this->cache_dir . "/" . $reqhash . ".cache";
188195
$fstream = fopen($file, "w");
@@ -270,7 +277,7 @@ function request ($command, $args = array(), $nocache = false)
270277
}
271278

272279
//Process arguments, including method and login data.
273-
$args = array_merge(array("method" => $command, "format" => "php_serial", "api_key" => $this->api_key), $args);
280+
$args = array_merge(array("method" => $command, "format" => "json", "nojsoncallback" => "1", "api_key" => $this->api_key), $args);
274281
if (!empty($this->token)) {
275282
$args = array_merge($args, array("auth_token" => $this->token));
276283
} elseif (!empty($_SESSION['phpFlickr_auth_token'])) {
@@ -279,7 +286,8 @@ function request ($command, $args = array(), $nocache = false)
279286
ksort($args);
280287
$auth_sig = "";
281288
$this->last_request = $args;
282-
if (!($this->response = $this->getCached($args)) || $nocache) {
289+
$this->response = $this->getCached($args);
290+
if (!($this->response) || $nocache) {
283291
foreach ($args as $key => $data) {
284292
if ( is_null($data) ) {
285293
unset($args[$key]);
@@ -295,13 +303,14 @@ function request ($command, $args = array(), $nocache = false)
295303
$this->cache($args, $this->response);
296304
}
297305

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

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

10951104
function photos_setContentType ($photo_id, $content_type) {

0 commit comments

Comments
 (0)