Skip to content

Commit 012dddc

Browse files
committed
Improved driver statistics + FilePath issues fixes
1 parent 0b505ad commit 012dddc

File tree

15 files changed

+221
-149
lines changed

15 files changed

+221
-149
lines changed

src/phpFastCache/Cache/DriverBaseTrait.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,7 @@ protected function encode($data)
120120
*/
121121
protected function decode($value)
122122
{
123-
$x = @unserialize($value);
124-
if ($x == false) {
125-
return $value;
126-
} else {
127-
return $x;
128-
}
123+
return @unserialize($value);
129124
}
130125

131126
/**

src/phpFastCache/CacheManager.php

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,31 @@
3939
class CacheManager
4040
{
4141
/**
42-
* @var bool
42+
* @var int
4343
*/
44-
public static $disabled = false;
44+
public static $ReadHits = 0;
45+
46+
/**
47+
* @var int
48+
*/
49+
public static $WriteHits = 0;
4550

4651
/**
4752
* @var array
4853
*/
4954
public static $config = [
50-
'default_chmod' => 0777, // 0777 , 0666, 0644
51-
52-
'overwrite' => "", // files, sqlite, etc it will overwrite ur storage and all other caching for waiting u fix ur server
53-
'allow_search' => false, // turn to true will allow $method search("/regex/")
54-
55+
'default_chmod' => 0777, // 0777 recommended
5556
'fallback' => 'files', //Fall back when old driver is not support
56-
5757
'securityKey' => 'auto',
5858
'htaccess' => true,
59-
'path' => '',
60-
61-
62-
'extensions' => [],
63-
"cache_method" => 2, // 1 = normal, 2 = phpfastcache, 3 = memory
64-
"limited_memory_each_object" => 4000, // maximum size (bytes) of object store in memory
59+
'path' => '',// if not set will be the value of sys_get_temp_dir()
60+
"limited_memory_each_object" => 4096, // maximum size (bytes) of object store in memory
6561
"compress_data" => false, // compress stored data, if the backend supports it
6662
];
6763

6864
/**
6965
* @var array
7066
*/
71-
protected static $tmp = [];
7267

