diff --git a/.gitignore b/.gitignore index 5f677a0..c21488b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ build vendor .idea +.DS_Store composer.lock -*.yml .env -!.travis.yml \ No newline at end of file +*.cache diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..b44a99f --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,22 @@ +build: + nodes: + analysis: + tests: + stop_on_failure: true + override: + - php-scrutinizer-run + environment: + php: + version: '7.2' + dependencies: + override: + - composer install --no-interaction --prefer-source + +filter: + excluded_paths: + - 'Tests/' + - 'vendor/' + +tools: + php_analyzer: true + external_code_coverage: true diff --git a/.travis.yml b/.travis.yml index 496c218..b351fcb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,4 +36,4 @@ script: - vendor/bin/phpunit --coverage-clover build/coverage/clover.xml after_success: - - travis_retry vendor/bin/codacycoverage clover build/coverage/clover.xml + - travis_retry vendor/bin/ocular code-coverage:upload --format=php-clover build/coverage/clover.xml diff --git a/CacheException.php b/CacheException.php index 51cdbc7..809481e 100644 --- a/CacheException.php +++ b/CacheException.php @@ -42,7 +42,7 @@ public static function forUnsupportedLogger(string $supported, string $given) public static function forCreatingDirectory(string $directory) { - return new static(Cache::E_DIRECTORY_NOT_CREATED, [':dir' => $directory]); + return new self(Cache::E_DIRECTORY_NOT_CREATED, [':dir' => $directory]); } diff --git a/Client/CacheClientFactory.php b/Client/CacheClientFactory.php index 97edf0e..edc65cc 100644 --- a/Client/CacheClientFactory.php +++ b/Client/CacheClientFactory.php @@ -173,7 +173,7 @@ private function createMemcachedClient(MemcachedConfiguration $conf): Cache $client->addServers($conf->getServers()); } - return new MemcachedClient($client, $conf->get('ttl')); + return new MemcachedClient($client, $conf->getTtl()); } /** diff --git a/Configuration/MemcachedConfiguration.php b/Configuration/MemcachedConfiguration.php index cda3b3f..b63061b 100644 --- a/Configuration/MemcachedConfiguration.php +++ b/Configuration/MemcachedConfiguration.php @@ -48,7 +48,8 @@ public function __construct(array $options = []) \Memcached::OPT_REMOVE_FAILED_SERVERS => true, \Memcached::OPT_RETRY_TIMEOUT => 1, \Memcached::OPT_PREFIX_KEY => null - ], $options['options'] ?? []) + ], $options['options'] ?? []), + 'ttl' => $options['ttl'] ?? null ]); } @@ -99,4 +100,18 @@ public function getOptions(): array return null !== $value; }); } + + /** + * Returns the global TTL in seconds, or NULL for never-expire value. + * + * @return int|null + */ + public function getTtl(): ?int + { + if (null === $ttl = $this->get('ttl')) { + return null; + } + + return (int)$ttl; + } } diff --git a/README.md b/README.md index f149b39..5f3a070 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ Koded - Simple Caching Library [![Latest Stable Version](https://img.shields.io/packagist/v/koded/cache-simple.svg)](https://packagist.org/packages/koded/cache-simple) [![Build Status](https://travis-ci.org/kodedphp/cache-simple.svg?branch=master)](https://travis-ci.org/kodedphp/cache-simple) -[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/1b3bad367cc74a3fa98996c252cdfe6f)](https://www.codacy.com/app/kodeart/cache-simple) -[![Codacy Badge](https://api.codacy.com/project/badge/Grade/1b3bad367cc74a3fa98996c252cdfe6f)](https://www.codacy.com/app/kodeart/cache-simple) +[![Code Coverage](https://scrutinizer-ci.com/g/kodedphp/cache-simple/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/kodedphp/cache-simple/?branch=master) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/kodedphp/cache-simple/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/kodedphp/cache-simple/?branch=master) [![Packagist Downloads](https://img.shields.io/packagist/dt/koded/cache-simple.svg)](https://packagist.org/packages/koded/cache-simple) [![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.2-8892BF.svg)](https://php.net/) [![Software license](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](LICENSE) @@ -160,6 +160,7 @@ $cache = simple_cache_factory('redis', [ | id | string | no | Memcached persistent_id value | | servers | array | no | A list of nested array with \[server, port\] values | | options | array | no | A list of Memcached options | +| ttl | int | no | Global TTL (in seconds) | The following options are set **by default** when instance of `MemcachedConfiguration` is created, except for `OPT_PREFIX_KEY` which is there as a reminder that it may be set @@ -198,7 +199,10 @@ Examples: \Memcached::OPT_PREFIX_KEY => 'i:', // item prefix \Memcached::OPT_REMOVE_FAILED_SERVERS => false, // change the default value \Memcached::OPT_DISTRIBUTION => null // remove this directive - ] + ], + + // the global expiration time (for ALL cached items) + 'ttl' => 120, ] ``` diff --git a/Tests/ClientFactoryTest.php b/Tests/ClientFactoryTest.php index 5906893..61eb178 100644 --- a/Tests/ClientFactoryTest.php +++ b/Tests/ClientFactoryTest.php @@ -25,6 +25,24 @@ public function test_should_create_memcached_client() $this->assertInstanceOf(MemcachedClient::class, $client); } + /** + * @depends test_should_create_memcached_client + */ + public function test_should_create_memcached_client_with_ttl() + { + if (false === extension_loaded('Memcached')) { + $this->markTestSkipped('Memcached is not installed on this environment.'); + } + + $client = (new CacheClientFactory(new ConfigFactory(['ttl' => 120])))->new('memcached'); + + $r = new \ReflectionClass($client); + $ttl = $r->getProperty('ttl'); + $ttl->setAccessible(true); + + $this->assertSame(120, $ttl->getValue($client)); + } + public function test_should_create_redis_client() { if (false === extension_loaded('redis')) { diff --git a/Tests/Configuration/MemcachedConfigurationTest.php b/Tests/Configuration/MemcachedConfigurationTest.php index 4e752db..507108d 100644 --- a/Tests/Configuration/MemcachedConfigurationTest.php +++ b/Tests/Configuration/MemcachedConfigurationTest.php @@ -68,6 +68,18 @@ public function test_should_build_default_arguments() ], $config->getOptions()); } + public function test_should_set_the_ttl() + { + $config = new MemcachedConfiguration(['ttl' => 120]); + $this->assertSame(120, $config->getTtl()); + + $config = new MemcachedConfiguration(['ttl' => '120']); + $this->assertSame(120, $config->getTtl()); + + $config = new MemcachedConfiguration; + $this->assertNull($config->getTtl()); + } + protected function tearDown(): void { putenv('MEMCACHED_POOL='); diff --git a/VERSION b/VERSION index 50aea0e..e3a4f19 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.1.0 \ No newline at end of file +2.2.0 \ No newline at end of file diff --git a/composer.json b/composer.json index 1ad0d75..66165b7 100644 --- a/composer.json +++ b/composer.json @@ -48,8 +48,8 @@ "mikey179/vfsstream": "~1", "predis/predis": "dev-master", "cache/integration-tests": "dev-master", - "codacy/coverage": "dev-master", - "symfony/phpunit-bridge": "^4.4@dev" + "symfony/phpunit-bridge": "^4.4@dev", + "scrutinizer/ocular": "^1.6" }, "extra": { "branch-alias": {