From e2818fbaa61279e38365fbaddadd72757aabdb6c Mon Sep 17 00:00:00 2001 From: Divine Niiquaye Ibok Date: Wed, 5 Aug 2020 01:53:02 +0000 Subject: [PATCH] Removed Preloader class, use darkghosthunter/preloader library instead --- README.md | 36 +-------- src/Preloader.php | 196 ---------------------------------------------- 2 files changed, 2 insertions(+), 230 deletions(-) delete mode 100644 src/Preloader.php diff --git a/README.md b/README.md index 0ca5402..ddb7c4d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ [![Quality Score](https://img.shields.io/scrutinizer/g/biurad/php-cache.svg?style=flat-square)](https://scrutinizer-ci.com/g/biurad/php-cache) [![Sponsor development of this project](https://img.shields.io/badge/sponsor%20this%20package-%E2%9D%A4-ff69b4.svg?style=flat-square)](https://biurad.com/sponsor) -**biurad/php-cache** is a php cache library based on [Doctrine Cache][] created by [Divine Niiquaye][@divineniiquaye] which supports many different drivers such as redis, memcache, apc, mongodb and others. Implemented in [PSR-6] and [PSR-16] for great interoperability, performance and resiliency. +**biurad/php-cache** is a php cache library based on [Doctrine Cache][] created by [Doctrine Team][] which supports many different drivers such as redis, memcache, apc, mongodb and others. Implemented in [PSR-6] and [PSR-16] for great interoperability, performance and resiliency. ## 📦 Installation & Basic Usage @@ -35,9 +35,8 @@ The `Doctrine\Common\Cache\Cache` storage is very simple for performance and in | BiuradPHP\Cache\CacheItemPool | For [PSR-6] caching abilities using [PSR-16] | | BiuradPHP\Cache\FastCache | For advance and optimized [PSR-16]/[PSR-6] caching strategy | | BiuradPHP\Cache\MemoryCache | For caching using `var_export` | -| BiuradPHP\Cache\Preloader | For PHP 7.4 opache.preload abilities | -Now you can create, retrieve, update and delete items using the above caching classes except `Preloader` class: +Now you can create, retrieve, update and delete items using the above caching classes: ### For manipulation with cache using [PSR-16], we use the `Biurad\Cache\SimpleCache`: @@ -219,37 +218,6 @@ $value = $cache->save('my_cache_key', function (CacheItemInterface $item) { }, $beta); ``` -### For manipulation of PHP 7.4 opcache preload feature, we use the `BiuradPHP\Cache\Preloader`: - ---- - -Starting from PHP 7.4, OPcache can compile and load classes at start-up and make them available to all requests until the server is restarted, improving performance significantly. - -```php -use BiuradPHP\Cache\Preloader; - -$preloadClasses = [...]; // A list array of classes to be appended for preloading. -$preloadFile = getcwd().'/opcache.preload.php'; // The file preloaded classes to fetch from. - -Preloader::append($preloadFile, $preloadClasses); - -// to check opcache preload statistics -var_dump(Preloader::getStatistics()); -``` - -After the `$preloadFile` is written into, set the following configuration in your php.ini file: - -```ini -; php.ini -opcache.preload=/path/to/opcache.preload.php - -; maximum memory that OPcache can use to store compiled PHP files -opcache.memory_consumption=256 - -; maximum number of files that can be stored in the cache -opcache.max_accelerated_files=20000 -``` - ## 📓 Documentation For in-depth documentation before using this library.. Full documentation on advanced usage, configuration, and customization can be found at [docs.biurad.com][docs]. diff --git a/src/Preloader.php b/src/Preloader.php deleted file mode 100644 index 246af6b..0000000 --- a/src/Preloader.php +++ /dev/null @@ -1,196 +0,0 @@ - - * @copyright 2019 Biurad Group (https://biurad.com/) - * @license https://opensource.org/licenses/BSD-3-Clause License - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Biurad\Cache; - -use ErrorException; -use LogicException; -use ReflectionClass; -use ReflectionException; -use ReflectionMethod; -use ReflectionNamedType; -use ReflectionProperty; - -class Preloader -{ - private const PRELOAD_KEY_CACHE = 'preload_statistics'; - - /** - * Append an array of classes to preload. - * - * @param string $file - * @param array $list - */ - public static function append(string $file, array $list): void - { - if (!\file_exists($file)) { - throw new LogicException(\sprintf('File "%s" does not exist.', $file)); - } - - $cacheDir = \dirname($file); - $classes = []; - - foreach ($list as $item) { - if (0 === \strpos($item, $cacheDir)) { - \file_put_contents( - $file, - \sprintf("require_once __DIR__.%s;\n", \var_export(\substr($item, \strlen($cacheDir)), true)), - \FILE_APPEND - ); - - continue; - } - - $classes[] = \sprintf("\$classes[] = %s;\n", \var_export($item, true)); - } - - \file_put_contents( - $file, - \sprintf("\n\$classes = [];\n%sPreloader::preload(\$classes);\n", \implode('', $classes)), - \FILE_APPEND - ); - } - - /** - * Gives some information's about opcache preloading. - * - * @param string $type of 'functions', 'scripts' or 'classes' - * - * @return null|array - */ - public static function getStatus(string $type): ?array - { - $opcacheStatus = (array) \opcache_get_status(); - - return $opcacheStatus[self::PRELOAD_KEY_CACHE][$type] ?? null; - } - - /** - * Returns the opcache preload statistics - * - * @return null|array - */ - public static function getStatistics(): ?array - { - $opcacheStatus = (array) \opcache_get_status(); - - return $opcacheStatus[self::PRELOAD_KEY_CACHE] ?? null; - } - - /** - * @param array $classes - */ - public static function preload(array $classes): void - { - \set_error_handler([__CLASS__, 'errorHandler']); - - $prev = []; - $preloaded = []; - - try { - while ($prev !== $classes) { - $prev = $classes; - - foreach ($classes as $c) { - if (!isset($preloaded[$c])) { - self::doPreload($c, $preloaded); - } - } - $classes = \array_merge( - \get_declared_classes(), - \get_declared_interfaces(), - \get_declared_traits() ?? [] - ); - } - } finally { - \restore_error_handler(); - } - } - - /** - * @param string $class - * @param array $preloaded - * - * @psalm-suppress ArgumentTypeCoercion - * @psalm-suppress UndefinedMethod - */ - private static function doPreload(string $class, array &$preloaded): void - { - if (isset($preloaded[$class]) || \in_array($class, ['self', 'static', 'parent'], true)) { - return; - } - - $preloaded[$class] = true; - - try { - $r = new ReflectionClass($class); - - if ($r->isInternal()) { - return; - } - - $r->getConstants(); - $r->getDefaultProperties(); - - if (\PHP_VERSION_ID >= 70400) { - foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $p) { - if (null !== ($t = $p->getType()) && !$t->isBuiltin()) { - \assert($t instanceof ReflectionNamedType); - self::doPreload($t->getName(), $preloaded); - } - } - } - - foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $m) { - foreach ($m->getParameters() as $p) { - if ($p->isDefaultValueAvailable() && $p->isDefaultValueConstant()) { - $c = (string) $p->getDefaultValueConstantName(); - - if ($i = \strpos($c, '::')) { - self::doPreload(\substr($c, 0, $i), $preloaded); - } - } - - if (null !== ($t = $p->getType()) && !$t->isBuiltin()) { - \assert($t instanceof ReflectionNamedType); - self::doPreload($t->getName(), $preloaded); - } - } - - if (null !== ($t = $m->getReturnType()) && !$t->isBuiltin()) { - \assert($t instanceof ReflectionNamedType); - self::doPreload($t->getName(), $preloaded); - } - } - } catch (ReflectionException $e) { - // ignore missing classes - } - } - - private static function errorHandler(int $type, string $message, string $file, int $line): bool - { - if (\error_reporting() & $type) { - if (__FILE__ !== $file) { - throw new ErrorException($message, 0, $type, $file, $line); - } - - throw new ReflectionException($message); - } - - return false; - } -}