@@ -113,6 +113,17 @@ public function getMultiple($keys, $default = null)
113
113
throw new InvalidArgumentException ($ e ->getMessage (), $ e ->getCode (), $ e );
114
114
}
115
115
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
+ {
116
127
foreach ($ items as $ key => $ item ) {
117
128
/** @type $item CacheItemInterface */
118
129
if (!$ item ->isHit ()) {
@@ -132,14 +143,29 @@ public function setMultiple($values, $ttl = null)
132
143
if (!$ values instanceof \Traversable) {
133
144
throw new InvalidArgumentException ('$values is neither an array nor Traversable ' );
134
145
}
146
+ }
135
147
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 ;
139
165
}
140
166
141
167
try {
142
- $ items = $ this ->cacheItemPool ->getItems (array_keys ( $ values ) );
168
+ $ items = $ this ->cacheItemPool ->getItems ($ keys );
143
169
} catch (CacheInvalidArgumentException $ e ) {
144
170
throw new InvalidArgumentException ($ e ->getMessage (), $ e ->getCode (), $ e );
145
171
}
@@ -148,7 +174,7 @@ public function setMultiple($values, $ttl = null)
148
174
149
175
foreach ($ items as $ key => $ item ) {
150
176
/* @var $item CacheItemInterface */
151
- $ item ->set ($ values [$ key ]);
177
+ $ item ->set ($ arrayValues [$ key ]);
152
178
153
179
try {
154
180
$ item ->expiresAfter ($ ttl );
0 commit comments