Skip to content

Commit ead45bc

Browse files
committed
- Added configurable instance ID as requested by @HackHers in #477
1 parent 885fc0f commit ead45bc

File tree

30 files changed

+166
-360
lines changed

30 files changed

+166
-360
lines changed

docs/migration/MigratingFromV6ToV7.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,17 @@ The ActOnAll helper use to return an array of returns only for getters and a str
1616

1717
#### :alarm_clock: Now:
1818
Whatever you call will now result in an array of returns giving you the possibility to know which driver is returning an unexpected response via the ActOnAll helper.
19-
Also the ActOnAll helper does no longer implements `ExtendedCacheItemPoolInterface` due to the type hint implementation.
19+
Also the ActOnAll helper does no longer implements `ExtendedCacheItemPoolInterface` due to the type hint implementation.
20+
21+
### Cache Manager: Instance ID
22+
:new: As of the V7 you can now specify an instance ID using the CacheManager:
23+
24+
```php
25+
CacheManager::getInstance($defaultDriver, $options, $instanceId);
26+
```
27+
Please note that `$instanceId` must be a valid __STRING__.
28+
29+
There's also a variant that throw a `phpFastCacheInstanceNotFoundException` if the instance is not found.
30+
```php
31+
CacheManager::getInstanceById( $instanceId);
32+
```

src/phpFastCache/CacheManager.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
1818
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
1919
use phpFastCache\Exceptions\phpFastCacheDriverNotFoundException;
20+
use phpFastCache\Exceptions\phpFastCacheInstanceNotFoundException;
2021
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException;
2122
use phpFastCache\Exceptions\phpFastCacheInvalidConfigurationException;
2223

@@ -189,14 +190,23 @@ class CacheManager
189190
/**
190191
* @param string $driver
191192
* @param array $config
193+
* @param string $instanceId
194+
*
192195
* @return ExtendedCacheItemPoolInterface
196+
*
193197
* @throws phpFastCacheDriverCheckException
194198
* @throws phpFastCacheInvalidConfigurationException
199+
* @throws phpFastCacheDriverNotFoundException
200+
* @throws phpFastCacheInvalidArgumentException
195201
*/
196-
public static function getInstance($driver = 'auto', array $config = [])
202+
public static function getInstance($driver = 'auto', array $config = [], $instanceId = null)
197203
{
198204
static $badPracticeOmeter = [];
199205

206+
if($instanceId !== null && !is_string($instanceId)){
207+
throw new phpFastCacheInvalidArgumentException('The Instance ID must be a string');
208+
}
209+
200210
/**
201211
* @todo: Standardize a method for driver name
202212
*/
@@ -207,7 +217,8 @@ public static function getInstance($driver = 'auto', array $config = [])
207217
$driver = self::getAutoClass($config);
208218
}
209219

