Esempio n. 1
0
 private function generateItems(array $keys, $now)
 {
     $f = $this->createCacheItem;
     foreach ($keys as $i => $key) {
         try {
             if (!($isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= $now || !$this->deleteItem($key)))) {
                 $this->values[$key] = $value = null;
             } elseif (!$this->storeSerialized) {
                 $value = $this->values[$key];
             } elseif ('b:0;' === ($value = $this->values[$key])) {
                 $value = false;
             } elseif (false === ($value = unserialize($value))) {
                 $this->values[$key] = $value = null;
                 $isHit = false;
             }
         } catch (\Exception $e) {
             CacheItem::log($this->logger, 'Failed to unserialize key "{key}"', array('key' => $key, 'exception' => $e));
             $this->values[$key] = $value = null;
             $isHit = false;
         }
         unset($keys[$i]);
         (yield $key => $f($key, $value, $isHit));
     }
     foreach ($keys as $key) {
         (yield $key => $f($key, null, false));
     }
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function commit()
 {
     $ok = true;
     $byLifetime = $this->mergeByLifetime;
     $byLifetime = $byLifetime($this->deferred, $this->namespace);
     $retry = $this->deferred = array();
     foreach ($byLifetime as $lifetime => $values) {
         try {
             $e = $this->doSave($values, $lifetime);
         } catch (\Exception $e) {
         }
         if (true === $e || array() === $e) {
             continue;
         }
         if (is_array($e) || 1 === count($values)) {
             foreach (is_array($e) ? $e : array_keys($values) as $id) {
                 $ok = false;
                 $v = $values[$id];
                 $type = is_object($v) ? get_class($v) : gettype($v);
                 CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => substr($id, strlen($this->namespace)), 'type' => $type, 'exception' => $e instanceof \Exception ? $e : null));
             }
         } else {
             foreach ($values as $id => $v) {
                 $retry[$lifetime][] = $id;
             }
         }
     }
     // When bulk-save failed, retry each item individually
     foreach ($retry as $lifetime => $ids) {
         foreach ($ids as $id) {
             try {
                 $v = $byLifetime[$lifetime][$id];
                 $e = $this->doSave(array($id => $v), $lifetime);
             } catch (\Exception $e) {
             }
             if (true === $e || array() === $e) {
                 continue;
             }
             $ok = false;
             $type = is_object($v) ? get_class($v) : gettype($v);
             CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => substr($id, strlen($this->namespace)), 'type' => $type, 'exception' => $e instanceof \Exception ? $e : null));
         }
     }
     $this->deferred = array();
     return $ok;
 }
Esempio n. 3
0
 private function generateItems($items, &$keys)
 {
     $f = $this->createCacheItem;
     try {
         foreach ($items as $id => $value) {
             $key = $keys[$id];
             unset($keys[$id]);
             (yield $key => $f($key, $value, true));
         }
     } catch (\Exception $e) {
         CacheItem::log($this->logger, 'Failed to fetch requested items', array('keys' => array_values($keys), 'exception' => $e));
     }
     foreach ($keys as $key) {
         (yield $key => $f($key, null, false));
     }
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function save(CacheItemInterface $item)
 {
     if (!$item instanceof CacheItem) {
         return false;
     }
     $item = (array) $item;
     $key = $item[CacheItem::CAST_PREFIX . 'key'];
     $value = $item[CacheItem::CAST_PREFIX . 'value'];
     $expiry = $item[CacheItem::CAST_PREFIX . 'expiry'];
     if (null !== $expiry && $expiry <= time()) {
         $this->deleteItem($key);
         return true;
     }
     if ($this->storeSerialized) {
         try {
             $value = serialize($value);
         } catch (\Exception $e) {
             $type = is_object($value) ? get_class($value) : gettype($value);
             CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
             return false;
         }
     }
     $this->values[$key] = $value;
     $this->expiries[$key] = null !== $expiry ? $expiry : PHP_INT_MAX;
     return true;
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function commit()
 {
     $f = $this->mergeByLifetime;
     $ko = array();
     foreach ($f($this->deferred, $this->namespace) as $lifetime => $values) {
         try {
             if (true === ($ok = $this->doSave($values, $lifetime))) {
                 continue;
             }
         } catch (\Exception $e) {
             $ok = false;
         }
         if (false === $ok) {
             $ok = array_keys($values);
         }
         foreach ($ok as $id) {
             $ko[$lifetime][] = array($id => $values[$id]);
         }
     }
     $this->deferred = array();
     $ok = true;
     // When bulk-save failed, retry each item individually
     foreach ($ko as $lifetime => $values) {
         foreach ($values as $v) {
             try {
                 $e = $this->doSave($v, $lifetime);
             } catch (\Exception $e) {
             }
             if (true !== $e && array() !== $e) {
                 $ok = false;
                 foreach ($v as $key => $value) {
                     $type = is_object($value) ? get_class($value) : gettype($value);
                     CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e instanceof \Exception ? $e : null));
                 }
             }
         }
     }
     return $ok;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function save(CacheItemInterface $item)
 {
     if (!$item instanceof CacheItem) {
         return false;
     }
     $item = (array) $item;
     $key = $item[CacheItem::CAST_PREFIX . 'key'];
     $value = $item[CacheItem::CAST_PREFIX . 'value'];
     $lifetime = $item[CacheItem::CAST_PREFIX . 'lifetime'];
     if (0 > $lifetime) {
         return true;
     }
     if (is_object($value)) {
         try {
             $value = clone $value;
         } catch (\Exception $e) {
             $type = is_object($value) ? get_class($value) : gettype($value);
             CacheItem::log($this->logger, 'Failed to clone key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
             return false;
         }
     }
     $this->values[$key] = $value;
     $this->expiries[$key] = $lifetime ? $lifetime + time() : PHP_INT_MAX;
     return true;
 }