Skip to content

Commit 296f59c

Browse files
authored
Merge pull request #6 from mecha/task/3_default-ttl
Per-instance default TTL
2 parents ef216f1 + 42322c4 commit 296f59c

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

src/CachePool.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ class CachePool implements CacheInterface
4040
* @var mixed
4141
*/
4242
protected $defaultValue;
43+
/**
44+
* @var int|DateInterval
45+
*/
46+
protected $defaultTtl;
4347

4448
/**
4549
* @param wpdb $wpdb The WP database object.
4650
* @param string $poolName The name of this cache pool. Must be unique to this instance.
4751
* @param mixed $defaultValue A random value. Used for false-negative detection. The more chaotic - the better.
52+
* @param int|DateInterval $defaultTtl Default TTL to use when caching new entries.
4853
*/
49-
public function __construct(wpdb $wpdb, string $poolName, $defaultValue)
54+
public function __construct(wpdb $wpdb, string $poolName, $defaultValue, $defaultTtl = 0)
5055
{
5156
if ($poolName === static::OPTION_NAME_PREFIX_TIMEOUT) {
5257
throw new RangeException(sprintf('Pool name cannot be "%1$s"', static::OPTION_NAME_PREFIX_TIMEOUT));
@@ -55,6 +60,7 @@ public function __construct(wpdb $wpdb, string $poolName, $defaultValue)
5560
$this->wpdb = $wpdb;
5661
$this->poolName = $poolName;
5762
$this->defaultValue = $defaultValue;
63+
$this->defaultTtl = $defaultTtl;
5864
}
5965

6066
/**
@@ -89,6 +95,8 @@ public function set($key, $value, $ttl = null)
8995
$origKey = $key;
9096
$key = $this->prepareKey($key);
9197

98+
$ttl = is_null($ttl) ? $this->defaultTtl : $ttl;
99+
92100
try {
93101
$ttl = $ttl instanceof DateInterval
94102
? $this->getIntervalDuration($ttl)
@@ -97,8 +105,6 @@ public function set($key, $value, $ttl = null)
97105
throw new CacheException(sprintf('Could not normalize cache TTL'));
98106
}
99107

100-
$ttl = is_null($ttl) ? 0 : $ttl;
101-
102108
if (!is_int($ttl)) {
103109
throw new InvalidArgumentException(sprintf('The specified cache TTL is invalid'));
104110
}

src/CachePoolFactory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace WpOop\TransientCache;
55

6+
use DateInterval;
67
use Psr\SimpleCache\CacheInterface;
78
use wpdb;
89

@@ -15,10 +16,15 @@ class CachePoolFactory implements CachePoolFactoryInterface
1516
* @var wpdb
1617
*/
1718
protected $wpdb;
19+
/**
20+
* @var int|DateInterval
21+
*/
22+
protected $defaultTtl;
1823

19-
public function __construct(wpdb $wpdb)
24+
public function __construct(wpdb $wpdb, $defaultTtl = 0)
2025
{
2126
$this->wpdb = $wpdb;
27+
$this->defaultTtl = $defaultTtl;
2228
}
2329

2430
/**
@@ -27,7 +33,7 @@ public function __construct(wpdb $wpdb)
2733
public function createCachePool(string $poolName): CacheInterface
2834
{
2935
$default = uniqid('default');
30-
$pool = new CachePool($this->wpdb, $poolName, $default);
36+
$pool = new CachePool($this->wpdb, $poolName, $default, $this->defaultTtl);
3137

3238
return $pool;
3339
}

tests/functional/CachePoolTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ protected function tearDown()
2727
/**
2828
* @return TestSubject&MockObject
2929
*/
30-
public function createInstance(wpdb $wpdb, string $poolName, $defaultValue): TestSubject
30+
public function createInstance(wpdb $wpdb, string $poolName, $defaultValue, $defaultTtl = null): TestSubject
3131
{
3232
$mock = $this->getMockBuilder(TestSubject::class)
3333
->setMethods(null)
34-
->setConstructorArgs([$wpdb, $poolName, $defaultValue])
34+
->setConstructorArgs([$wpdb, $poolName, $defaultValue, $defaultTtl])
3535
->getMock();
3636

3737
return $mock;
@@ -251,6 +251,35 @@ public function testSet()
251251
}
252252
}
253253

254+
/**
255+
* @doesNotPerformAssertions
256+
* @throws \Brain\Monkey\Expectation\Exception\ExpectationArgsRequired
257+
* @throws \Psr\SimpleCache\InvalidArgumentException
258+
*/
259+
public function testSetDefaultTtl()
260+
{
261+
{
262+
$poolName = uniqid('pool');
263+
$wpdb = $this->createWpdb();
264+
$key = uniqid('key');
265+
$value = uniqid('value');
266+
$defaultTtl = new DateInterval('P2D');
267+
$separator = TestSubject::NAMESPACE_SEPARATOR;
268+
$transientName = "{$poolName}{$separator}{$key}";
269+
$subject = $this->createInstance($wpdb, $poolName, uniqid('default'), $defaultTtl);
270+
}
271+
272+
{
273+
Functions\expect('set_transient')
274+
->with($transientName, $value, 2 * 60 * 60 * 24)
275+
->andReturn(true);
276+
}
277+
278+
{
279+
$subject->set($key, $value, null);
280+
}
281+
}
282+
254283
/**
255284
* Tests that a correct exception is thrown when key too long.
256285
*

0 commit comments

Comments
 (0)