Skip to content

Commit 852e302

Browse files
committed
Use a property
1 parent 3e42bfd commit 852e302

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,26 @@ class Pool implements CacheItemPoolInterface, TaggablePoolInterface
8181

8282
### Implement interface and use trait for CacheItemInterface
8383

84-
The purpose of the trait is to be able to return a `taggedKey` and a normal `key`. Use the trait and rename your
85-
`getKey()` to `getTaggedKey()`.
84+
The purpose of the trait is to be able to return a `taggedKey` and a normal `key`. The trait has one protected function
85+
`getKeyFromTaggedKey()` and one public function `getTaggedkey()`. You should use the trait and make sure you set values
86+
to your two keys.
8687

8788
```php
8889
class Pool implements CacheItemInterface, TaggableItemInterface
8990
{
9091
use TaggableItemTrait;
9192

92-
private $key;
93+
private $normalKey;
9394

94-
// Rename from getKey to getTaggedKey
95-
public function getTaggedKey()
95+
public function __construct($key)
9696
{
97-
return $this->key;
97+
$this->taggedKey = $key;
98+
$this->normalKey = $this->getKeyFromTaggedKey($key);
99+
}
100+
101+
public function getKey()
102+
{
103+
return $this->normalKey;
98104
}
99105

100106
// ...

src/TaggableItemTrait.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,31 @@
1717
trait TaggableItemTrait
1818
{
1919
/**
20+
* @type string
21+
*/
22+
protected $taggedKey;
23+
24+
/**
25+
* A key that is dependent on the tags.
26+
*
2027
* @return string
2128
*/
22-
abstract public function getTaggedKey();
29+
public function getTaggedKey()
30+
{
31+
return $this->taggedKey;
32+
}
2333

2434
/**
25-
* Return the key for this item.
35+
* Return the cache key for this item. This is the generic cache key that the calling library sees.
2636
*
2737
* @return string
2838
*/
29-
public function getKey()
39+
protected function getKeyFromTaggedKey($taggedKey)
3040
{
31-
$key = $this->getTaggedKey();
32-
if (false === $pos = strrpos($key, ':')) {
33-
return $key;
41+
if (false === $pos = strrpos($taggedKey, ':')) {
42+
return $taggedKey;
3443
}
3544

36-
return substr($key, $pos + 1);
45+
return substr($taggedKey, $pos + 1);
3746
}
3847
}

tests/Helper/CacheItem.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class CacheItem implements CacheItemInterface, TaggableItemInterface
3939
*/
4040
public function __construct($key)
4141
{
42-
$this->key = $key;
42+
$this->taggedKey = $key;
43+
$this->key = $this->getKeyFromTaggedKey($key);
4344
}
4445

4546
/**
@@ -53,7 +54,7 @@ public function isHit()
5354
/**
5455
* @return string
5556
*/
56-
public function getTaggedKey()
57+
public function getKey()
5758
{
5859
return $this->key;
5960
}

0 commit comments

Comments
 (0)