@@ -173,52 +173,56 @@ public function flush() {
173173 return true ;
174174 }
175175
176- private function getMulti ($ keys , $ flags = null ) {
176+ private function getMulti ($ keys , $ flags = null , $ for_peek = false ) {
177177 $ request = new MemcacheGetRequest ();
178178 $ response = new MemcacheGetResponse ();
179179
180+ if ($ for_peek ) {
181+ $ request ->setForPeek (true );
182+ }
183+
180184 foreach ($ keys as $ key ) {
181185 $ request ->addKey ($ key );
182186 }
183187
184- ApiProxy::makeSyncCall ('memcache ' , 'Get ' , $ request , $ response );
188+ try {
189+ ApiProxy::makeSyncCall ('memcache ' , 'Get ' , $ request , $ response );
190+ } catch (Error $ e ) {
191+ return false ;
192+ }
185193
186194 $ return_value = array ();
187195 foreach ($ response ->getItemList () as $ item ) {
188196 try {
189- $ return_value [$ item ->getKey ()] = MemcacheUtils::deserializeValue (
190- $ item ->getValue (), $ item ->getFlags ());
197+ $ value = MemcacheUtils::deserializeValue ($ item ->getValue (), $ item ->getFlags ());
198+ if ($ for_peek ) {
199+ $ memcacheItemWithTimestamps = new MemcacheItemWithTimestamps (
200+ $ value ,
201+ $ item ->getTimestamps ()->getExpirationTimeSec (),
202+ $ item ->getTimestamps ()->getLastAccessTimeSec (),
203+ $ item ->getTimestamps ()->getDeleteLockTimeSec ());
204+ $ return_value [$ item ->getKey ()] = $ memcacheItemWithTimestamps ;
205+ } else {
206+ $ return_value [$ item ->getKey ()] = $ value ;
207+ }
191208 } catch (\UnexpectedValueException $ e ) {
192209 // Skip entries that cannot be deserialized.
193210 }
194211 }
195212 return $ return_value ;
196213 }
197214
198- /**
199- * Fetches previously stored data from the cache.
200- *
201- * @param string|string[] $keys The key associated with the value to fetch, or
202- * an array of keys if fetching multiple values.
203- *
204- * @param int $flags This parameter is present only for compatibility and is
205- * ignored. It should return the stored flag value.
206- *
207- * @return mixed On success, the string associated with the key, or an array
208- * of key-value pairs when $keys is an array. On failure, false
209- * is returned.
210- */
211- public function get ($ keys , $ flags = null ) {
215+ private function getInternal ($ keys , $ flags , $ for_peek ) {
212216 if (is_array ($ keys )) {
213- $ return_value = $ this ->getMulti ($ keys , $ flags );
217+ $ return_value = $ this ->getMulti ($ keys , $ flags, $ for_peek );
214218 if (empty ($ return_value )) {
215219 return false ;
216220 } else {
217221 return $ return_value ;
218222 }
219223 } else {
220224 try {
221- $ return_value = $ this ->getMulti (array ($ keys ), array ($ flags ));
225+ $ return_value = $ this ->getMulti (array ($ keys ), array ($ flags ), $ for_peek );
222226 } catch (Error $ e ) {
223227 return false ;
224228 }
@@ -230,6 +234,23 @@ public function get($keys, $flags = null) {
230234 }
231235 }
232236
237+ /**
238+ * Fetches previously stored data from the cache.
239+ *
240+ * @param string|string[] $keys The key associated with the value to fetch, or
241+ * an array of keys if fetching multiple values.
242+ *
243+ * @param int $flags This parameter is present only for compatibility and is
244+ * ignored. It should return the stored flag value.
245+ *
246+ * @return mixed On success, the string associated with the key, or an array
247+ * of key-value pairs when $keys is an array. On failure, false
248+ * is returned.
249+ */
250+ public function get ($ keys , $ flags = null ) {
251+ return $ this ->getInternal ($ keys , $ flags , false /* $for_peek */ );
252+ }
253+
233254 // Not implemented:
234255 // getExtendedStats
235256 // getServerStatus
@@ -296,6 +317,23 @@ public function pconnect($host, $port = null, $timeout = 1) {
296317 return true ;
297318 }
298319
320+ /**
321+ * Gets an item from memcache along with timestamp metadata.
322+ *
323+ * @param string|string[] $keys The key associated with the value to fetch, or
324+ * an array of keys if fetching multiple values.
325+ *
326+ * @param int $flags This parameter is present only for compatibility and is
327+ * ignored. It should return the stored flag value.
328+ *
329+ * @return mixed On success, the MemcacheItemWithTimestamps associated with
330+ * the key, or an array of key-MemcacheItemWithTimestamp pairs
331+ * when $keys is an array. On failure, false is returned.
332+ */
333+ public function peek ($ keys , $ flags = null ) {
334+ return $ this ->getInternal ($ keys , $ flags , true /* $for_peek */ );
335+ }
336+
299337 /**
300338 * Replaces an existing item in the cache. Will fail if the key is not already
301339 * present in the cache.
0 commit comments