Skip to content

Commit 6d2fec9

Browse files
committed
Add Cache File mode
1 parent 45a7972 commit 6d2fec9

File tree

4 files changed

+92
-26
lines changed

4 files changed

+92
-26
lines changed

app/Config/Cache.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Cache extends BaseConfig
4646
* system.
4747
*
4848
* @var string
49+
*
50+
* @deprecated Use the driver-specific variant under $file
4951
*/
5052
public $storePath = WRITEPATH . 'cache/';
5153

@@ -80,6 +82,20 @@ class Cache extends BaseConfig
8082
*/
8183
public $prefix = '';
8284

85+
/**
86+
* --------------------------------------------------------------------------
87+
* File settings
88+
* --------------------------------------------------------------------------
89+
* Your file storage preferences can be specified below, if you are using
90+
* the File driver.
91+
*
92+
* @var array<string, string|int|null>
93+
*/
94+
public $file = [
95+
'storePath' => WRITEPATH . 'cache/',
96+
'mode' => 0640,
97+
];
98+
8399
/**
84100
* -------------------------------------------------------------------------
85101
* Memcached settings

system/Cache/Handlers/FileHandler.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ class FileHandler implements CacheInterface
3434
*/
3535
protected $path;
3636

37+
/**
38+
* Mode for the stored files.
39+
* Must be chmod-safe (octal).
40+
*
41+
* @var int
42+
*
43+
* @see https://www.php.net/manual/en/function.chmod.php
44+
*/
45+
protected $mode;
46+
3747
//--------------------------------------------------------------------
3848

3949
/**
@@ -44,14 +54,23 @@ class FileHandler implements CacheInterface
4454
*/
4555
public function __construct(Cache $config)
4656
{
47-
$path = ! empty($config->storePath) ? $config->storePath : WRITEPATH . 'cache';
48-
if (! is_really_writable($path))
57+
if (isset($config->file))
58+
{
59+
$this->path = $config->file['storePath'] ?? WRITEPATH . 'cache';
60+
$this->mode = $config->file['mode'] ?? 0640;
61+
}
62+
else
63+
{
64+
$this->path = ! empty($config->storePath) ? $config->storePath : WRITEPATH . 'cache';
65+
}
66+
67+
if (! is_really_writable($this->path))
4968
{
50-
throw CacheException::forUnableToWrite($path);
69+
throw CacheException::forUnableToWrite($this->path);
5170
}
5271

5372
$this->prefix = $config->prefix ?: '';
54-
$this->path = rtrim($path, '/') . '/';
73+
$this->path = rtrim($this->path, '/') . '/';
5574
}
5675

5776
//--------------------------------------------------------------------
@@ -105,7 +124,7 @@ public function save(string $key, $value, int $ttl = 60)
105124

106125
if ($this->writeFile($this->path . $key, serialize($contents)))
107126
{
108-
chmod($this->path . $key, 0640);
127+
chmod($this->path . $key, $this->mode);
109128

110129
return true;
111130
}

tests/system/Cache/Handlers/FileHandlerTest.php

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ protected function setUp(): void
2626
{
2727
parent::setUp();
2828

29-
//Initialize path
30-
$this->config = new \Config\Cache();
31-
$this->config->storePath .= self::$directory;
29+
// Initialize path
30+
$this->config = new \Config\Cache();
31+
$this->config->file['storePath'] .= self::$directory;
3232

33-
if (! is_dir($this->config->storePath))
33+
if (! is_dir($this->config->file['storePath']))
3434
{
35-
mkdir($this->config->storePath, 0777, true);
35+
mkdir($this->config->file['storePath'], 0777, true);
3636
}
3737

3838
$this->fileHandler = new FileHandler($this->config);
@@ -41,20 +41,20 @@ protected function setUp(): void
4141

4242
public function tearDown(): void
4343
{
44-
if (is_dir($this->config->storePath))
44+
if (is_dir($this->config->file['storePath']))
4545
{
46-
chmod($this->config->storePath, 0777);
46+
chmod($this->config->file['storePath'], 0777);
4747

4848
foreach (self::getKeyArray() as $key)
4949
{
50-
if (is_file($this->config->storePath . DIRECTORY_SEPARATOR . $key))
50+
if (is_file($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key))
5151
{
52-
chmod($this->config->storePath . DIRECTORY_SEPARATOR . $key, 0777);
53-
unlink($this->config->storePath . DIRECTORY_SEPARATOR . $key);
52+
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key, 0777);
53+
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key);
5454
}
5555
}
5656

57-
rmdir($this->config->storePath);
57+
rmdir($this->config->file['storePath']);
5858
}
5959
}
6060