7368
protected static $namespacePath;
7469
protected static $instances = [];
@@ -91,8 +86,8 @@ public static function getInstance($driver = 'auto', $config = [])
9186
$class = self::getNamespacePath() . $driver . '\Driver';
9287
self::$instances[ $instance ] = new $class($config);
9388
} else {
94-
trigger_error('Calling CacheManager::getInstance for already instanced drivers is a bad practice and have a significant impact on performances.
95-
See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F');
89+
// trigger_error('[' . $driver . '] Calling CacheManager::getInstance for already instanced drivers is a bad practice and have a significant impact on performances.
90+
// See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F');
9691
}
9792

9893
return self::$instances[ $instance ];
@@ -106,26 +101,9 @@ public static function getInstance($driver = 'auto', $config = [])
106101
public static function getAutoClass($config)
107102
{
108103
static $autoDriver;
109-
$systemDrivers = [
110-
'Sqlite',
111-
'Files',
112-
'Apc',
113-
'Apcu',
114-
'Memcache',
115-
'Memcached',
116-
'Couchbase',
117-
'Mongodb',
118-
'Predis',
119-
'Redis',
120-
'Ssdb',
121-
'Leveldb',
122-
'Wincache',
123-
'Xcache',
124-
'Devnull',
125-
];
126104

127105
if ($autoDriver === null) {
128-
foreach ($systemDrivers as $driver) {
106+
foreach (self::getStaticSystemDrivers() as $driver) {
129107
try {
130108
self::getInstance($driver, $config);
131109
$autoDriver = $driver;
@@ -190,4 +168,40 @@ public static function setup($name, $value = '')
190168
self::$config[ $name ] = $value;
191169
}
192170
}
171+
172+
/**
173+
* @return array
174+
*/
175+
public static function getStaticSystemDrivers()
176+
{
177+
return [
178+
'Sqlite',
179+
'Files',
180+
'Apc',
181+
'Apcu',
182+
'Memcache',
183+
'Memcached',
184+
'Couchbase',
185+
'Mongodb',
186+
'Predis',
187+
'Redis',
188+
'Ssdb',
189+
'Leveldb',
190+
'Wincache',
191+
'Xcache',
192+
'Devnull',
193+
];
194+
}
195+
196+
/**
197+
* @return array
198+
*/
199+
public static function getStaticAllDrivers()
200+
{
201+
return array_merge(self::getStaticSystemDrivers(), [
202+
'Devtrue',
203+
'Devfalse',
204+
'Cookie',
205+
]);
206+
}
193207
}

src/phpFastCache/Core/PathSeekerTrait.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ trait PathSeekerTrait
3030
* @return string
3131
* @throws \Exception
3232
*/
33-
public function getPath()
33+
public function getPath($getBasePath = false)
3434
{
3535
$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
3636

@@ -50,6 +50,11 @@ public function getPath()
5050
$path = $this->config[ 'path' ];
5151
}
5252

53+
if($getBasePath === true)
54+
{
55+
return $path;
56+
}
57+
5358
$securityKey = array_key_exists('securityKey', $this->config) ? $this->config[ 'securityKey' ] : '';
5459
if ($securityKey == "" || $securityKey == 'auto') {
5560
$securityKey = $this->config[ 'securityKey' ];
@@ -117,6 +122,16 @@ public function isExpired()
117122
return true;
118123
}
119124

125+
126+
/**
127+
* @return string
128+
* @throws \phpFastCache\Exceptions\phpFastCacheCoreException
129+
*/
130+
public function getFileDir()
131+
{
132+
return $this->getPath() . DIRECTORY_SEPARATOR . self::FILE_DIR;
133+
}
134+
120135
/**
121136
* @param $keyword
122137
* @param bool $skip
@@ -125,7 +140,7 @@ public function isExpired()
125140
*/
126141
private function getFilePath($keyword, $skip = false)
127142
{
128-
$path = $this->getFilesDir();
143+
$path = $this->getFileDir();
129144

130145
if($keyword === false)
131146
{

src/phpFastCache/Core/StandardPsr6StructureTrait.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace phpFastCache\Core;
1616

1717
use phpFastCache\Cache\ExtendedCacheItemInterface;
18+
use phpFastCache\CacheManager;
1819
use Psr\Cache\CacheItemInterface;
1920

2021
/**
@@ -45,13 +46,15 @@ public function getItem($key)
4546
if (is_string($key)) {
4647
if (!array_key_exists($key, $this->itemInstances)) {
4748
//(new \ReflectionObject($this))->getNamespaceName()
48-
49+
4950
/**
5051
* @var $item ExtendedCacheItemInterface
5152
*/
53+
CacheManager::$ReadHits++;
5254
$class = new \ReflectionClass((new \ReflectionObject($this))->getNamespaceName() . '\Item');
5355
$item = $class->newInstanceArgs([$this, $key]);
5456
$driverArray = $this->driverRead($key);
57+
//var_dump($driverArray);exit;
5558
if ($driverArray) {
5659
$item->set($this->driverUnwrapData($driverArray));
5760
$item->expiresAt($this->driverUnwrapTime($driverArray));
@@ -112,6 +115,7 @@ public function getItems(array $keys = [])
112115
*/
113116
public function hasItem($key)
114117
{
118+
CacheManager::$ReadHits++;
115119
return $this->getItem($key)->isHit();
116120
}
117121

@@ -121,6 +125,7 @@ public function hasItem($key)
121125
public function clear()
122126
{
123127
if ($this->driverClear()) {
128+
CacheManager::$WriteHits++;
124129
$this->itemInstances = [];
125130

126131
return true;
@@ -137,6 +142,7 @@ public function clear()
137142
public function deleteItem($key)
138143
{
139144
if ($this->hasItem($key) && $this->driverDelete($this->getItem($key))) {
145+
CacheManager::$WriteHits++;
140146
unset($this->itemInstances[ $key ]);
141147

142148
return true;
@@ -173,8 +179,13 @@ public function save(CacheItemInterface $item)
173179
if (!array_key_exists($item->getKey(), $this->itemInstances)) {
174180
$this->itemInstances[ $item->getKey() ] = $item;
175181
}
182+
if($this->driverWrite($item) && $this->driverWriteTags($item))
183+
{
184+
CacheManager::$WriteHits++;
185+
return true;
186+
}
176187

177-
return $this->driverWrite($item) && $this->driverWriteTags($item);
188+
return false;
178189
}
179190

180191
/**

src/phpFastCache/Drivers/Apc/Driver.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,13 @@ public function driverIsHit(CacheItemInterface $item)
150150
public function getStats()
151151
{
152152
$stat = new driverStatistic();
153-
$stat->setInfo(apc_cache_info('user'));
154153

155-
return $stat;
154+
$stats = (array) apc_cache_info('user');
155+
$date = (new \DateTime())->setTimestamp($stats['start_time']);
156+
return (new driverStatistic())
157+
->setData(implode(', ', array_keys($this->itemInstances)))
158+
->setInfo(sprintf("The APC cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.", $date->format(DATE_RFC2822), $stats['num_entries']))
159+
->setRawData($stats)
160+
->setSize($stats['mem_size']);
156161
}
157162
}

src/phpFastCache/Drivers/Couchbase/Driver.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function driverDelete(CacheItemInterface $item)
124124
*/
125125
public function driverClear()
126126
{
127-
return $this->getBucket()->flush();
127+
return $this->getBucket()->manager()->flush();
128128
}
129129

130130
/**
@@ -150,7 +150,6 @@ public function driverConnect()
150150

151151
$this->instance = $this->instance ?: new CouchbaseClient("couchbase://{$host}", $username, $password);
152152

153-
154153
foreach ($buckets as $bucket) {
155154
$this->bucketCurrent = $this->bucketCurrent ?: $bucket[ 'bucket' ];
156155
$this->setBucket($bucket[ 'bucket' ], $this->instance->openBucket($bucket[ 'bucket' ], $bucket[ 'password' ]));
@@ -208,6 +207,11 @@ public function driverIsHit(CacheItemInterface $item)
208207
*/
209208
public function getStats()
210209
{
211-
return (new driverStatistic())->setInfo(implode('<br />', (array) $this->instance->info()));
210+
$info = $this->getBucket()->manager()->info();
211+
return (new driverStatistic())
212+
->setSize($info['basicStats']['diskUsed'])
213+
->setRawData($info)
214+
->setData(implode(', ', array_keys($this->itemInstances)))
215+
->setInfo('CouchBase version ' . $info[ 'nodes' ][0]['version'] . ', Uptime (in days): ' . round($info[ 'nodes' ][0][ 'uptime' ] / 86400, 1). "\n For more information see RawData.");
212216
}
213217
}

src/phpFastCache/Drivers/Files/Driver.php

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Driver extends DriverAbstract
3434
/**
3535
*
3636
*/
37-
const FILES_DIR = 'files';
37+
const FILE_DIR = 'files';
3838

3939
/**
4040
* Driver constructor.
@@ -55,7 +55,7 @@ public function __construct(array $config = [])
5555
*/
5656
public function driverCheck()
5757
{
58-
return is_writable($this->getFilesDir()) || @mkdir($this->getFilesDir(), $this->setChmodAuto(), true);
58+
return is_writable($this->getFileDir()) || @mkdir($this->getFileDir(), $this->setChmodAuto(), true);
5959
}
6060

6161
/**
@@ -157,34 +157,7 @@ public function driverDelete(CacheItemInterface $item)
157157
*/
158158
public function driverClear()
159159
{
160-
$return = null;
161-
$path = $this->getFilePath(false);
162-
$dir = @opendir($path);
163-
if (!$dir) {
164-
throw new phpFastCacheDriverException("Can't read PATH:" . $path);
165-
}
166-
167-
while ($file = readdir($dir)) {
168-
if ($file != '.' && $file != '..' && is_dir($path . '/' . $file)) {
169-
// read sub dir
170-
$subdir = @opendir($path . '/' . $file);
171-
if (!$subdir) {
172-
throw new phpFastCacheDriverException("Can't read path:" . $path . '/' . $file);
173-
}
174-
175-
while ($subdirFile = readdir($subdir)) {
176-
if ($subdirFile != '.' && $subdirFile != '..') {
177-
$file_path = $path . '/' . $file . '/' . $subdirFile;
178-
$result = @unlink($file_path);
179-
if ($return !== false) {
180-
$return = $result;
181-
}
182-
}
183-
}
184-
}
185-
}
186-
187-
return (bool) $return;
160+
return (bool) Directory::rrmdir($this->getPath(true));
188161
}
189162

190163
/**
@@ -244,15 +217,6 @@ public static function isValidOption($optionName, $optionValue)
244217
}
245218
}
246219

247-
/**
248-
* @return string
249-
* @throws \phpFastCache\Exceptions\phpFastCacheCoreException
250-
*/
251-
public function getFilesDir()
252-
{
253-
return $this->getPath() . DIRECTORY_SEPARATOR . self::FILES_DIR;
254-
}
255-
256220
/**
257221
* @return array
258222
*/
@@ -288,9 +252,9 @@ public function getStats()
288252
if (!is_dir($path)) {
289253
throw new phpFastCacheDriverException("Can't read PATH:" . $path, 94);
290254
}
291-
255+
292256
$stat->setData(implode(', ', array_keys($this->itemInstances)))
293-
->setRawData($this->itemInstances)
257+
->setRawData([])
294258
->setSize(Directory::dirSize($path))
295259
->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($path));
296260

0 commit comments

Comments
 (0)