Example #1
0
 /**
  * @inheritdoc
  */
 public function getMany(array $keys, array $options = [])
 {
     $cached = $this->cache->getMany($keys, $options);
     return array_map(function ($cached) {
         if (is_array($cached) && count($cached) === 2) {
             list($data, $exp) = $cached;
             return new OffloadResult($data, true, $exp);
         } else {
             return OffloadResult::miss();
         }
     }, $cached);
 }
Example #2
0
 /**
  * Get the given key and refresh it if not found in cache or is stale.
  *
  * @param string   $key        The key to get/refresh.
  * @param callable $repopulate A callable that returns fresh data for the key.
  * @param array    $options    Offload options.
  *
  * @return OffloadResult The offload result.
  */
 protected function refresh($key, callable $repopulate, $options = [])
 {
     // Check cache as long as there is a cache time set.
     if ($options[self::OPTION_TTL_STALE] || $options[self::OPTION_TTL_FRESH]) {
         $cache_options = empty($options[self::OPTION_CACHE_OPTIONS]) ? [] : $options[self::OPTION_CACHE_OPTIONS];
         $result = $this->cache->get($key, $cache_options);
     } else {
         $result = OffloadResult::miss();
     }
     $from_cache = $result->isFromCache();
     $stale = $result->isStale();
     if (!$from_cache || $stale && !$options[self::OPTION_BACKGROUND]) {
         // If there is no data in cache or the data is stale and background fetch is turned off,
         // run the repopulate immediately and cache the results.
         $data = $this->run($key, $repopulate, $options)->wait();
         $result = new OffloadResult($data, false, 0);
     } elseif ($stale) {
         // If there is data in cache and the data is stale, queue a background repopulate.
         $this->queue($key, $repopulate, $options);
     }
     return $result;
 }