Skip to content

Commit d6dac1c

Browse files
authored
bugfix/2.2
1 parent b006d5c commit d6dac1c

11 files changed

Lines changed: 83 additions & 12 deletions

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
build
22
vendor
33
.idea
4+
.DS_Store
45
composer.lock
5-
*.yml
66
.env
7-
!.travis.yml
7+
*.cache

.scrutinizer.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
build:
2+
nodes:
3+
analysis:
4+
tests:
5+
stop_on_failure: true
6+
override:
7+
- php-scrutinizer-run
8+
environment:
9+
php:
10+
version: '7.2'
11+
dependencies:
12+
override:
13+
- composer install --no-interaction --prefer-source
14+
15+
filter:
16+
excluded_paths:
17+
- 'Tests/'
18+
- 'vendor/'
19+
20+
tools:
21+
php_analyzer: true
22+
external_code_coverage: true

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ script:
3636
- vendor/bin/phpunit --coverage-clover build/coverage/clover.xml
3737

3838
after_success:
39-
- travis_retry vendor/bin/codacycoverage clover build/coverage/clover.xml
39+
- travis_retry vendor/bin/ocular code-coverage:upload --format=php-clover build/coverage/clover.xml

CacheException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function forUnsupportedLogger(string $supported, string $given)
4242

4343
public static function forCreatingDirectory(string $directory)
4444
{
45-
return new static(Cache::E_DIRECTORY_NOT_CREATED, [':dir' => $directory]);
45+
return new self(Cache::E_DIRECTORY_NOT_CREATED, [':dir' => $directory]);
4646
}
4747

4848

Client/CacheClientFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private function createMemcachedClient(MemcachedConfiguration $conf): Cache
173173
$client->addServers($conf->getServers());
174174
}
175175

176-
return new MemcachedClient($client, $conf->get('ttl'));
176+
return new MemcachedClient($client, $conf->getTtl());
177177
}
178178

179179
/**

Configuration/MemcachedConfiguration.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public function __construct(array $options = [])
4848
\Memcached::OPT_REMOVE_FAILED_SERVERS => true,
4949
\Memcached::OPT_RETRY_TIMEOUT => 1,
5050
\Memcached::OPT_PREFIX_KEY => null
51-
], $options['options'] ?? [])
51+
], $options['options'] ?? []),
52+
'ttl' => $options['ttl'] ?? null
5253
]);
5354
}
5455

@@ -99,4 +100,18 @@ public function getOptions(): array
99100
return null !== $value;
100101
});
101102
}
103+
104+
/**
105+
* Returns the global TTL in seconds, or NULL for never-expire value.
106+
*
107+
* @return int|null
108+
*/
109+
public function getTtl(): ?int
110+
{
111+
if (null === $ttl = $this->get('ttl')) {
112+
return null;
113+
}
114+
115+
return (int)$ttl;
116+
}
102117
}

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Koded - Simple Caching Library
33

44
[![Latest Stable Version](https://img.shields.io/packagist/v/koded/cache-simple.svg)](https://packagist.org/packages/koded/cache-simple)
55
[![Build Status](https://travis-ci.org/kodedphp/cache-simple.svg?branch=master)](https://travis-ci.org/kodedphp/cache-simple)
6-
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/1b3bad367cc74a3fa98996c252cdfe6f)](https://www.codacy.com/app/kodeart/cache-simple)
7-
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/1b3bad367cc74a3fa98996c252cdfe6f)](https://www.codacy.com/app/kodeart/cache-simple)
6+
[![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)
7+
[![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)
88
[![Packagist Downloads](https://img.shields.io/packagist/dt/koded/cache-simple.svg)](https://packagist.org/packages/koded/cache-simple)
99
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.2-8892BF.svg)](https://php.net/)
1010
[![Software license](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](LICENSE)
@@ -160,6 +160,7 @@ $cache = simple_cache_factory('redis', [
160160
| id | string | no | Memcached persistent_id value |
161161
| servers | array | no | A list of nested array with \[server, port\] values |
162162
| options | array | no | A list of Memcached options |
163+
| ttl | int | no | Global TTL (in seconds) |
163164

164165
The following options are set **by default** when instance of `MemcachedConfiguration` is created,
165166
except for `OPT_PREFIX_KEY` which is there as a reminder that it may be set
@@ -198,7 +199,10 @@ Examples:
198199
\Memcached::OPT_PREFIX_KEY => 'i:', // item prefix
199200
\Memcached::OPT_REMOVE_FAILED_SERVERS => false, // change the default value
200201
\Memcached::OPT_DISTRIBUTION => null // remove this directive
201-
]
202+
],
203+
204+
// the global expiration time (for ALL cached items)
205+
'ttl' => 120,
202206
]
203207
```
204208

Tests/ClientFactoryTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ public function test_should_create_memcached_client()
2525
$this->assertInstanceOf(MemcachedClient::class, $client);
2626
}
2727

28+
/**
29+
* @depends test_should_create_memcached_client
30+
*/
31+
public function test_should_create_memcached_client_with_ttl()
32+
{
33+
if (false === extension_loaded('Memcached')) {
34+
$this->markTestSkipped('Memcached is not installed on this environment.');
35+
}
36+
37+
$client = (new CacheClientFactory(new ConfigFactory(['ttl' => 120])))->new('memcached');
38+
39+
$r = new \ReflectionClass($client);
40+
$ttl = $r->getProperty('ttl');
41+
$ttl->setAccessible(true);
42+
43+
$this->assertSame(120, $ttl->getValue($client));
44+
}
45+
2846
public function test_should_create_redis_client()
2947
{
3048
if (false === extension_loaded('redis')) {

Tests/Configuration/MemcachedConfigurationTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ public function test_should_build_default_arguments()
6868
], $config->getOptions());
6969
}
7070

71+
public function test_should_set_the_ttl()
72+
{
73+
$config = new MemcachedConfiguration(['ttl' => 120]);
74+
$this->assertSame(120, $config->getTtl());
75+
76+
$config = new MemcachedConfiguration(['ttl' => '120']);
77+
$this->assertSame(120, $config->getTtl());
78+
79+
$config = new MemcachedConfiguration;
80+
$this->assertNull($config->getTtl());
81+
}
82+
7183
protected function tearDown(): void
7284
{
7385
putenv('MEMCACHED_POOL=');

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.0
1+
2.2.0

0 commit comments

Comments
 (0)