Skip to content

Commit

Permalink
[+]: use "symfony/var-exporter" for OpCache v2
Browse files Browse the repository at this point in the history
-> better performance, because we don't need to serialize the data
  • Loading branch information
voku committed Mar 15, 2020
1 parent 730f803 commit c057de1
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Expand Up @@ -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/"
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Expand Up @@ -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\./'
Expand Down
14 changes: 13 additions & 1 deletion src/voku/cache/AdapterOpCache.php
Expand Up @@ -79,14 +79,26 @@ protected function getFileName(string $key): string

/**
* {@inheritdoc}
*
* @noinspection PhpUndefinedClassInspection
* @noinspection PhpUndefinedNamespaceInspection
* @noinspection BadExceptionsProcessingInspection
*/
public function setExpired(string $key, $value, int $ttl = 0): bool
{
$item = [
'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 = '<?php return ' . $content . ';';

Expand Down
6 changes: 3 additions & 3 deletions src/voku/cache/AdapterPredis.php
Expand Up @@ -47,7 +47,7 @@ public function setPredisClient(Client $client)
*/
public function exists(string $key): bool
{
return $this->client->exists($key);
return (bool) $this->client->exists($key);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}
6 changes: 5 additions & 1 deletion src/voku/cache/Cache.php
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/voku/cache/CacheAdapterAutoManager.php
Expand Up @@ -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)
{
Expand All @@ -99,9 +99,9 @@ private function validateAdapter(string $replaceAdapter)
/**
* @param callable $callableFunction
*
* @return void
*
* @throws InvalidArgumentException
*
* @return void
*/
private function validateCallable(callable $callableFunction = null)
{
Expand Down
21 changes: 20 additions & 1 deletion tests/OpCacheVarExporterTest.php
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit c057de1

Please sign in to comment.