1111
1212namespace Cache \Hierarchy ;
1313
14+ use Cache \Adapter \Common \CacheItem ;
1415use Cache \Adapter \Common \Exception \InvalidArgumentException ;
16+ use Cache \Taggable \TaggableItemInterface ;
1517use Cache \Taggable \TaggablePoolInterface ;
1618use Psr \Cache \CacheItemInterface ;
1719use Psr \Cache \CacheItemPoolInterface ;
@@ -42,11 +44,23 @@ public function __construct(CacheItemPoolInterface $cache)
4244 */
4345 public function getItem ($ key , array $ tags = [])
4446 {
45- if (!$ this ->isHierarchyKey ($ key )) {
46- return $ this ->cache ->getItem ($ key , $ tags );
47+ $ item = $ this ->cache ->getItem ($ key , $ tags );
48+ if (!$ this ->isHierarchyKey ($ key ) || !$ item ->isHit ()) {
49+ return $ item ;
4750 }
4851
49- // TODO: Implement getItem() method.
52+ if (!$ this ->validateParents ($ key , $ tags )) {
53+ return $ item ;
54+ }
55+
56+ // Invalid item
57+ if ($ item instanceof TaggableItemInterface) {
58+ $ key = $ item ->getTaggedKey ();
59+ } else {
60+ $ key = $ item ->getKey ();
61+ }
62+
63+ return new CacheItem ($ key );
5064 }
5165
5266 /**
@@ -67,10 +81,12 @@ public function getItems(array $keys = [], array $tags = [])
6781 */
6882 public function hasItem ($ key , array $ tags = [])
6983 {
70- if (!$ this ->isHierarchyKey ($ key )) {
71- return $ this ->cache ->hasItem ($ key , $ tags );
84+ $ hasItem = $ this ->cache ->hasItem ($ key , $ tags );
85+ if (!$ this ->isHierarchyKey ($ key ) || $ hasItem === false ) {
86+ return $ hasItem ;
7287 }
73- // TODO: Implement hasItem() method.
88+
89+ return $ this ->validateParents ($ key , $ tags );
7490 }
7591
7692 /**
@@ -86,30 +102,31 @@ public function clear(array $tags = [])
86102 */
87103 public function deleteItem ($ key , array $ tags = [])
88104 {
89- if (!$ this ->isHierarchyKey ($ key )) {
90- return $ this ->cache ->deleteItem ($ key , $ tags );
91- }
92- // TODO: Implement deleteItem() method.
105+ return $ this ->cache ->deleteItem ($ key , $ tags );
93106 }
94107
95108 /**
96109 * {@inheritdoc}
97110 */
98111 public function deleteItems (array $ keys , array $ tags = [])
99112 {
100- $ result = true ;
101- foreach ($ keys as $ key ) {
102- $ result = $ result && $ this ->deleteItem ($ key , $ tags );
103- }
104-
105- return $ result ;
113+ return $ this ->cache ->deleteItems ($ keys , $ tags );
106114 }
107115
108116 /**
109117 * {@inheritdoc}
110118 */
111119 public function save (CacheItemInterface $ item )
112120 {
121+ $ parts = $ this ->explodeKey ($ item ->getKey ());
122+ $ parentKey = '' ;
123+ foreach ($ parts as $ part ) {
124+ $ parentKey .= $ part ;
125+ $ parent = $ this ->cache ->getItem ($ parentKey );
126+ $ parent ->set (null );
127+ $ this ->cache ->save ($ parent );
128+ }
129+
113130 return $ this ->cache ->save ($ item );
114131 }
115132
@@ -143,4 +160,42 @@ private function isHierarchyKey($key)
143160
144161 return substr ($ key , 0 , 1 ) === self ::SEPARATOR ;
145162 }
163+
164+ /**
165+ * @param string $key
166+ * @param array $tags
167+ *
168+ * @return bool true if parents are valid
169+ */
170+ private function validateParents ($ key , array $ tags )
171+ {
172+ $ parts = $ this ->explodeKey ($ key );
173+ $ parentKey = '' ;
174+ foreach ($ parts as $ part ) {
175+ $ parentKey .= $ part ;
176+ if (!$ this ->cache ->hasItem ($ parentKey , $ tags )) {
177+ // Invalid item
178+ return false ;
179+ }
180+ }
181+
182+ return true ;
183+ }
184+
185+ /**
186+ * @param CacheItemInterface $item
187+ *
188+ * @return array
189+ */
190+ private function explodeKey ($ key )
191+ {
192+ $ parts = explode (self ::SEPARATOR , $ key );
193+
194+ unset($ parts [0 ]);
195+ foreach ($ parts as &$ part ) {
196+ $ part = self ::SEPARATOR .$ part ;
197+ }
198+
199+ return $ parts ;
200+ }
146201}
0 commit comments