210-
$instance = crc32($driver . serialize($config));
220+
$instance = $instanceId ?: crc32($driver . serialize($config));
221+
211222
if (!isset(self::$instances[ $instance ])) {
212223
$badPracticeOmeter[ $driver ] = 1;
213224
if (!$config[ 'ignoreSymfonyNotice' ] && interface_exists('Symfony\Component\HttpKernel\KernelInterface') && !class_exists('phpFastCache\Bundle\phpFastCacheBundle')) {
@@ -217,7 +228,7 @@ public static function getInstance($driver = 'auto', array $config = [])
217228
$class = self::getNamespacePath() . $driver . '\Driver';
218229
try {
219230
if(class_exists($class)){
220-
self::$instances[ $instance ] = new $class($config);
231+
self::$instances[ $instance ] = new $class($config, $instance);
221232
self::$instances[ $instance ]->setEventManager(EventManager::getInstance());
222233
}else{
223234
throw new phpFastCacheDriverNotFoundException('The driver "%s" does not exists', $driver);
@@ -229,7 +240,7 @@ public static function getInstance($driver = 'auto', array $config = [])
229240
if ($fallback !== $driver) {
230241
$class = self::getNamespacePath() . $fallback . '\Driver';
231242
if(class_exists($class)){
232-
self::$instances[ $instance ] = new $class($config, EventManager::getInstance());
243+
self::$instances[ $instance ] = new $class($config, $instance);
233244
self::$instances[ $instance ]->setEventManager(EventManager::getInstance());
234245
}else{
235246
throw new phpFastCacheDriverNotFoundException('The driver "%s" does not exists', $driver);
@@ -256,6 +267,28 @@ public static function getInstance($driver = 'auto', array $config = [])
256267
return self::$instances[ $instance ];
257268
}
258269

270+
/**
271+
* @param string $instanceId
272+
*
273+
* @return ExtendedCacheItemPoolInterface
274+
*
275+
* @throws phpFastCacheInvalidArgumentException
276+
* @throws phpFastCacheInstanceNotFoundException
277+
*/
278+
public static function getInstanceById($instanceId): ExtendedCacheItemPoolInterface
279+
{
280+
if($instanceId !== null && !is_string($instanceId)){
281+
throw new phpFastCacheInvalidArgumentException('The Instance ID must be a string');
282+
}
283+
284+
if(isset(self::$instances[ $instanceId ]))
285+
{
286+
return self::$instances[ $instanceId ];
287+
}
288+
289+
throw new phpFastCacheInstanceNotFoundException(sprintf('Instance ID %s not found', $instanceId));
290+
}
291+
259292
/**
260293
* This method is intended for internal
261294
* use only and should not be used for

src/phpFastCache/Core/Pool/DriverBaseTrait.php

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

1717
use phpFastCache\Core\Item\ExtendedCacheItemInterface;
18+
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
1819
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException;
1920
use phpFastCache\Exceptions\phpFastCacheLogicException;
2021
use phpFastCache\Util\ArrayObject;
@@ -48,6 +49,30 @@ trait DriverBaseTrait
4849
*/
4950
protected $driverName;
5051

52+
/**
53+
* @internal This variable is read-access only
54+
* @var string
55+
*/
56+
protected $instanceId;
57+
58+
/**
59+
* Driver constructor.
60+
* @param array $config
61+
* @param string $instanceId
62+
* @throws phpFastCacheDriverCheckException
63+
*/
64+
public function __construct(array $config = [], $instanceId)
65+
{
66+
$this->setup($config);
67+
$this->instanceId = $instanceId;
68+
69+
if (!$this->driverCheck()) {
70+
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
71+
}else {
72+
$this->driverConnect();
73+
}
74+
}
75+
5176
/**
5277
* @param $config_name
5378
* @param string $value
@@ -232,6 +257,14 @@ public function getDriverName(): string
232257
return $this->driverName;
233258
}
234259

260+
/**
261+
* @return string
262+
*/
263+
public function getInstanceId(): string
264+
{
265+
return $this->instanceId;
266+
}
267+
235268
/**
236269
* @param \phpFastCache\Core\Item\ExtendedCacheItemInterface $item
237270
* @return bool

src/phpFastCache/Core/Pool/ExtendedCacheItemPoolInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public function getConfigOption($optionName);
8383
*/
8484
public function getDriverName(): string;
8585

86+
/**
87+
* @return mixed
88+
*/
89+
public function getInstanceId(): string;
8690

8791
/**
8892
* [phpFastCache phpDoc Override]

src/phpFastCache/Drivers/Apc/Driver.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@ class Driver implements ExtendedCacheItemPoolInterface
3131
{
3232
use DriverBaseTrait;
3333

34-
/**
35-
* Driver constructor.
36-
* @param array $config
37-
* @throws phpFastCacheDriverException
38-
*/
39-
public function __construct(array $config = [])
40-
{
41-
$this->setup($config);
42-
43-
if (!$this->driverCheck()) {
44-
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
45-
}
46-
}
47-
4834
/**
4935
* @return bool
5036
*/

src/phpFastCache/Drivers/Apcu/Driver.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,6 @@ class Driver implements ExtendedCacheItemPoolInterface
3131
{
3232
use DriverBaseTrait;
3333

34-
/**
35-
* Driver constructor.
36-
* @param array $config
37-
* @throws phpFastCacheDriverException
38-
*/
39-
public function __construct(array $config = [])
40-
{
41-
$this->setup($config);
42-
43-
if (!$this->driverCheck()) {
44-
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
45-
}
46-
}
47-
4834
/**
4935
* @return bool
5036
*/

src/phpFastCache/Drivers/Cassandra/Driver.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,6 @@ class Driver implements ExtendedCacheItemPoolInterface
3838

3939
use DriverBaseTrait;
4040

41-
/**
42-
* Driver constructor.
43-
* @param array $config
44-
* @throws phpFastCacheDriverException
45-
*/
46-
public function __construct(array $config = [])
47-
{
48-
$this->setup($config);
49-
50-
if (!$this->driverCheck()) {
51-
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
52-
} else {
53-
$this->driverConnect();
54-
}
55-
}
56-
5741
/**
5842
* @return bool
5943
*/

src/phpFastCache/Drivers/Cookie/Driver.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,6 @@ class Driver implements ExtendedCacheItemPoolInterface
3232

3333
const PREFIX = 'PFC_';
3434

35-
/**
36-
* Driver constructor.
37-
* @param array $config
38-
* @throws phpFastCacheDriverException
39-
*/
40-
public function __construct(array $config = [])
41-
{
42-
$this->setup($config);
43-
44-
if (!$this->driverCheck()) {
45-
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
46-
}
47-
}
48-
4935
/**
5036
* @return bool
5137
*/

src/phpFastCache/Drivers/Couchbase/Driver.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,6 @@ class Driver implements ExtendedCacheItemPoolInterface
4444
*/
4545
protected $bucketCurrent = '';
4646

47-
/**
48-
* Driver constructor.
49-
* @param array $config
50-
* @throws phpFastCacheDriverException
51-
*/
52-
public function __construct(array $config = [])
53-
{
54-
$this->setup($config);
55-
56-
if (!$this->driverCheck()) {
57-
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
58-
} else {
59-
$this->driverConnect();
60-
}
61-
}
62-
6347
/**
6448
* @return bool
6549
*/

src/phpFastCache/Drivers/Couchdb/Driver.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,6 @@ class Driver implements ExtendedCacheItemPoolInterface
3737

3838
use DriverBaseTrait;
3939

40-
/**
41-
* Driver constructor.
42-
* @param array $config
43-
* @throws phpFastCacheDriverException
44-
*/
45-
public function __construct(array $config = [])
46-
{
47-
$this->setup($config);
48-
49-
if (!$this->driverCheck()) {
50-
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
51-
} else {
52-
$this->driverConnect();
53-
}
54-
}
55-
5640
/**
5741
* @return bool
5842
*/

0 commit comments

Comments
 (0)