Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f47fbba

Browse filesBrowse files
[Cache] Add FilesystemAdapter
1 parent 80f3410 commit f47fbba
Copy full SHA for f47fbba

File tree

2 files changed

+174
-0
lines changed
Filter options

2 files changed

+174
-0
lines changed
+144Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Adapter;
13+
14+
use Symfony\Component\Cache\Exception\InvalidArgumentException;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class FilesystemAdapter extends AbstractAdapter
20+
{
21+
private $directory;
22+
23+
public function __construct($directory, $defaultLifetime = null)
24+
{
25+
parent::__construct('', $defaultLifetime);
26+
27+
$dir = rtrim($directory, '/'.DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
28+
29+
if (!file_exists($dir)) {
30+
@mkdir($dir, 0777, true);
31+
}
32+
if (false === $dir = realpath($dir)) {
33+
throw new InvalidArgumentException(sprintf('Cache directory does not exist (%s)', $directory));
34+
}
35+
if (!is_writable($dir .= DIRECTORY_SEPARATOR)) {
36+
throw new InvalidArgumentException(sprintf('Cache directory is not writable (%s)', $directory));
37+
}
38+
// On Windows the whole path is limited to 258 chars
39+
if ('\\' === DIRECTORY_SEPARATOR && strlen($dir) > 190) {
40+
throw new InvalidArgumentException(sprintf('Cache directory too long (%s)', $directory));
41+
}
42+
43+
$this->directory = $dir;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
protected function doFetch(array $ids)
50+
{
51+
$values = array();
52+
$now = time();
53+
54+
foreach ($ids as $id) {
55+
$file = $this->getFile($id);
56+
if (false !== $h = @fopen($file, 'rb')) {
57+
flock($h, LOCK_SH);
58+
if ($now >= (int) fgets($h)) {
59+
flock($h, LOCK_UN);
60+
fclose($h);
61+
@unlink($file);
62+
} else {
63+
$value = stream_get_contents($h);
64+
flock($h, LOCK_UN);
65+
fclose($h);
66+
$values[$id] = unserialize($value);
67+
}
68+
}
69+
}
70+
71+
return $values;
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
protected function doHave($id)
78+
{
79+
$file = $this->getFile($id);
80+
81+
return @filemtime($file) > time() || $this->doFetch(array($id));
82+
}
83+
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
protected function doClear($namespace)
88+
{
89+
$ok = true;
90+
91+
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS)) as $file) {
92+
$ok = ($file->isDir() || @unlink($file) || !file_exists($file)) && $ok;
93+
}
94+
95+
return $ok;
96+
}
97+
98+
/**
99+
* {@inheritdoc}
100+
*/
101+
protected function doDelete(array $ids)
102+
{
103+
$ok = true;
104+
105+
foreach ($ids as $id) {
106+
$file = $this->getFile($id);
107+
$ok = (!file_exists($file) || @unlink($file) || !file_exists($file)) && $ok;
108+
}
109+
110+
return $ok;
111+
}
112+
113+
/**
114+
* {@inheritdoc}
115+
*/
116+
protected function doSave(array $values, $lifetime)
117+
{
118+
$ok = true;
119+
$expiresAt = $lifetime ? time() + $lifetime : PHP_INT_MAX;
120+
121+
foreach ($values as $id => $value) {
122+
$file = $this->getFile($id);
123+
$dir = dirname($file);
124+
if (!file_exists($dir)) {
125+
@mkdir($dir, 0777, true);
126+
}
127+
$value = $expiresAt."\n".serialize($value);
128+
if (false !== @file_put_contents($file, $value, LOCK_EX)) {
129+
@touch($file, $expiresAt);
130+
} else {
131+
$ok = false;
132+
}
133+
}
134+
135+
return $ok;
136+
}
137+
138+
private function getFile($id)
139+
{
140+
$hash = hash('sha256', $id);
141+
142+
return $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR.$hash;
143+
}
144+
}
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use Cache\IntegrationTests\CachePoolTest;
15+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
16+
17+
/**
18+
* @group time-sensitive
19+
*/
20+
class FilesystemAdapterTest extends CachePoolTest
21+
{
22+
public function createCachePool()
23+
{
24+
if (defined('HHVM_VERSION')) {
25+
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
26+
}
27+
28+
return new FilesystemAdapter(sys_get_temp_dir().DIRECTORY_SEPARATOR.'sf-cache');
29+
}
30+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.