@@ -67,15 +67,15 @@ public function testNewWithNonWritablePath()
6767
{
6868
$this->expectException('CodeIgniter\Cache\Exceptions\CacheException');
6969

70-
chmod($this->config->storePath, 0444);
70+
chmod($this->config->file['storePath'], 0444);
7171
new FileHandler($this->config);
7272
}
7373

7474
public function testSetDefaultPath()
7575
{
76-
//Initialize path
77-
$config = new \Config\Cache();
78-
$config->storePath = null;
76+
// Initialize path
77+
$config = new \Config\Cache();
78+
$config->file['storePath'] = null;
7979

8080
$this->fileHandler = new FileHandler($config);
8181
$this->fileHandler->initialize();
@@ -98,7 +98,7 @@ public function testSave()
9898
{
9999
$this->assertTrue($this->fileHandler->save(self::$key1, 'value'));
100100

101-
chmod($this->config->storePath, 0444);
101+
chmod($this->config->file['storePath'], 0444);
102102
$this->assertFalse($this->fileHandler->save(self::$key2, 'value'));
103103
}
104104

@@ -173,6 +173,36 @@ public function testIsSupported()
173173
$this->assertTrue($this->fileHandler->isSupported());
174174
}
175175

176+
/**
177+
* @dataProvider modeProvider
178+
*/
179+
public function testSaveMode($int, $string)
180+
{
181+
// Initialize mode
182+
$config = new \Config\Cache();
183+
$config->file['mode'] = $int;
184+
185+
$this->fileHandler = new FileHandler($config);
186+
$this->fileHandler->initialize();
187+
188+
$this->fileHandler->save(self::$key1, 'value');
189+
190+
$file = $config->file['storePath'] . DIRECTORY_SEPARATOR . self::$key1;
191+
$mode = substr(sprintf('%o', fileperms($file)), -4);
192+
193+
$this->assertEquals($string, $mode);
194+
}
195+
196+
public function modeProvider()
197+
{
198+
return [
199+
[0640, '0640'],
200+
[0600, '0600'],
201+
[0660, '0660'],
202+
[0777, '0777'],
203+
];
204+
}
205+
176206
//--------------------------------------------------------------------
177207

178208
public function testFileHandler()
@@ -200,8 +230,8 @@ final class BaseTestFileHandler extends FileHandler
200230

201231
public function __construct()
202232
{
203-
$this->config = new \Config\Cache();
204-
$this->config->storePath .= self::$directory;
233+
$this->config = new \Config\Cache();
234+
$this->config->file['storePath'] .= self::$directory;
205235

206236
parent::__construct($this->config);
207237
}

user_guide_src/source/libraries/caching.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ more complex, multi-server setups.
5959
If you have more than one application using the same cache storage, you can add a custom prefix
6060
here that is prepended to all key names.
6161

62-
**$path**
62+
**$file**
6363

64-
This is used by the ``file`` handler to show where it should save the cache files to.
64+
This is an array of settings specific to the ``File`` handler determine how it should save the cache files.
6565

6666
**$memcached**
6767

@@ -212,7 +212,8 @@ File-based Caching
212212
Unlike caching from the Output Class, the driver file-based caching
213213
allows for pieces of view files to be cached. Use this with care, and
214214
make sure to benchmark your application, as a point can come where disk
215-
I/O will negate positive gains by caching. This requires a writable cache directory to be really writable (0777).
215+
I/O will negate positive gains by caching. This requires a cache
216+
directory to be really writable by the application.
216217

217218
=================
218219
Memcached Caching

0 commit comments

Comments
 (0)