Skip to content

Commit 4af9226

Browse files
committed
Merge remote-tracking branch 'github/final' into issue-177
# Conflicts: # phpfastcache/3.0.0/abstract.php # phpfastcache/3.0.0/drivers/files.php # phpfastcache/3.0.0/drivers/sqlite.php # phpfastcache/3.0.0/phpfastcache.php
2 parents 17c589f + 54e5a61 commit 4af9226

File tree

4 files changed

+87
-106
lines changed

4 files changed

+87
-106
lines changed

phpfastcache/3.0.0/abstract.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,12 @@ public function getInfoMulti($list = array())
274274
/**
275275
* @param array $list
276276
*/
277-
public function deleteMulti($list = array())
277+
public function deleteMulti($list = array(), $option = array())
278278
{
279-
foreach ($list as $array) {
280-
$this->delete($array[ 0 ],
281-
isset($array[ 1 ]) ? $array[ 1 ] : array());
279+
foreach ($list as $item) {
280+
if (is_array($item) && count($item) === 2) {
281+
$this->delete($item[ 0 ], $item[ 1 ]);
282+
}
282283
}
283284
}
284285

@@ -419,6 +420,7 @@ protected function required_extension($name)
419420
require_once(dirname(__FILE__) . "/../_extensions/" . $name);
420421
}
421422

423+
422424
/**
423425
* @param $file
424426
* @return string
@@ -427,7 +429,7 @@ protected function required_extension($name)
427429
protected function readfile($file)
428430
{
429431
if (function_exists("file_get_contents")) {
430-
return @file_get_contents($file);
432+
return file_get_contents($file);
431433
} else {
432434
$string = "";
433435

@@ -492,7 +494,7 @@ protected function htaccessGen($path = "")
492494
{
493495
if ($this->option("htaccess") == true) {
494496

495-
if (!@file_exists($path . "/.htaccess")) {
497+
if (!file_exists($path . "/.htaccess")) {
496498
// echo "write me";
497499
$html = "order deny, allow \r\n
498500
deny from all \r\n
@@ -510,7 +512,6 @@ protected function htaccessGen($path = "")
510512
// echo "got me";
511513
}*/
512514
}
513-
514515
}
515516

