From c057de1db9107204c7d40ecbf47a4aae91e1ba90 Mon Sep 17 00:00:00 2001 From: Lars Moelleken Date: Sun, 15 Mar 2020 13:53:03 +0100 Subject: [PATCH] [+]: use "symfony/var-exporter" for OpCache v2 -> better performance, because we don't need to serialize the data --- .travis.yml | 4 ++-- composer.json | 7 +++++-- phpstan.neon | 4 ++++ src/voku/cache/AdapterOpCache.php | 14 +++++++++++++- src/voku/cache/AdapterPredis.php | 6 +++--- src/voku/cache/Cache.php | 6 +++++- src/voku/cache/CacheAdapterAutoManager.php | 8 ++++---- tests/OpCacheVarExporterTest.php | 21 ++++++++++++++++++++- 8 files changed, 56 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index b0eb341..89e4dca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,14 +46,14 @@ before_script: - travis_retry composer self-update - travis_retry composer require satooshi/php-coveralls:1.0.0 - travis_retry composer require predis/predis - - if [ "${TRAVIS_PHP_VERSION:0:3}" != 7.0 ]; then travis_retry composer require phpstan/phpstan; fi + - if [ "$(phpenv version-name)" == 7.3 ]; then travis_retry composer require phpstan/phpstan; fi - travis_retry composer install --no-interaction --prefer-source - composer dump-autoload -o script: - mkdir -p build/logs - php vendor/bin/phpunit -c phpunit.xml - - if [ "${TRAVIS_PHP_VERSION:0:3}" != 7.0 ]; then vendor/bin/phpstan analyse; fi + - if [ "$(phpenv version-name)" == 7.3 ]; then vendor/bin/phpstan analyse; fi after_script: - php vendor/bin/coveralls -v diff --git a/composer.json b/composer.json index 929be6d..999bc24 100644 --- a/composer.json +++ b/composer.json @@ -22,12 +22,15 @@ }, "require": { "php": ">=7.0.0", - "psr/simple-cache": "~1.0", - "symfony/var-exporter" : "~3.0 || ~4.0 || ~5.0" + "psr/simple-cache": "~1.0" }, "require-dev": { "phpunit/phpunit": "~6.0 || ~7.0" }, + "suggest": { + "symfony/var-exporter" : "~3.0 || ~4.0 || ~5.0", + "predis/predis": "~1.1" + }, "autoload": { "psr-4": { "voku\\cache\\": "src/voku/cache/" diff --git a/phpstan.neon b/phpstan.neon index b4a2a59..8824866 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -13,12 +13,16 @@ parameters: - message: ~If condition is always true~ path: %currentWorkingDirectory%/src/voku/cache/AdapterMemcached.php + - + message: ~If condition is always false~ + path: %currentWorkingDirectory%/src/voku/cache/AdapterMemcached.php - message: ~Right side of && is always true~ path: %currentWorkingDirectory%/src/voku/cache/AdapterMemcached.php - '/PHPDoc tag @return with type array\|false is not subtype of native type array\./' - '/Result of \&\& is always false\./' - '/Predis\\Client/' + - '/Symfony\\Component\\VarExporter/' - '/Memcache(d)*/' - '/MEMCACHE_COMPRESSED/' - '/Function checkForDev not found\./' diff --git a/src/voku/cache/AdapterOpCache.php b/src/voku/cache/AdapterOpCache.php index 73b58c1..1685587 100644 --- a/src/voku/cache/AdapterOpCache.php +++ b/src/voku/cache/AdapterOpCache.php @@ -79,6 +79,10 @@ protected function getFileName(string $key): string /** * {@inheritdoc} + * + * @noinspection PhpUndefinedClassInspection + * @noinspection PhpUndefinedNamespaceInspection + * @noinspection BadExceptionsProcessingInspection */ public function setExpired(string $key, $value, int $ttl = 0): bool { @@ -86,7 +90,15 @@ public function setExpired(string $key, $value, int $ttl = 0): bool 'value' => $value, 'ttl' => $ttl ? $ttl + \time() : 0, ]; - $content = \Symfony\Component\VarExporter\VarExporter::export($item); + if (\class_exists('\Symfony\Component\VarExporter\VarExporter')) { + try { + $content = \Symfony\Component\VarExporter\VarExporter::export($item); + } catch (\Symfony\Component\VarExporter\Exception\ExceptionInterface $e) { + $content = \var_export($item, true); + } + } else { + $content = \var_export($item, true); + } $content = 'client->exists($key); + return (bool) $this->client->exists($key); } /** @@ -71,7 +71,7 @@ public function installed(): bool */ public function remove(string $key): bool { - return $this->client->del($key); + return (bool) $this->client->del($key); } /** @@ -95,6 +95,6 @@ public function set(string $key, $value): bool */ public function setExpired(string $key, $value, int $ttl = 0): bool { - return $this->client->setex($key, $ttl, $value); + return (bool) $this->client->setex($key, $ttl, $value); } } diff --git a/src/voku/cache/Cache.php b/src/voku/cache/Cache.php index 5fab9f0..3b88917 100644 --- a/src/voku/cache/Cache.php +++ b/src/voku/cache/Cache.php @@ -169,7 +169,11 @@ public function __construct( ) { // INFO: Memcache(d) has his own "serializer", so don't use it twice $serializer = new SerializerNo(); - } elseif ($adapter instanceof AdapterOpCache) { + } elseif ( + $adapter instanceof AdapterOpCache + && + \class_exists('\Symfony\Component\VarExporter\VarExporter') + ) { // INFO: opcache + Symfony-VarExporter don't need any "serializer" $serializer = new SerializerNo(); } else { diff --git a/src/voku/cache/CacheAdapterAutoManager.php b/src/voku/cache/CacheAdapterAutoManager.php index ec0a8ed..cf07881 100644 --- a/src/voku/cache/CacheAdapterAutoManager.php +++ b/src/voku/cache/CacheAdapterAutoManager.php @@ -83,9 +83,9 @@ public function merge(self $adapterManager): self * * @psalm-param class-string $replaceAdapter * - * @return void - * * @throws InvalidArgumentException + * + * @return void */ private function validateAdapter(string $replaceAdapter) { @@ -99,9 +99,9 @@ private function validateAdapter(string $replaceAdapter) /** * @param callable $callableFunction * - * @return void - * * @throws InvalidArgumentException + * + * @return void */ private function validateCallable(callable $callableFunction = null) { diff --git a/tests/OpCacheVarExporterTest.php b/tests/OpCacheVarExporterTest.php index 1159223..95c12b7 100644 --- a/tests/OpCacheVarExporterTest.php +++ b/tests/OpCacheVarExporterTest.php @@ -88,6 +88,20 @@ public function testSetGetItemWithPrefix() static::assertSame([3, 2, 1], $return); } + public function testSetGetSimpleItemWithoutPrefix() + { + $this->cache->setPrefix(''); + $prefix = $this->cache->getPrefix(); + static::assertSame('', $prefix); + + // https://github.com/symfony/var-exporter/blob/master/Internal/Exporter.php#L188 + $return = $this->cache->setItem('lalll', false); + static::assertTrue($return); + + $return = $this->cache->getItem('lalll'); + static::assertFalse($return); + } + public function testSetGetCacheWithEndDateTime() { $expireDate = new DateTime(); @@ -208,7 +222,12 @@ public function testGetUsedSerializerClassName() protected function setUp() { $this->adapter = new AdapterOpCache(); - $this->serializer = new \voku\cache\SerializerNo(); + + if (\class_exists('\Symfony\Component\VarExporter\VarExporter')) { + $this->serializer = new \voku\cache\SerializerNo(); + } else { + $this->serializer = new \voku\cache\SerializerDefault(); + } $this->cache = new Cache($this->adapter, $this->serializer, false, true);