/** * {@inheritdoc} */ protected function doSave(array $values, $lifetime) { $ok = true; $data = array($lifetime ? time() + $lifetime : PHP_INT_MAX, ''); $allowCompile = 'cli' !== PHP_SAPI || ini_get('opcache.enable_cli'); foreach ($values as $key => $value) { if (null === $value || is_object($value)) { $value = serialize($value); } elseif (is_array($value)) { $serialized = serialize($value); $unserialized = parent::unserialize($serialized); // Store arrays serialized if they contain any objects or references if ($unserialized !== $value || false !== strpos($serialized, ';R:') && preg_match('/;R:[1-9]/', $serialized)) { $value = $serialized; } } elseif (is_string($value)) { // Serialize strings if they could be confused with serialized objects or arrays if ('N;' === $value || isset($value[2]) && ':' === $value[1]) { $value = serialize($value); } } elseif (!is_scalar($value)) { throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable %s value.', $key, gettype($value))); } $data[1] = $value; $file = $this->getFile($key, true); $ok = $this->write($file, '<?php return ' . var_export($data, true) . ';') && $ok; if ($allowCompile) { @opcache_compile_file($file); } } return $ok; }
/** * {@inheritdoc} */ protected function doFetch(array $ids) { if ($ids) { $values = $this->redis->mGet($ids); $index = 0; foreach ($ids as $id) { if ($value = $values[$index++]) { (yield $id => parent::unserialize($value)); } } } }
/** * {@inheritdoc} */ protected function doFetch(array $ids) { $now = time(); $expired = array(); $sql = str_pad('', (count($ids) << 1) - 1, '?,'); $sql = "SELECT {$this->idCol}, CASE WHEN {$this->lifetimeCol} IS NULL OR {$this->lifetimeCol} + {$this->timeCol} > ? THEN {$this->dataCol} ELSE NULL END FROM {$this->table} WHERE {$this->idCol} IN ({$sql})"; $stmt = $this->getConnection()->prepare($sql); $stmt->bindValue($i = 1, $now, \PDO::PARAM_INT); foreach ($ids as $id) { $stmt->bindValue(++$i, $id); } $stmt->execute(); while ($row = $stmt->fetch(\PDO::FETCH_NUM)) { if (null === $row[1]) { $expired[] = $row[0]; } else { (yield $row[0] => parent::unserialize(is_resource($row[1]) ? stream_get_contents($row[1]) : $row[1])); } } if ($expired) { $sql = str_pad('', (count($expired) << 1) - 1, '?,'); $sql = "DELETE FROM {$this->table} WHERE {$this->lifetimeCol} + {$this->timeCol} <= ? AND {$this->idCol} IN ({$sql})"; $stmt = $this->getConnection()->prepare($sql); $stmt->bindValue($i = 1, $now, \PDO::PARAM_INT); foreach ($expired as $id) { $stmt->bindValue(++$i, $id); } $stmt->execute($expired); } }