Example #1
0
 /**
  * Write data for key into cache
  *
  * @param string $key Identifier for the data
  * @param mixed $data Data to be cached
  * @param integer $duration How long to cache the data, in seconds
  * @return boolean True if the data was successfully cached, false on failure
  */
 public function write($key, $data, $duration)
 {
     if ($data === '' || !$this->_init) {
         return false;
     }
     if ($this->_setKey($key, true) === false) {
         return false;
     }
     $lineBreak = "\n";
     if ($this->settings['isWindows']) {
         $lineBreak = "\r\n";
     }
     if (!empty($this->settings['serialize'])) {
         if ($this->settings['isWindows']) {
             $data = str_replace('\\', '\\\\\\\\', serialize($data));
         } else {
             $data = serialize($data);
         }
     }
     $expires = time() + $duration;
     $contents = $expires . $lineBreak . $data . $lineBreak;
     if ($this->settings['lock']) {
         $this->_File->flock(LOCK_EX);
     }
     $this->_File->rewind();
     $success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush();
     if ($this->settings['lock']) {
         $this->_File->flock(LOCK_UN);
     }
     return $success;
 }
Example #2
0
 /**
  * File that exists in memory only
  *
  * @param string $contents
  * @return File
  */
 protected function fakeFile($contents)
 {
     $file = new File('php://memory', 'a+');
     $file->fwrite($contents);
     $file->rewind();
     return $file;
 }