Skip to content

Commit bfd1927

Browse files
Added docs and made tags lazy load.
1 parent f3b56fa commit bfd1927

2 files changed

Lines changed: 109 additions & 13 deletions

File tree

TaggablePSR6ItemAdapter.php

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
312
namespace Cache\Taggable;
413

514
use Cache\Taggable\TaggableItemInterface;
@@ -9,30 +18,53 @@
918
use Psr\Cache\CacheItemPoolInterface;
1019

1120
/**
12-
*
13-
*/
21+
* @internal
22+
*
23+
* An adapter for non-taggable cache items, to be used with the cache pool
24+
* adapter.
25+
*
26+
* This adapter stores tags along with the cached value, by storing wrapping
27+
* the item in an array structure containing both.
28+
*
29+
* @author Magnus Nordlander <magnus@fervo.se>
30+
*/
1431
class TaggablePSR6ItemAdapter implements TaggableItemInterface
1532
{
33+
/**
34+
* @type boolean
35+
*/
36+
private $initialized = false;
37+
38+
/**
39+
* @type CacheItemInterface
40+
*/
1641
private $cacheItem;
42+
43+
/**
44+
* @type array<string>
45+
*/
1746
private $tags = [];
1847

48+
/**
49+
* @param CacheItemInterface $cacheItem
50+
*/
1951
private function __construct(CacheItemInterface $cacheItem)
2052
{
2153
$this->cacheItem = $cacheItem;
22-
if ($this->cacheItem->isHit()) {
23-
$rawItem = $this->cacheItem->get();
24-
25-
if (is_array($rawItem) && isset($rawItem['tags'])) {
26-
$this->tags = $rawItem['tags'];
27-
}
28-
}
2954
}
3055

31-
public static function makeTaggable(CacheItemInterface $cacheItem) // @TODO naming?
56+
/**
57+
* @param CacheItemInterface $cacheItem
58+
* @return TaggableItemInterface
59+
*/
60+
public static function makeTaggable(CacheItemInterface $cacheItem)
3261
{
3362
return new self($cacheItem);
3463
}
3564

65+
/**
66+
* @return CacheItemInterface
67+
*/
3668
public function unwrap()
3769
{
3870
return $this->cacheItem;
@@ -73,6 +105,8 @@ public function isHit()
73105
*/
74106
public function set($value)
75107
{
108+
$this->initializeTags();
109+
76110
$this->cacheItem->set([
77111
'value' => $value,
78112
'tags' => $this->tags,
@@ -86,6 +120,7 @@ public function set($value)
86120
*/
87121
public function getTags()
88122
{
123+
$this->initializeTags();
89124
return $this->tags;
90125
}
91126

@@ -94,6 +129,7 @@ public function getTags()
94129
*/
95130
public function setTags(array $tags)
96131
{
132+
$this->initialized = true;
97133
$this->tags = $tags;
98134
$this->updateTags();
99135

@@ -105,6 +141,7 @@ public function setTags(array $tags)
105141
*/
106142
public function addTag($tag)
107143
{
144+
$this->initializeTags();
108145
$this->tags[] = $tag;
109146
$this->updateTags();
110147

@@ -136,4 +173,19 @@ private function updateTags()
136173
'tags' => $this->tags,
137174
]);
138175
}
176+
177+
private function initializeTags()
178+
{
179+
if (!$this->initialized) {
180+
if ($this->cacheItem->isHit()) {
181+
$rawItem = $this->cacheItem->get();
182+
183+
if (is_array($rawItem) && isset($rawItem['tags'])) {
184+
$this->tags = $rawItem['tags'];
185+
}
186+
}
187+
188+
$this->initialized = true;
189+
}
190+
}
139191
}

TaggablePSR6PoolAdapter.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
312
namespace Cache\Taggable;
413

514
use Cache\Taggable\TaggableItemInterface;
@@ -9,15 +18,44 @@
918
use Psr\Cache\CacheItemPoolInterface;
1019

1120
/**
12-
*
13-
*/
21+
* This adapter lets you make any PSR-6 cache pool taggable. If a pool is
22+
* already taggable, it is simply returned by makeTaggable. Tags are stored
23+
* either in the same cache pool, or a a separate pool, and both of these
24+
* appoaches come with different caveats.
25+
*
26+
* A general caveat is that using this adapter reserves any cache key starting
27+
* with '__tag.'.
28+
*
29+
* Using the same pool is precarious if your cache does LRU evictions of items
30+
* even if they do not expire (as in e.g. memcached). If so, the tag item may
31+
* be evicted without all of the tagged items having been evicted first,
32+
* causing items to lose their tags.
33+
*
34+
* In order to mitigate this issue, you may use a separate, more persistent
35+
* pool for your tag items. Do however note that if you are doing so, the
36+
* entire pool is reserved for tags, as this pool is cleared whenever the
37+
* main pool is cleared.
38+
*
39+
* @author Magnus Nordlander <magnus@fervo.se>
40+
*/
1441
class TaggablePSR6PoolAdapter implements TaggablePoolInterface
1542
{
1643
use TaggablePoolTrait;
1744

45+
/**
46+
* @type CacheItemPoolInterface
47+
*/
1848
private $cachePool;
49+
50+
/**
51+
* @type CacheItemPoolInterface
52+
*/
1953
private $tagStorePool;
2054

55+
/**
56+
* @param CacheItemPoolInterface $cachePool
57+
* @param CacheItemPoolInterface $tagStorePool
58+
*/
2159
private function __construct(CacheItemPoolInterface $cachePool, CacheItemPoolInterface $tagStorePool = null)
2260
{
2361
$this->cachePool = $cachePool;
@@ -28,7 +66,13 @@ private function __construct(CacheItemPoolInterface $cachePool, CacheItemPoolInt
2866
}
2967
}
3068

31-
public static function makeTaggable(CacheItemPoolInterface $cachePool, CacheItemPoolInterface $tagStorePool = null) // @TODO naming?
69+
/**
70+
* @param CacheItemPoolInterface $cachePool The pool to which to add tagging capabilities.
71+
* @param CacheItemPoolInterface|null $tagStorePool The pool to store tags in. If null is passed, the main pool is used.
72+
*
73+
* @return TaggablePoolInterface
74+
*/
75+
public static function makeTaggable(CacheItemPoolInterface $cachePool, CacheItemPoolInterface $tagStorePool = null)
3276
{
3377
if ($cachePool instanceOf TaggablePoolInterface && $tagStorePool === null) {
3478
return $cachePool;

0 commit comments

Comments
 (0)