コード例 #1
0
 /**
  * Reads an array of cache objects from storage.
  *
  * @param  mixed	array of Ids of the cache entry to read
  * @return array of array	includes key, data, expires
  */
 protected function readCacheArray($keys, $writeLock = false)
 {
     if (empty($keys)) {
         return array();
     }
     if (count($keys) == 1) {
         //faster to just call the method for single key. Saves overhead
         $key = array_pop($keys);
         return array($key => $this->readCache($key));
     }
     //There's some bookkeeping here. We need to map keys from the app with
     //prefixed keys to get data, and then map back.
     $memKeys = array();
     foreach ($keys as $key) {
         $memKeys[] = $this->prefix . $key;
     }
     $cached = $this->memcached->get($memKeys);
     //if we didn't get anything, we're done.
     if (empty($cached)) {
         return array();
     }
     //now map the result back to the original keys
     $found = array();
     foreach ($cached as $cacheRecord) {
         $found[$cacheRecord['key']] = $cacheRecord;
     }
     $allEvents = array();
     foreach ($found as $key => $record) {
         try {
             if (isset($record['data'])) {
                 //we need to check events. Let's get all the events so we can
                 //make one call.
                 if (!empty($record['events'])) {
                     foreach ((array) $record['events'] as $event) {
                         // We have to store the retrieved record's events in memory so that calling event() will work correctly even if
                         // we read from cache but did not write to cache within the same session. VBV-12944
                         if (!isset($this->events[$event])) {
                             $this->events[$event] = array();
                         }
                         $this->events[$event][$key] = $key;
                         // this key is the original, non-prefixed key. See "now map the result back to the original keys" above.
                         $allEvents[] = $event;
                     }
                 }
             } else {
                 unset($found[$key]);
             }
         } catch (exception $e) {
             //If we got here, something was improperly serialized
             unset($found[$key]);
         }
     }
     //Possible we've eliminated everything, or there are no events. If so we're done.
     if (empty($found) or empty($allEvents)) {
         return $found;
     }
     $eventTimes = $this->getEventTimes($allEvents);
     foreach ($found as $key => $record) {
         //now check the events.
         if (!empty($record['events']) and is_array($record['events'])) {
             foreach ($record['events'] as $event) {
                 if (!isset($eventTimes[$event]) or $eventTimes[$event] >= $record['created']) {
                     unset($found[$key]);
                     $this->purgeCache($key);
                     continue;
                 }
             }
         }
     }
     return $found;
 }