Skip to content

Commit 4fb5595

Browse files
committed
Merge pull request #14 from php-cache/callable
Using callable
2 parents 6dad3bc + aec9549 commit 4fb5595

File tree

3 files changed

+48
-37
lines changed

3 files changed

+48
-37
lines changed

src/AbstractCachePool.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,19 @@ protected function getItemWithoutGenerateCacheKey($key)
6161
return is_object($item) ? clone $item : $item;
6262
}
6363

64-
$item = $this->fetchObjectFromCache($key);
65-
if (false === $item || !$item instanceof CacheItemInterface) {
66-
$item = new CacheItem($key);
67-
}
64+
$func = function () use ($key) {
65+
return $this->fetchObjectFromCache($key);
66+
};
6867

69-
return $item;
68+
return new CacheItem($key, $func);
7069
}
7170

7271
/**
7372
* Fetch an object from the cache implementation.
7473
*
7574
* @param string $key
7675
*
77-
* @return CacheItem|false
76+
* @return array with [isHit, value]
7877
*/
7978
abstract protected function fetchObjectFromCache($key);
8079

@@ -168,11 +167,6 @@ abstract protected function clearOneObjectFromCache($key);
168167
*/
169168
public function save(CacheItemInterface $item)
170169
{
171-
// This item has no data
172-
if (!$item->isHit()) {
173-
return false;
174-
}
175-
176170
if ($item instanceof TaggableItemInterface) {
177171
$key = $item->getTaggedKey();
178172
} else {

src/CacheItem.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class CacheItem implements HasExpirationDateInterface, CacheItemInterface, Tagga
2323
{
2424
use TaggableItemTrait;
2525

26+
/**
27+
* @type \Closure
28+
*/
29+
private $callable;
30+
2631
/**
2732
* @type string
2833
*/
@@ -44,20 +49,20 @@ class CacheItem implements HasExpirationDateInterface, CacheItemInterface, Tagga
4449
private $hasValue = false;
4550

4651
/**
47-
* @param string $key
48-
* @param bool $hasValue
49-
* @param mixed $value
50-
* @param \DateTimeInterface|null $expirationDate
52+
* @param string $key
53+
* @param \Closure|bool $callable or boolean hasValue
5154
*/
52-
public function __construct($key, $hasValue = false, $value = null, \DateTimeInterface $expirationDate = null)
55+
public function __construct($key, $callable = null, $value = null)
5356
{
54-
$this->taggedKey = $key;
55-
$this->key = $this->getKeyFromTaggedKey($key);
56-
$this->hasValue = $hasValue;
57-
$this->expirationDate = $expirationDate;
58-
59-
if ($hasValue) {
60-
$this->value = $value;
57+
$this->taggedKey = $key;
58+
$this->key = $this->getKeyFromTaggedKey($key);
59+
60+
if ($callable === true) {
61+
$this->hasValue = true;
62+
$this->value = $value;
63+
} elseif ($callable !== false) {
64+
// This must be a callable or null
65+
$this->callable = $callable;
6166
}
6267
}
6368

@@ -76,6 +81,7 @@ public function set($value)
7681
{
7782
$this->value = $value;
7883
$this->hasValue = true;
84+
$this->callable = null;
7985

8086
return $this;
8187
}
@@ -97,15 +103,18 @@ public function get()
97103
*/
98104
public function isHit()
99105
{
100-
if (!$this->hasValue) {
101-
return false;
106+
if ($this->callable !== null) {
107+
// Initialize
108+
$f = $this->callable;
109+
list($this->hasValue, $this->value) = $f();
110+
$this->callable = null;
102111
}
103112

104-
if ($this->expirationDate === null) {
105-
return true;
113+
if (!$this->hasValue) {
114+
return false;
106115
}
107116

108-
return (new \DateTime()) <= $this->expirationDate;
117+
return true;
109118
}
110119

111120
/**
@@ -121,7 +130,11 @@ public function getExpirationDate()
121130
*/
122131
public function expiresAt($expiration)
123132
{
124-
$this->expirationDate = $expiration;
133+
if ($expiration instanceof \DateTimeInterface) {
134+
$this->expirationDate = clone $expiration;
135+
} else {
136+
$this->expirationDate = $expiration;
137+
}
125138

126139
return $this;
127140
}

tests/CacheItemTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,22 @@ public function testGet()
6060

6161
public function testHit()
6262
{
63-
$item = new CacheItem('test_key');
64-
$this->assertFalse($item->isHit());
65-
66-
$item->set('foobar');
63+
$item = new CacheItem('test_key', true, 'value');
6764
$this->assertTrue($item->isHit());
6865

69-
$item->set(null);
70-
$this->assertTrue($item->isHit());
66+
$item = new CacheItem('test_key', false, 'value');
67+
$this->assertFalse($item->isHit());
7168

72-
$item->expiresAfter(5);
69+
$closure = function () {
70+
return [true, 'value'];
71+
};
72+
$item = new CacheItem('test_key', $closure);
7373
$this->assertTrue($item->isHit());
74-
$item->expiresAfter(-1);
74+
75+
$closure = function () {
76+
return [false, null];
77+
};
78+
$item = new CacheItem('test_key', $closure);
7579
$this->assertFalse($item->isHit());
7680
}
7781

0 commit comments

Comments
 (0)