Пример #1
0
 /**
  * Load Data from Meta File
  *
  * @param string|optional $key
  *   Optional Metadata Entry
  *
  * @return boolean|string|array|null
  *
  *   FALSE if unable to read Metadata File
  *
  *   If <strong>$key</strong> argument IS null the whole Metadata
  *   Content will be returned
  *
  *   If <strong>$key</strong> is NOT null and given key is present in
  *   Metadata Content, for simple Information, a string will returned.
  *   For complex Information, an array will.
  *
  *   If given key is not present in Metadata Content OR an Exception is thrown,
  *   NULL will returned
  *
  * @throws Next\Cache\CacheException
  *   Fail to read Meta Data
  *
  * @throws Next\Cache\CacheException
  *   Meta Data is Corrupted (not an array)
  */
 public function load($key = NULL)
 {
     try {
         // Setting Up Reader Object
         $reader = new Reader(new Socket(sprintf('%s/%s', $this->backendOptions->meta->path, $this->backendOptions->meta->file), 'r'));
         // Reading Cached Content
         $data = $reader->readAll();
         // Closing Stream
         $reader->getStream()->close();
         if (!$data) {
             throw CacheException::noMetadata();
         }
         // Decompressing Data
         if (function_exists('gzuncompress')) {
             $data = gzuncompress($data);
         }
         // Unserializing Data
         $data = unserialize($data);
         if (!is_array($data)) {
             throw CacheException::corruptedMetadata();
         }
         // What should we return?
         if (!is_null($key)) {
             $key = md5($key);
             return array_key_exists($key, $data) ? $data[$key] : NULL;
         } else {
             return $data;
         }
     } catch (ReaderException $e) {
         /**
          * @internal
          * As we are trying to read a file, if we got an Exception
          * means the file doesn't exists or it's not readable
          *
          * So the Meta Data File must be regenerated
          */
         return FALSE;
     }
 }
Пример #2
0
 /**
  * Load Cached Data
  *
  * @param string $key
  *   Cache Key
  *
  * @param boolean|optional $keepSerialized
  *   Flag to condition if Cache Data will keep serialized or not
  *
  * @return mixed|boolean
  *
  *   FALSE if:
  *
  *   <ul>
  *
  *       <li>There is no Cached entry for given key</li>
  *
  *       <li>We were unable to read Cached Data</li>
  *
  *       <li>
  *
  *           Cache Integrity Validation is enabled and read
  *           Cached Data has a different hash
  *
  *       </li>
  *
  *   </ul>
  *
  *   <p>
  *       Otherwise, if <strong>$keepSerialized</strong> is set as TRUE
  *       the Cached Data will be returned as string, just like it
  *       was stored.
  *   </p>
  *
  *   <p>
  *       If this argument is set as FALSE, we'll unserialize it to
  *       the original state
  *   </p>
  */
 public function load($key, $keepSerialized = TRUE)
 {
     if (!$this->test($key)) {
         return FALSE;
     }
     try {
         // Setting Up Reader Object
         $reader = new Reader(new Socket($this->buildFilePath($key), 'r'));
         // Reading Cached Content
         $data = $reader->readAll();
         // Closing Stream
         $reader->getStream()->close();
         if (empty($data)) {
             return NULL;
         }
         $meta = $this->meta->load($key);
         if ($meta !== FALSE) {
             // Should we check if Cache's Hash is valid?
             if ($this->options->testValidity && $meta['hash'] !== FALSE) {
                 // Comparing the Hashes
                 if ($this->hash($data, $meta['cacheControl']) !== $meta['hash']) {
                     // We have a Invalid Cache, should we delete it?
                     if ($this->options->removeCorrupted !== FALSE) {
                         try {
                             $this->remove($key);
                         } catch (CacheException $e) {
                         }
                     }
                     return FALSE;
                 }
             }
             // Should we Decompress the Cached Content?
             if ($meta['compressed']) {
                 if (function_exists('gzuncompress')) {
                     $data = gzuncompress($data);
                 }
             }
             return (bool) $keepSerialized !== FALSE ? $data : unserialize($data);
         }
         return $data;
     } catch (ReaderException $e) {
         /**
          * @internal
          *
          * As we are trying to read a file, if we got an Exception
          * means the file doesn't exists or it's not readable
          *
          * So the Cache Data must be regenerated
          */
         return FALSE;
     }
 }