Skip to content

Commit e96391e

Browse files
committed
Improved extension mechanism performances
1 parent 58f77cf commit e96391e

File tree

8 files changed

+222
-42
lines changed

8 files changed

+222
-42
lines changed

lib/Phpfastcache/CacheManager.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
2424
use Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException;
2525
use Phpfastcache\Exceptions\PhpfastcacheExtensionNotFoundException;
26+
use Phpfastcache\Exceptions\PhpfastcacheExtensionNotInstalledException;
2627
use Phpfastcache\Exceptions\PhpfastcacheInstanceNotFoundException;
2728
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
2829
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
@@ -59,6 +60,11 @@ class CacheManager
5960
*/
6061
protected static array $driverCustoms = [];
6162

63+
/**
64+
* @var string[]
65+
*/
66+
protected static array $driverExtensions = [];
67+
6268
/**
6369
* @param string $instanceId
6470
* @return ExtendedCacheItemPoolInterface
@@ -91,6 +97,7 @@ public static function getInstances(): array
9197
* @throws PhpfastcacheDriverCheckException
9298
* @throws PhpfastcacheDriverException
9399
* @throws PhpfastcacheDriverNotFoundException
100+
* @throws PhpfastcacheExtensionNotInstalledException
94101
* @throws PhpfastcacheLogicException
95102
*/
96103
public static function getInstance(string $driver, ?ConfigurationOptionInterface $config = null, ?string $instanceId = null): ExtendedCacheItemPoolInterface
@@ -116,22 +123,19 @@ public static function getInstance(string $driver, ?ConfigurationOptionInterface
116123
);
117124
} else {
118125
try {
119-
ExtensionManager::loadExtension($driver);
126+
self::$driverExtensions[$driver] = ExtensionManager::getExtension($driver);
120127
return CacheManager::getInstance($driver, $config, $instanceId);
121128
} catch (PhpfastcacheExtensionNotFoundException) {
122-
// Temporary check until v10
123-
$extensionWarning = '';
124-
if (in_array($driver, ['Arangodb', 'Couchdb', 'Dynamodb', 'Firestore', 'Mongodb', 'Solr'], true)) {
125-
$extensionWarning .= sprintf(
126-
'However, it seems that you are using a driver which is now an extension. Run the following command to solve this issue: %s',
129+
if (in_array($driver, ExtensionManager::KNOWN_EXTENSION_NAMES, true)) {
130+
throw new PhpfastcacheExtensionNotInstalledException(sprintf(
131+
'You requested a driver which is now an extension. Run the following command to solve this issue: %s',
127132
sprintf('composer install phpfastcache/%s-extension', strtolower($driver))
128-
);
133+
));
129134
}
130135
throw new PhpfastcacheDriverNotFoundException(sprintf(
131-
'The driver "%s" does not exist or does not implement %s. %s',
136+
'The driver "%s" does not exist or does not implement %s.',
132137
$driver,
133138
ExtendedCacheItemPoolInterface::class,
134-
$extensionWarning,
135139
));
136140
}
137141
}
@@ -191,7 +195,9 @@ public static function normalizeDriverName(string $driverName): string
191195
*/
192196
public static function getDriverClass(string $driverName): string
193197
{
194-
if (!empty(self::$driverCustoms[$driverName])) {
198+
if (!empty(self::$driverExtensions[$driverName])) {
199+
$driverClass = self::$driverExtensions[$driverName];
200+
} elseif (!empty(self::$driverCustoms[$driverName])) {
195201
$driverClass = self::$driverCustoms[$driverName];
196202
} elseif (!empty(self::$driverOverrides[$driverName])) {
197203
$driverClass = self::$driverOverrides[$driverName];

lib/Phpfastcache/Exceptions/PhpfastcacheExtensionNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616

1717
namespace Phpfastcache\Exceptions;
1818

19-
class PhpfastcacheExtensionNotFoundException extends PhpfastcacheDriverException
19+
class PhpfastcacheExtensionNotFoundException extends PhpfastcacheDriverNotFoundException
2020
{
2121
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of Phpfastcache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10+
*
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace Phpfastcache\Exceptions;
18+
19+
class PhpfastcacheExtensionNotInstalledException extends PhpfastcacheDriverNotFoundException
20+
{
21+
}

lib/Phpfastcache/ExtensionManager.php

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,67 @@
1616

1717
namespace Phpfastcache;
1818

19-
use Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException;
2019
use Phpfastcache\Exceptions\PhpfastcacheExtensionNotFoundException;
2120
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
2221
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
2322
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedOperationException;
2423
use Phpfastcache\Helper\UninstanciableObjectTrait;
2524

25+
/**
26+
* @internal This extension manager is meant to manager officials Phpfastcache's extensions.
27+
* @see \Phpfastcache\CacheManager::addCustomDriver() to add you own drivers.
28+
*/
2629
final class ExtensionManager
2730
{
2831
use UninstanciableObjectTrait;
2932

33+
public const KNOWN_EXTENSION_NAMES = [
34+
'Arangodb',
35+
'Couchbasev4',
36+
'Couchdb',
37+
'Dynamodb',
38+
'Firestore',
39+
'Mongodb',
40+
'Solr'
41+
];
42+
3043
/**
3144
* @var array<string, string>
3245
*/
3346
protected static array $registeredExtensions = [];
3447

3548
public static function registerExtension(string $extensionName, string $driverClassName): void
3649
{
50+
if (!str_starts_with($driverClassName, ltrim('Phpfastcache\\Extensions\\', '\\'))) {
51+
throw new PhpfastcacheInvalidArgumentException(
52+
'Only extensions from "\\Phpfastcache\\Extensions\\" namespace are allowed. Use CacheManager::addCustomDriver() to create your own extensions.'
53+
);
54+
}
3755
self::$registeredExtensions[$extensionName] = $driverClassName;
3856
}
3957

40-
/**
41-
* Autoload all the discoverable extensions.
42-
*
43-
* @return void
44-
* @throws PhpfastcacheExtensionNotFoundException
45-
* @throws PhpfastcacheInvalidArgumentException
46-
* @throws PhpfastcacheLogicException
47-
* @throws PhpfastcacheUnsupportedOperationException
48-
*/
49-
public static function autoloadExtensions(): void
58+
public static function extensionExists(string $extensionName): bool
5059
{
51-
foreach (self::$registeredExtensions as $extension) {
52-
self::loadExtension($extension);
53-
}
60+
return isset(self::$registeredExtensions[$extensionName]);
5461
}
5562

5663
/**
5764
* @param string $name
58-
* @return void
65+
* @return string
5966
* @throws PhpfastcacheExtensionNotFoundException
60-
* @throws PhpfastcacheInvalidArgumentException
61-
* @throws PhpfastcacheLogicException
62-
* @throws PhpfastcacheUnsupportedOperationException
6367
*/
64-
public static function loadExtension(string $name): void
68+
public static function getExtension(string $name): string
6569
{
66-
if (!CacheManager::customDriverExists($name)) {
67-
if (isset(self::$registeredExtensions[$name])) {
68-
CacheManager::addCustomDriver($name, self::$registeredExtensions[$name]);
69-
} else {
70-
throw new PhpfastcacheExtensionNotFoundException(
71-
sprintf(
72-
'Unable too find the %s extension. Make sure that you you added through composer: `composer require phpfastcache/%s-extension`',
73-
$name,
74-
strtolower($name)
75-
)
76-
);
77-
}
70+
if (isset(self::$registeredExtensions[$name])) {
71+
return self::$registeredExtensions[$name];
72+
} else {
73+
throw new PhpfastcacheExtensionNotFoundException(
74+
sprintf(
75+
'Unable too find the %s extension. Make sure that you you added through composer: `composer require phpfastcache/%s-extension`',
76+
$name,
77+
strtolower($name)
78+
)
79+
);
7880
}
7981
}
8082
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of Phpfastcache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10+
*
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13+
*/
14+
15+
use Phpfastcache\CacheManager;
16+
use Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException;
17+
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
18+
use Phpfastcache\Exceptions\PhpfastcacheExtensionNotInstalledException;
19+
use Phpfastcache\Tests\Helper\TestHelper;
20+
21+
chdir(__DIR__);
22+
require_once __DIR__ . '/../../vendor/autoload.php';
23+
require_once __DIR__ . '/../mock/Autoload.php';
24+
$testHelper = new TestHelper('Apcu test (CRUD)');
25+
26+
try {
27+
$pool = CacheManager::getInstance('Arangodb');
28+
$testHelper->assertFail('CacheManager didnt thrown an Exception');
29+
} catch (PhpfastcacheExtensionNotInstalledException) {
30+
$testHelper->assertPass('CacheManager thrown a PhpfastcacheExtensionNotInstalledException.');
31+
} catch (\Throwable $e) {
32+
$testHelper->assertFail('CacheManager thrown a ' . $e::class);
33+
}
34+
35+
try {
36+
$pool = CacheManager::getInstance(bin2hex(random_bytes(8)));
37+
$testHelper->assertFail('CacheManager didnt thrown an Exception');
38+
} catch (PhpfastcacheDriverNotFoundException $e) {
39+
if ($e::class === PhpfastcacheDriverNotFoundException::class) {
40+
$testHelper->assertPass('CacheManager thrown a PhpfastcacheDriverNotFoundException.');
41+
} else {
42+
$testHelper->assertFail('CacheManager thrown a ' . $e::class);
43+
}
44+
} catch (\Throwable $e) {
45+
$testHelper->assertFail('CacheManager thrown a ' . $e::class);
46+
}
47+
48+
try {
49+
\Phpfastcache\ExtensionManager::registerExtension(
50+
'Extensiontest',
51+
\Phpfastcache\Extensions\Drivers\Extensiontest\Driver::class
52+
);
53+
$testHelper->assertPass('Registered a test extension.');
54+
} catch (PhpfastcacheInvalidArgumentException) {
55+
$testHelper->assertFail('Failed to register a test extension.');
56+
}
57+
58+
try {
59+
CacheManager::getInstance('Extensiontest');
60+
$testHelper->assertPass('Retrieved a test extension cache pool.');
61+
} catch (PhpfastcacheDriverNotFoundException) {
62+
$testHelper->assertFail('Failed to retrieve a test extension cache pool.');
63+
}
64+
65+
66+
$testHelper->terminateTest();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of Phpfastcache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
9+
*
10+
* @author Georges.L (Geolim4) <[email protected]>
11+
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
12+
*/
13+
14+
namespace Phpfastcache\Extensions\Drivers\Extensiontest;
15+
16+
use Phpfastcache\Drivers\Files\Config as FilesConfig;
17+
18+
class Config extends FilesConfig
19+
{
20+
/**
21+
* @var bool
22+
*/
23+
protected bool $customOption = true;
24+
25+
/**
26+
* @return bool
27+
*/
28+
public function isCustomOption(): bool
29+
{
30+
return $this->customOption;
31+
}
32+
33+
/**
34+
* @param bool $customOption
35+
* @return Config
36+
*/
37+
public function setCustomOption(bool $customOption): Config
38+
{
39+
$this->customOption = $customOption;
40+
return $this;
41+
}
42+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of Phpfastcache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
9+
*
10+
* @author Georges.L (Geolim4) <[email protected]>
11+
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
12+
*/
13+
14+
namespace Phpfastcache\Extensions\Drivers\Extensiontest;
15+
16+
use Phpfastcache\Drivers\Files\Driver as FilesDriver;
17+
18+
class Driver extends FilesDriver
19+
{
20+
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of Phpfastcache.
5+
*
6+
* @license MIT License (MIT)
7+
*
8+
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
9+
*
10+
* @author Georges.L (Geolim4) <[email protected]>
11+
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
12+
*/
13+
14+
namespace Phpfastcache\Extensions\Drivers\Extensiontest;
15+
16+
use Phpfastcache\Drivers\Files\Item as FilesItem;
17+
18+
19+
class Item extends FilesItem
20+
{
21+
22+
}

0 commit comments

Comments
 (0)