/** * Add new Data into Cache * * @param string $key * Cache Key * * @param mixed $value * Data to Cache * * @param integer|optional $ttl * Optional Lifetime * * @param boolean|optional $isTouching * Flag to condition when we're touching the Cache File in order to give * it an extra lifetime * * @return boolean * TRUE on success and FALSE otherwise */ public function add($key, $value, $ttl = NULL, $isTouching = FALSE) { // Serializing the Content $value = serialize($value); // Should we compress Data? if ($this->options->compression->enabled) { if (function_exists('gzcompress')) { $value = gzcompress($value, $this->options->compression->level); } } try { // Setting Up Writer Object $writer = new Writer(new Socket($this->buildFilePath($key), 'w')); // Building and Writing Meta Data $this->meta->write($key, $this->metadata($key, $value, (int) $ttl, $isTouching)); // Writing Data $bytes = (bool) $writer->write($value); // Closing File Stream $writer->getStream()->close(); return $bytes > 0; } catch (WriterException $e) { /** * As we are trying to create a Cache File, if we got an Exception * means the file exists but it's not writable or it doesn't exists * and its parent diectory is not writable * * So the Cache Data could not be created and, when trying to load * it, the cache will be regenerated anyway */ return FALSE; } }
/** * Writing Metadata Wrapper * * @param array $data * Data to Write * * @return boolean * TRUE if Metadata was successfully written and FALSE otherwise */ private function writeMeta(array $data) { // Serializing the Content (required by compression below) $data = serialize($data); // Can we compress the Meta Data Contents? if (function_exists('gzcompress')) { $data = gzcompress($data, 9); } try { // Setting Up Writer Object $writer = new Writer(new Socket(sprintf('%s/%s', $this->backendOptions->meta->path, $this->backendOptions->meta->file), 'w')); // Writing Data $result = (bool) $writer->write($data); // Closing File Stream $writer->getStream()->close(); return $result; } catch (WriterException $e) { /** * @internal * As we are trying to create/update the Meta Data File, if we got an Exception * means the file exists but it's not writable or it doesn't exists * and its parent directory is not writable * * So the Meta Data File could not be created/updated and, when trying to load * it, the file will be regenerated anyway */ return FALSE; } }