Skip to content

Commit 400061a

Browse files
AnahkiasenNyholm
authored andcommitted
Add ability to change cache path in FilesystemCachePool (#72)
* Add ability to change cache path in FilesystemCachePool
1 parent f66e514 commit 400061a

3 files changed

Lines changed: 39 additions & 7 deletions

File tree

FilesystemCachePool.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,38 @@
2525
*/
2626
class FilesystemCachePool extends AbstractCachePool implements TaggablePoolInterface
2727
{
28-
const CACHE_PATH = 'cache';
29-
3028
use TaggablePoolTrait;
3129

3230
/**
3331
* @type Filesystem
3432
*/
3533
private $filesystem;
3634

35+
/**
36+
* The folder should not begin nor end with a slash. Example: path/to/cache.
37+
*
38+
* @type string
39+
*/
40+
private $folder;
41+
3742
/**
3843
* @param Filesystem $filesystem
44+
* @param string $folder
3945
*/
40-
public function __construct(Filesystem $filesystem)
46+
public function __construct(Filesystem $filesystem, $folder = 'cache')
4147
{
48+
$this->folder = $folder;
49+
4250
$this->filesystem = $filesystem;
43-
$this->filesystem->createDir(self::CACHE_PATH);
51+
$this->filesystem->createDir($this->folder);
52+
}
53+
54+
/**
55+
* @param string $folder
56+
*/
57+
public function setFolder($folder)
58+
{
59+
$this->folder = $folder;
4460
}
4561

4662
/**
@@ -71,8 +87,8 @@ protected function fetchObjectFromCache($key)
7187
*/
7288
protected function clearAllObjectsFromCache()
7389
{
74-
$this->filesystem->deleteDir(self::CACHE_PATH);
75-
$this->filesystem->createDir(self::CACHE_PATH);
90+
$this->filesystem->deleteDir($this->folder);
91+
$this->filesystem->createDir($this->folder);
7692

7793
return true;
7894
}
@@ -122,7 +138,7 @@ private function getFilePath($key)
122138
throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid keys must match [a-zA-Z0-9_\.! ].', $key));
123139
}
124140

125-
return sprintf('%s/%s', self::CACHE_PATH, $key);
141+
return sprintf('%s/%s', $this->folder, $key);
126142
}
127143

128144
/**

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ $filesystem = new Filesystem($filesystemAdapter);
3131
$pool = new FilesystemCachePool($filesystem);
3232
```
3333

34+
You can change the folder the cache pool will write to through the `setFolder` setter:
35+
36+
```php
37+
$pool = new FilesystemCachePool($filesystem);
38+
$pool->setFolder('path/to/cache');
39+
```
40+
3441
### Contribute
3542

3643
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/php-cache/cache) or

Tests/FilesystemCachePoolTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,13 @@ public function testCleanupOnExpire()
4444
$this->assertFalse($item->isHit());
4545
$this->assertFalse($this->getFilesystem()->has('cache/test_ttl_null'));
4646
}
47+
48+
public function testChangeFolder()
49+
{
50+
$pool = $this->createCachePool();
51+
$pool->setFolder('foobar');
52+
53+
$pool->save($pool->getItem('test_path'));
54+
$this->assertTrue($this->getFilesystem()->has('foobar/test_path'));
55+
}
4756
}

0 commit comments

Comments
 (0)