Skip to content

Commit c89db45

Browse files
committed
Re-implemented driver fallback
1 parent 2834f46 commit c89db45

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

V5-ROADMAP.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
- [x] Implement Dev Driver (return null/bool everywhere for development purpose)
1919
- [x] Implement advanced tags features: incrementByTag(s), decrementByTag(s), appendByTag(s), prependByTag(s), expiresAfterByTag(s)
2020
- [x] Final code review + psr2 checks + psr-6 null value as legitimates value.
21+
- [x] Testing fallback option
22+
- [x] Rewrite Wiki
2123
- [ ] Check Wincache driver in real Windows env (and not in VM)
22-
- [ ] Testing fallback option
23-
- [ ] Rewrite Wiki

src/phpFastCache/CacheManager.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CacheManager
5353
*/
5454
protected static $config = [
5555
'default_chmod' => 0777, // 0777 recommended
56-
'fallback' => 'files', //Fall back when old driver is not support
56+
'fallback' => false, //Fall back when old driver is not support
5757
'securityKey' => 'auto',// The securityKey that will be used to create sub-directory
5858
'htaccess' => true,// Auto-generate .htaccess if tit is missing
5959
'path' => '',// if not set will be the value of sys_get_temp_dir()
@@ -80,7 +80,10 @@ public static function getInstance($driver = 'auto', $config = [])
8080
{
8181
static $badPracticeOmeter = [];
8282

83-
$driver = ucfirst(strtolower($driver));
83+
/**
84+
* @todo: Standardize a method for driver name
85+
*/
86+
$driver = self::standardizeDriverName($driver);
8487
$config = array_merge(self::$config, $config);
8588
if (!$driver || $driver === 'Auto') {
8689
$driver = self::getAutoClass($config);
@@ -90,7 +93,18 @@ public static function getInstance($driver = 'auto', $config = [])
9093
if (!isset(self::$instances[ $instance ])) {
9194
$badPracticeOmeter[$driver] = 1;
9295
$class = self::getNamespacePath() . $driver . '\Driver';
93-
self::$instances[ $instance ] = new $class($config);
96+
try{
97+
self::$instances[ $instance ] = new $class($config);
98+
}catch(phpFastCacheDriverCheckException $e){
99+
$fallback = self::standardizeDriverName($config['fallback']);
100+
if($fallback && $fallback !== $driver){
101+
$class = self::getNamespacePath() . $fallback . '\Driver';
102+
self::$instances[ $instance ] = new $class($config);
103+
trigger_error(sprintf('The "%s" driver is unavailable at the moment, the fallback driver "%s" has been used instead.', $driver, $fallback), E_USER_WARNING);
104+
}else{
105+
throw new phpFastCacheDriverCheckException($e->getMessage(), $e->getCode(), $e);
106+
}
107+
}
94108
} else if(++$badPracticeOmeter[$driver] >= 5){
95109
trigger_error('[' . $driver . '] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances.
96110
See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F');
@@ -192,7 +206,7 @@ public static function setDefaultConfig($name, $value = null)
192206
/**
193207
* @return array
194208
*/
195-
public function getDefaultConfig()
209+
public static function getDefaultConfig()
196210
{
197211
return self::$config;
198212
}
@@ -232,4 +246,13 @@ public static function getStaticAllDrivers()
232246
'Cookie',
233247
]);
234248
}
249+
250+
/**
251+
* @param string $driverName
252+
* @return string
253+
*/
254+
public static function standardizeDriverName($driverName)
255+
{
256+
return ucfirst(strtolower(trim($driverName)));
257+
}
235258
}

0 commit comments

Comments
 (0)