4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Cache\Traits;
14 use Symfony\Component\Cache\Exception\CacheException;
17 * @author Nicolas Grekas <p@tchwork.com>
18 * @author Rob Frawley 2nd <rmf@src.run>
24 use FilesystemCommonTrait;
29 public function prune()
34 foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
35 if (!$h = @fopen($file, 'rb')) {
39 if (($expiresAt = (int) fgets($h)) && $time >= $expiresAt) {
41 $pruned = @unlink($file) && !file_exists($file) && $pruned;
53 protected function doFetch(array $ids)
58 foreach ($ids as $id) {
59 $file = $this->getFile($id);
60 if (!file_exists($file) || !$h = @fopen($file, 'rb')) {
63 if (($expiresAt = (int) fgets($h)) && $now >= $expiresAt) {
67 $i = rawurldecode(rtrim(fgets($h)));
68 $value = stream_get_contents($h);
71 $values[$id] = parent::unserialize($value);
82 protected function doHave($id)
84 $file = $this->getFile($id);
86 return file_exists($file) && (@filemtime($file) > time() || $this->doFetch([$id]));
92 protected function doSave(array $values, $lifetime)
95 $expiresAt = $lifetime ? (time() + $lifetime) : 0;
97 foreach ($values as $id => $value) {
98 $ok = $this->write($this->getFile($id, true), $expiresAt."\n".rawurlencode($id)."\n".serialize($value), $expiresAt) && $ok;
101 if (!$ok && !is_writable($this->directory)) {
102 throw new CacheException(sprintf('Cache directory is not writable (%s).', $this->directory));