@@ -113,6 +113,17 @@ public function getMultiple($keys, $default = null)
113113 throw new InvalidArgumentException ($ e ->getMessage (), $ e ->getCode (), $ e );
114114 }
115115
116+ return $ this ->generateValues ($ default , $ items );
117+ }
118+
119+ /**
120+ * @param $default
121+ * @param $items
122+ *
123+ * @return \Generator
124+ */
125+ private function generateValues ($ default , $ items )
126+ {
116127 foreach ($ items as $ key => $ item ) {
117128 /** @type $item CacheItemInterface */
118129 if (!$ item ->isHit ()) {
@@ -132,14 +143,29 @@ public function setMultiple($values, $ttl = null)
132143 if (!$ values instanceof \Traversable) {
133144 throw new InvalidArgumentException ('$values is neither an array nor Traversable ' );
134145 }
146+ }
135147
136- // Since we need to throw an exception if *any* key is invalid, it doesn't
137- // make sense to wrap iterators or something like that.
138- $ values = iterator_to_array ($ values , false );
148+ $ keys = [];
149+ $ arrayValues = [];
150+ foreach ($ values as $ key => $ value ) {
151+ if (is_int ($ key )) {
152+ $ key = (string ) $ key ;
153+ }
154+
155+ if (!is_string ($ key )) {
156+ throw new InvalidArgumentException (sprintf ('Cache key must be string, "%s" given ' , gettype ($ key )));
157+ }
158+
159+ if (preg_match ('|[\{\}\(\)/ \\\@\:]| ' , $ key )) {
160+ throw new InvalidArgumentException (sprintf ('Invalid key: "%s". The key contains one or more characters reserved for future extension: {}()/\@: ' , $ key ));
161+ }
162+
163+ $ keys [] = $ key ;
164+ $ arrayValues [$ key ] = $ value ;
139165 }
140166
141167 try {
142- $ items = $ this ->cacheItemPool ->getItems (array_keys ( $ values ) );
168+ $ items = $ this ->cacheItemPool ->getItems ($ keys );
143169 } catch (CacheInvalidArgumentException $ e ) {
144170 throw new InvalidArgumentException ($ e ->getMessage (), $ e ->getCode (), $ e );
145171 }
@@ -148,7 +174,7 @@ public function setMultiple($values, $ttl = null)
148174
149175 foreach ($ items as $ key => $ item ) {
150176 /* @var $item CacheItemInterface */
151- $ item ->set ($ values [$ key ]);
177+ $ item ->set ($ arrayValues [$ key ]);
152178
153179 try {
154180 $ item ->expiresAfter ($ ttl );
0 commit comments