516517
/**
@@ -574,13 +575,14 @@ public function systemInfo()
574575
return $this->option;
575576
}
576577

578+
577579
/**
578-
* @param string $class
580+
* @param $class
579581
* @return bool
580582
*/
581583
protected function isExistingDriver($class)
582584
{
583-
if (@file_exists(dirname(__FILE__) . "/drivers/" . $class . ".php")) {
585+
if (file_exists(dirname(__FILE__) . "/drivers/" . $class . ".php")) {
584586
require_once(dirname(__FILE__) . "/drivers/" . $class . ".php");
585587
if (class_exists("phpfastcache_" . $class)) {
586588
return true;

phpfastcache/3.0.0/drivers/files.php

Lines changed: 30 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,11 @@ private function getFilePath($keyword, $skip = false)
7575
/*
7676
* Skip Create Sub Folders;
7777
*/
78-
if ($skip == false) {
79-
//if it doesn't exist, I can't create it, and nobody beat me to creating it:
80-
if (!@is_dir($path) && !@mkdir($path,
81-
$this->__setChmodAuto()) && !@is_dir($path)
82-
) {
83-
throw new Exception("PLEASE CHMOD " . $this->getPath() . " - 0777 OR ANY WRITABLE PERMISSION!",
84-
92);
85-
}
86-
//if it does exist (after someone beat me to it, perhaps), but isn't writable or fixable:
87-
if (@is_dir($path) && !is_writeable($path) && !@chmod($path,
88-
$this->__setChmodAuto())
89-
) {
90-
throw new Exception("PLEASE CHMOD " . $this->getPath() . " - 0777 OR ANY WRITABLE PERMISSION!",
91-
92);
78+
if($skip == false) {
79+
if(!file_exists($path)) {
80+
if(!mkdir($path,$this->__setChmodAuto())) {
81+
throw new Exception("PLEASE CHMOD ".$this->getPath()." - 0777 OR ANY WRITABLE PERMISSION!",92);
82+
}
9283
}
9384
}
9485

@@ -115,7 +106,7 @@ public function driver_set($keyword, $value = "", $time = 300, $option = array()
115106
/*
116107
* Skip if Existing Caching in Options
117108
*/
118-
if (isset($option[ 'skipExisting' ]) && $option[ 'skipExisting' ] == true && @file_exists($file_path)) {
109+
if(isset($option['skipExisting']) && $option['skipExisting'] == true && file_exists($file_path)) {
119110
$content = $this->readfile($file_path);
120111
$old = $this->decode($content);
121112
$toWrite = false;
@@ -130,21 +121,11 @@ public function driver_set($keyword, $value = "", $time = 300, $option = array()
130121
* because first-to-lock wins and the file will exist before the writer attempts
131122
* to write.
132123
*/
133-
if ($toWrite == true && !@file_exists($tmp_path) && !@file_exists($file_path)) {
134-
try {
135-
$f = @fopen($tmp_path, "c");
136-
if ($f) {
137-
if (flock($f, LOCK_EX | LOCK_NB)) {
138-
$written = ($written && fwrite($f, $data));
139-
$written = ($written && fflush($f));
140-
$written = ($written && flock($f, LOCK_UN));
141-
} else {
142-
//arguably the file is being written to so the job is done
143-
$written = false;
144-
}
145-
$written = ($written && @fclose($f));
146-
$written = ($written && @rename($tmp_path, $file_path));
147-
}
124+
if($toWrite == true && !file_exists($tmp_path) && !file_exists($file_path)) {
125+
try {
126+
$f = fopen($file_path, "w+");
127+
fwrite($f, $data);
128+
fclose($f);
148129
} catch (Exception $e) {
149130
// miss cache
150131
$written = false;
@@ -163,7 +144,7 @@ public function driver_get($keyword, $option = array())
163144
{
164145

165146
$file_path = $this->getFilePath($keyword);
166-
if (!@file_exists($file_path)) {
147+
if(!file_exists($file_path)) {
167148
return null;
168149
}
169150

@@ -194,10 +175,8 @@ public function driver_delete($keyword, $option = array())
194175
}
195176
}
196177

197-
/*
198-
* Return total cache size + auto removed expired files
199-
*/
200178
/**
179+
* Return total cache size + auto removed expired files
201180
* @param array $option
202181
* @return array
203182
* @throws \Exception
@@ -218,20 +197,18 @@ public function driver_stats($option = array())
218197

219198
$total = 0;
220199
$removed = 0;
221-
$content = array();
222-
while ($file = @readdir($dir)) {
223-
if ($file != "." && $file != ".." && is_dir($path . "/" . $file)) {
200+
while($file=readdir($dir)) {
201+
if($file!="." && $file!=".." && is_dir($path."/".$file)) {
224202
// read sub dir
225-
$subdir = @opendir($path . "/" . $file);
226-
if (!$subdir) {
227-
throw new Exception("Can't read path:" . $path . "/" . $file,
228-
93);
203+
$subdir = opendir($path."/".$file);
204+
if(!$subdir) {
205+
throw new Exception("Can't read path:".$path."/".$file,93);
229206
}
230207

231-
while ($f = @readdir($subdir)) {
232-
if ($f != "." && $f != "..") {
233-
$file_path = $path . "/" . $file . "/" . $f;
234-
$size = @filesize($file_path);
208+
while($f = readdir($subdir)) {
209+
if($f!="." && $f!="..") {
210+
$file_path = $path."/".$file."/".$f;
211+
$size = filesize($file_path);
235212
$object = $this->decode($this->readfile($file_path));
236213

237214
if (strpos($f, ".") === false) {
@@ -290,18 +267,18 @@ public function driver_clean($option = array())
290267
throw new Exception("Can't read PATH:" . $path, 94);
291268
}
292269

293-
while ($file = @readdir($dir)) {
294-
if ($file != "." && $file != ".." && is_dir($path . "/" . $file)) {
270+
while($file=readdir($dir)) {
271+
if($file!="." && $file!=".." && is_dir($path."/".$file)) {
295272
// read sub dir
296273
$subdir = @opendir($path . "/" . $file);
297274
if (!$subdir) {
298275
throw new Exception("Can't read path:" . $path . "/" . $file,
299276
93);
300277
}
301278

302-
while ($f = @readdir($subdir)) {
303-
if ($f != "." && $f != "..") {
304-
$file_path = $path . "/" . $file . "/" . $f;
279+
while($f = readdir($subdir)) {
280+
if($f!="." && $f!="..") {
281+
$file_path = $path."/".$file."/".$f;
305282
@unlink($file_path);
306283
}
307284
} // end read subdir
@@ -315,10 +292,9 @@ public function driver_clean($option = array())
315292
* @return bool
316293
* @throws \Exception
317294
*/
318-
public function driver_isExisting($keyword)
319-
{
320-
$file_path = $this->getFilePath($keyword, true);
321-
if (!@file_exists($file_path)) {
295+
public function driver_isExisting($keyword) {
296+
$file_path = $this->getFilePath($keyword,true);
297+
if(!file_exists($file_path)) {
322298
return false;
323299
} else {
324300
// check expired or not

phpfastcache/3.0.0/drivers/sqlite.php

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public function initIndexing(PDO $db)
8585
{
8686

8787
// delete everything before reset indexing
88-
$dir = @opendir($this->path);
89-
while ($file = @readdir($dir)) {
90-
if ($file != "." && $file != ".." && $file != self::INDEXING_FILE && $file != "dbfastcache") {
91-
@unlink($this->path . "/" . $file);
88+
$dir = opendir($this->path);
89+
while($file = readdir($dir)) {
90+
if($file != "." && $file!=".." && $file != "indexing" && $file!="dbfastcache") {
91+
unlink($this->path."/".$file);
9292
}
9393
}
9494

@@ -109,7 +109,7 @@ public function indexing($keyword)
109109
{
110110
if ($this->indexing == null) {
111111
$createTable = false;
112-
if (!@file_exists($this->path . "/" . self::INDEXING_FILE)) {
112+
if(!file_exists($this->path."/indexing")) {
113113
$createTable = true;
114114
}
115115

@@ -136,8 +136,8 @@ public function indexing($keyword)
136136

137137
// check file size
138138

139-
$size = @file_exists($this->path . "/db" . $db) ? @filesize($this->path . "/db" . $db) : 1;
140-
$size = round($size / 1024 / 1024, 1);
139+
$size = file_exists($this->path."/db".$db) ? filesize($this->path."/db".$db) : 1;
140+
$size = round($size / 1024 / 1024,1);
141141

142142

143143
if ($size > $this->max_size) {
@@ -188,7 +188,7 @@ public function db($keyword, $reset = false)
188188
if (!isset($this->instant[ $instant ])) {
189189
// check DB Files ready or not
190190
$createTable = false;
191-
if (!@file_exists($this->path . "/db" . $instant) || $reset == true) {
191+
if(!file_exists($this->path."/db".$instant) || $reset == true) {
192192
$createTable = true;
193193
}
194194
$PDO = new PDO("sqlite:" . $this->path . "/db" . $instant);
@@ -233,7 +233,7 @@ public function driver_set(
233233
$time = 300,
234234
$option = array()
235235
) {
236-
$skipExisting = isset($option[ 'skipExisting' ]) ? $option[ 'skipExisting' ] : false;
236+
$skipExisting = isset($option['skipExisting']) ? $option['skipExisting'] : false;
237237
$toWrite = true;
238238

239239
// check in cache first
@@ -392,31 +392,33 @@ public function driver_stats($option = array())
392392
$total = 0;
393393
$optimized = 0;
394394

395-
$dir = @opendir($this->path);
396-
while ($file = @readdir($dir)) {
397-
if ($file != "." && $file != "..") {
398-
$file_path = $this->path . "/" . $file;
399-
$size = @filesize($file_path);
400-
$total += $size;
401-
if ($file != self::INDEXING_FILE) {
402-
try {
403-
$PDO = new PDO("sqlite:" . $file_path);
404-
$PDO->setAttribute(PDO::ATTR_ERRMODE,
405-
PDO::ERRMODE_EXCEPTION);
406-
407-
$stm = $PDO->prepare("DELETE FROM `caching` WHERE `exp` <= :U");
408-
$stm->execute(array(
409-
":U" => time(),
410-
));
411-
412-
$PDO->exec("VACUUM;");
413-
$size = @filesize($file_path);
414-
} catch (PDOException $e) {
415-
$res[ 'data' ] .= sprintf("%s: %s\n", $file_path,
416-
$e->getMessage());
417-
}
418-
}
419-
$optimized += $size;
395+
$dir = opendir($this->path);
396+
while($file = readdir($dir)) {
397+
if($file!="." && $file!="..") {
398+
$file_path = $this->path."/".$file;
399+
$size = filesize($file_path);
400+
$total = $total + $size;
401+
402+
try {
403+
$PDO = new PDO("sqlite:".$file_path);
404+
$PDO->setAttribute(PDO::ATTR_ERRMODE,
405+
PDO::ERRMODE_EXCEPTION);
406+
407+
$stm = $PDO->prepare("DELETE FROM `caching` WHERE `exp` <= :U");
408+
$stm->execute(array(
409+
":U" => date("U"),
410+
));
411+
412+
$PDO->exec("VACUUM;");
413+
$size = filesize($file_path);
414+
$optimized = $optimized + $size;
415+
} catch (PDOException $e) {
416+
$size = 0;
417+
$optimized = 0;
418+
}
419+
420+
421+
420422
}
421423
}
422424
$res[ 'size' ] = $optimized;
@@ -436,13 +438,13 @@ public function driver_clean($option = array())
436438

437439
// close connection
438440
$this->instant = array();
439-
$this->indexing = null;
440-
441-
// delete everything
442-
$dir = @opendir($this->path);
443-
while ($file = @readdir($dir)) {
444-
if ($file != "." && $file != "..") {
445-
@unlink($this->path . "/" . $file);
441+
$this->indexing = NULL;
442+
443+
// delete everything before reset indexing
444+
$dir = opendir($this->path);
445+
while($file = readdir($dir)) {
446+
if($file != "." && $file!="..") {
447+
unlink($this->path."/".$file);
446448
}
447449
}
448450
}

phpfastcache/3.0.0/phpfastcache.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class phpFastCache
128128
* @param string $storage
129129
* @param array $config
130130
*/
131-
function __construct($storage = "", $config = array())
131+
public function __construct($storage = "", $config = array())
132132
{
133133
if (empty($config)) {
134134
$config = phpFastCache::$config;
@@ -333,7 +333,8 @@ protected static function htaccessGen($path, $create = true)
333333
92);
334334
}
335335
}
336-
if (!@file_exists($path . "/.htaccess")) {
336+
337+
if(!file_exists($path."/.htaccess")) {
337338
// echo "write me";
338339
$html = "order deny, allow \r\n
339340
deny from all \r\n

0 commit comments

Comments
 (0)