Skip to content

Commit

Permalink
fixed delete of expired items, formatting change
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeMTOL committed May 26, 2014
1 parent fe6f493 commit 177ddee
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions phpFlickr.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ function enableCache ($type, $connection, $cache_expire = 600, $table = 'flickr_
*/
mysqli_query($db, "
CREATE TABLE IF NOT EXISTS `$table` (
`request` varchar(128) NOT NULL,
`response` mediumtext NOT NULL,
`expiration` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY `request` (`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");
if($result) {
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, "DELETE FROM $table WHERE CURRENT_TIMESTAMP > expiration");
mysqli_query($db, 'OPTIMIZE TABLE ' . $this->cache_table);
}
$this->cache = 'db';
Expand Down Expand Up @@ -145,8 +145,6 @@ function getCached ($request)
$this->cache_key = $reqhash;
$this->cache_request = $request;
if ($this->cache == 'db') {
$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);
Expand Down Expand Up @@ -179,21 +177,17 @@ function cache ($request, $response)
}
$reqhash = md5(serialize($request));
if ($this->cache == 'db') {

//$this->cache_db->query("DELETE FROM $this->cache_table WHERE request = '$reqhash'");
$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) ";
$sql = 'INSERT INTO '.$this->cache_table.' (request, response, expiration)
VALUES (\''.$reqhash'\', \''.$response.'\', TIMESTAMPADD(SECOND,$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);
}
if(!$result) {
echo mysqli_error($this->cache_db);
}

return $result;
} elseif ($this->cache == "fs") {
Expand Down Expand Up @@ -233,7 +227,6 @@ 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 ) {
Expand Down

0 comments on commit 177ddee

Please sign in to comment.