diff --git a/README.md b/README.md index 9741bd758..835852b40 100644 --- a/README.md +++ b/README.md @@ -122,8 +122,4 @@ $cache->decrementMulti(array( array("key2", 2), )); - - - - -```` \ No newline at end of file +``` diff --git a/phpfastcache/3.0.0/drivers/files.php b/phpfastcache/3.0.0/drivers/files.php index 08fbee824..84284a2eb 100644 --- a/phpfastcache/3.0.0/drivers/files.php +++ b/phpfastcache/3.0.0/drivers/files.php @@ -28,14 +28,14 @@ function __construct($config = array()) { } } - + private function encodeFilename($keyword) { - return trim(trim(preg_replace("/[^a-zA-Z0-9]+/","_",$keyword),"_")); + return trim(trim(preg_replace("/[^a-zA-Z0-9]+/","_",$keyword),"_")); // return rtrim(base64_encode($keyword), '='); } - + private function decodeFilename($filename) { - return $filename; + return $filename; // return base64_decode($filename); } @@ -44,8 +44,8 @@ private function decodeFilename($filename) { */ private function getFilePath($keyword, $skip = false) { $path = $this->getPath(); - - $filename = $this->encodeFilename($keyword); + + $filename = $this->encodeFilename($keyword); $folder = substr($filename,0,2); $path = rtrim($path,"/")."/".$folder; /* @@ -59,7 +59,7 @@ private function getFilePath($keyword, $skip = false) { } elseif(!is_writeable($path)) { if(!chmod($path,$this->__setChmodAuto())) { - throw new Exception("PLEASE CHMOD ".$this->getPath()." - 0777 OR ANY WRITABLE PERMISSION!",92); + throw new Exception("PLEASE CHMOD ".$this->getPath()." - 0777 OR ANY WRITABLE PERMISSION!",92); } } } @@ -68,7 +68,6 @@ private function getFilePath($keyword, $skip = false) { return $file_path; } - function driver_set($keyword, $value = "", $time = 300, $option = array() ) { $file_path = $this->getFilePath($keyword); // echo "
DEBUG SET: ".$keyword." - ".$value." - ".$time."
"; @@ -99,9 +98,6 @@ function driver_set($keyword, $value = "", $time = 300, $option = array() ) { } } - - - function driver_get($keyword, $option = array()) { $file_path = $this->getFilePath($keyword); @@ -162,19 +158,19 @@ function driver_stats($option = array()) { $object = $this->decode($this->readfile($file_path)); if($this->isExpired($object)) { @unlink($file_path); - $removed = $removed + $size; + $removed += $size; } - $total = $total + $size; + $total += $size; } } // end read subdir } // end if } // end while - $res['size'] = $total - $removed; + $res['size'] = $total - $removed; $res['info'] = array( - "Total" => $total, - "Removed" => $removed, - "Current" => $res['size'], + "Total [bytes]" => $total, + "Expired and removed [bytes]" => $removed, + "Current [bytes]" => $res['size'], ); return $res; } @@ -212,10 +208,8 @@ function driver_clean($option = array()) { } // end if } // end while - } - function driver_isExisting($keyword) { $file_path = $this->getFilePath($keyword,true); if(!@file_exists($file_path)) { @@ -232,7 +226,6 @@ function driver_isExisting($keyword) { } function isExpired($object) { - if(isset($object['expired_time']) && time() >= $object['expired_time']) { return true; } else { @@ -240,7 +233,4 @@ function isExpired($object) { } } - - - } diff --git a/phpfastcache/3.0.0/drivers/predis.php b/phpfastcache/3.0.0/drivers/predis.php index 491b963c5..e8369e90f 100644 --- a/phpfastcache/3.0.0/drivers/predis.php +++ b/phpfastcache/3.0.0/drivers/predis.php @@ -16,21 +16,22 @@ class phpfastcache_predis extends BasePhpFastCache implements phpfastcache_drive function checkdriver() { // Check memcache + if (! class_exists("\\Predis\\Client")) { $this->required_extension("predis-1.0/autoload.php"); try { Predis\Autoloader::register(); } catch(Exception $e) { } - return true; + } + return true; } function __construct($config = array()) { $this->setup($config); + if (! class_exists("\\Predis\\Client")) { $this->required_extension("predis-1.0/autoload.php"); - - - + } } function connectServer() { @@ -121,7 +122,7 @@ function driver_get($keyword, $option = array()) { function driver_delete($keyword, $option = array()) { if($this->connectServer()) { - $this->instant->delete($keyword); + $this->instant->del($keyword); } } @@ -164,4 +165,4 @@ function driver_isExisting($keyword) { -} \ No newline at end of file +} diff --git a/phpfastcache/3.0.0/drivers/sqlite.php b/phpfastcache/3.0.0/drivers/sqlite.php index a04b9a36d..a55d4eeac 100644 --- a/phpfastcache/3.0.0/drivers/sqlite.php +++ b/phpfastcache/3.0.0/drivers/sqlite.php @@ -8,6 +8,9 @@ class phpfastcache_sqlite extends BasePhpFastCache implements phpfastcache_driver { + const SQLITE_DIR = 'sqlite'; + const INDEXING_FILE = 'indexing'; + var $max_size = 10; // 10 mb var $instant = array(); @@ -22,7 +25,7 @@ class phpfastcache_sqlite extends BasePhpFastCache implements phpfastcache_drive function initDB(PDO $db) { $db->exec('drop table if exists "caching"'); $db->exec('CREATE TABLE "caching" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "keyword" VARCHAR UNIQUE, "object" BLOB, "exp" INTEGER)'); - $db->exec('CREATE UNIQUE INDEX "cleaup" ON "caching" ("keyword","exp")'); + $db->exec('CREATE UNIQUE INDEX "cleanup" ON "caching" ("keyword","exp")'); $db->exec('CREATE INDEX "exp" ON "caching" ("exp")'); $db->exec('CREATE UNIQUE INDEX "keyword" ON "caching" ("keyword")'); } @@ -35,7 +38,7 @@ function initIndexing(PDO $db) { // delete everything before reset indexing $dir = @opendir($this->path); while($file = @readdir($dir)) { - if($file != "." && $file!=".." && $file != "indexing" && $file!="dbfastcache") { + if($file!="." && $file!=".." && $file!=self::INDEXING_FILE && $file!="dbfastcache") { @unlink($this->path."/".$file); } } @@ -43,7 +46,7 @@ function initIndexing(PDO $db) { $db->exec('drop table if exists "balancing"'); $db->exec('CREATE TABLE "balancing" ("keyword" VARCHAR PRIMARY KEY NOT NULL UNIQUE, "db" INTEGER)'); $db->exec('CREATE INDEX "db" ON "balancing" ("db")'); - $db->exec('CREATE UNIQUE INDEX "lookup" ON "balacing" ("keyword")'); + $db->exec('CREATE UNIQUE INDEX "lookup" ON "balancing" ("keyword")'); } @@ -54,11 +57,11 @@ function initIndexing(PDO $db) { function indexing($keyword) { if($this->indexing == NULL) { $createTable = false; - if(!@file_exists($this->path."/indexing")) { + if(!@file_exists($this->path."/".self::INDEXING_FILE)) { $createTable = true; } - $PDO = new PDO("sqlite:".$this->path."/indexing"); + $PDO = new PDO("sqlite:".$this->path."/".self::INDEXING_FILE); $PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); @@ -115,8 +118,6 @@ function indexing($keyword) { return $db; } - - function db($keyword, $reset = false) { /* * Default is fastcache @@ -145,17 +146,14 @@ function db($keyword, $reset = false) { } - return $this->instant[$instant]; } - - function checkdriver() { if(extension_loaded('pdo_sqlite') && is_writeable($this->getPath())) { return true; } - $this->fallback = true; + $this->fallback = true; return false; } @@ -168,15 +166,15 @@ function __construct($config = array()) { */ $this->setup($config); if(!$this->checkdriver() && !isset($config['skipError'])) { - $this->fallback = true; + $this->fallback = true; } - if(!@file_exists($this->getPath()."/sqlite")) { - if(!@mkdir($this->getPath()."/sqlite",$this->__setChmodAuto())) { - $this->fallback = true; + if(!@file_exists($this->getPath()."/".self::SQLITE_DIR)) { + if(!@mkdir($this->getPath()."/".self::SQLITE_DIR,$this->__setChmodAuto())) { + $this->fallback = true; } } - $this->path = $this->getPath()."/sqlite"; + $this->path = $this->getPath()."/".self::SQLITE_DIR; } @@ -207,16 +205,16 @@ function driver_set($keyword, $value = "", $time = 300, $option = array() ) { return true; } catch(PDOException $e) { - try { - $stm = $this->db($keyword,true)->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)"); - $stm->execute(array( - ":keyword" => $keyword, - ":object" => $this->encode($value), - ":exp" => time() + (Int)$time, - )); - } catch (PDOException $e) { - return false; - } + try { + $stm = $this->db($keyword,true)->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)"); + $stm->execute(array( + ":keyword" => $keyword, + ":object" => $this->encode($value), + ":exp" => time() + (Int)$time, + )); + } catch (PDOException $e) { + return false; + } } @@ -238,32 +236,28 @@ function driver_get($keyword, $option = array()) { $row = $stm->fetch(PDO::FETCH_ASSOC); } catch(PDOException $e) { - try { - $stm = $this->db($keyword,true)->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); - $stm->execute(array( - ":keyword" => $keyword - )); - $row = $stm->fetch(PDO::FETCH_ASSOC); - } catch(PDOException $e) { - return null; - } + try { + $stm = $this->db($keyword,true)->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); + $stm->execute(array( + ":keyword" => $keyword + )); + $row = $stm->fetch(PDO::FETCH_ASSOC); + } catch(PDOException $e) { + return null; + } } - if($this->isExpired($row)) { $this->deleteRow($row); return null; } - - if(isset($row['id'])) { $data = $this->decode($row['object']); return $data; } - return null; } @@ -276,31 +270,34 @@ function isExpired($row) { } function deleteRow($row) { - try { - $stm = $this->db($row['keyword'])->prepare("DELETE FROM `caching` WHERE (`id`=:id) OR (`exp` <= :U) "); - $stm->execute(array( - ":id" => $row['id'], - ":U" => time(), - )); - } catch (PDOException $e) { - return false; - } + try { + $stm = $this->db($row['keyword'])->prepare("DELETE FROM `caching` WHERE (`id`=:id) OR (`exp` <= :U) "); + $stm->execute(array( + ":id" => $row['id'], + ":U" => time(), + )); + } catch (PDOException $e) { + return false; + } } function driver_delete($keyword, $option = array()) { - try { - $stm = $this->db($keyword)->prepare("DELETE FROM `caching` WHERE (`keyword`=:keyword) OR (`exp` <= :U)"); - $stm->execute(array( - ":keyword" => $keyword, - ":U" => time(), - )); - } catch (PDOException $e) { - return false; - } + try { + $stm = $this->db($keyword)->prepare("DELETE FROM `caching` WHERE (`keyword`=:keyword) OR (`exp` <= :U)"); + $stm->execute(array( + ":keyword" => $keyword, + ":U" => time(), + )); + } catch (PDOException $e) { + return false; + } } + /* + * Return total cache size + auto removed expired entries + */ function driver_stats($option = array()) { $res = array( "info" => "", @@ -315,46 +312,43 @@ function driver_stats($option = array()) { if($file!="." && $file!="..") { $file_path = $this->path."/".$file; $size = @filesize($file_path); - $total = $total + $size; - - try { - $PDO = new PDO("sqlite:".$file_path); - $PDO->setAttribute(PDO::ATTR_ERRMODE, - PDO::ERRMODE_EXCEPTION); - - $stm = $PDO->prepare("DELETE FROM `caching` WHERE `exp` <= :U"); - $stm->execute(array( - ":U" => time(), - )); - - $PDO->exec("VACUUM;"); - $size = @filesize($file_path); - $optimized = $optimized + $size; - } catch (PDOException $e) { - $size = 0; - $optimized = 0; - } - - - + $total += $size; + if ($file!=self::INDEXING_FILE) { + try { + $PDO = new PDO("sqlite:".$file_path); + $PDO->setAttribute(PDO::ATTR_ERRMODE, + PDO::ERRMODE_EXCEPTION); + + $stm = $PDO->prepare("DELETE FROM `caching` WHERE `exp` <= :U"); + $stm->execute(array( + ":U" => time(), + )); + + $PDO->exec("VACUUM;"); + $size = @filesize($file_path); + } catch (PDOException $e) { + $res['data'] .= sprintf("%s: %s\n", $file_path, $e->getMessage()); + } + } + $optimized += $size; } } - $res['size'] = round($optimized/1024/1024,1); + $res['size'] = $optimized; $res['info'] = array( - "total" => round($total/1024/1024,1), - "optimized" => round($optimized/1024/1024,1), + "total before removing expired entries [bytes]" => $total, + "optimized after removing expired entries [bytes]" => $optimized, ); return $res; } function driver_clean($option = array()) { - + // close connection $this->instant = array(); $this->indexing = NULL; - - // delete everything before reset indexing + + // delete everything $dir = @opendir($this->path); while($file = @readdir($dir)) { if($file != "." && $file!="..") { @@ -364,24 +358,19 @@ function driver_clean($option = array()) { } function driver_isExisting($keyword) { - try { - $stm = $this->db($keyword)->prepare("SELECT COUNT(`id`) as `total` FROM `caching` WHERE `keyword`=:keyword"); - $stm->execute(array( - ":keyword" => $keyword - )); - $data = $stm->fetch(PDO::FETCH_ASSOC); - if($data['total'] >= 1) { - return true; - } else { - return false; - } - } catch (PDOException $e) { - return false; - } - - - + try { + $stm = $this->db($keyword)->prepare("SELECT COUNT(`id`) as `total` FROM `caching` WHERE `keyword`=:keyword"); + $stm->execute(array( + ":keyword" => $keyword + )); + $data = $stm->fetch(PDO::FETCH_ASSOC); + if($data['total'] >= 1) { + return true; + } else { + return false; + } + } catch (PDOException $e) { + return false; + } } - - } diff --git a/phpfastcache/3.0.0/phpfastcache.php b/phpfastcache/3.0.0/phpfastcache.php index 7533e77cc..fc9c01da9 100644 --- a/phpfastcache/3.0.0/phpfastcache.php +++ b/phpfastcache/3.0.0/phpfastcache.php @@ -196,7 +196,7 @@ public static function getPath($skip_create_path = false, $config) { self::htaccessGen($full_path, $config['htaccess']); } - return $full_path; + return realpath($full_path); }