/
FileCacheStorage.php
107 lines (94 loc) · 3.61 KB
/
FileCacheStorage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
namespace Rector\Caching\ValueObject\Storage;
use Nette\Utils\Random;
use Rector\Caching\ValueObject\CacheFilePaths;
use Rector\Caching\ValueObject\CacheItem;
use PHPStan\File\FileWriter;
use Symplify\SmartFileSystem\SmartFileSystem;
use Symplify\EasyCodingStandard\Caching\Exception\CachingException;
/**
* Inspired by https://github.com/phpstan/phpstan-src/blob/1e7ceae933f07e5a250b61ed94799e6c2ea8daa2/src/Cache/FileCacheStorage.php
*/
final class FileCacheStorage
{
public function __construct(
private string $directory,
private SmartFileSystem $smartFileSystem
) {
}
/**
* @param string $key
* @param string $variableKey
* @return mixed|null
*/
public function load(string $key, string $variableKey)
{
return (function (string $key, string $variableKey) {
$cacheFilePaths = $this->getCacheFilePaths($key);
$filePath = $cacheFilePaths->getFilePath();
if (!\is_file($filePath)) {
return null;
}
$cacheItem = (require $filePath);
if (!$cacheItem instanceof CacheItem) {
return null;
}
if (!$cacheItem->isVariableKeyValid($variableKey)) {
return null;
}
return $cacheItem->getData();
})($key, $variableKey);
}
/**
* @param string $key
* @param string $variableKey
* @param mixed $data
* @return void
*/
public function save(string $key, string $variableKey, $data) : void
{
$cacheFilePaths = $this->getCacheFilePaths($key);
$this->smartFileSystem->mkdir($cacheFilePaths->getFirstDirectory());
$this->smartFileSystem->mkdir($cacheFilePaths->getSecondDirectory());
$path = $cacheFilePaths->getFilePath();
$tmpPath = \sprintf('%s/%s.tmp', $this->directory, Random::generate());
$errorBefore = \error_get_last();
$exported = @\var_export(new CacheItem($variableKey, $data), true);
$errorAfter = \error_get_last();
if ($errorAfter !== null && $errorBefore !== $errorAfter) {
throw new CachingException(\sprintf('Error occurred while saving item %s (%s) to cache: %s', $key, $variableKey, $errorAfter['message']));
}
// for performance reasons we don't use SmartFileSystem
FileWriter::write($tmpPath, \sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported));
$renameSuccess = @\rename($tmpPath, $path);
if ($renameSuccess) {
return;
}
@\unlink($tmpPath);
if (\DIRECTORY_SEPARATOR === '/' || !\file_exists($path)) {
throw new CachingException(\sprintf('Could not write data to cache file %s.', $path));
}
}
public function clean(string $cacheKey): void
{
$cacheFilePaths = $this->getCacheFilePaths($cacheKey);
$this->smartFileSystem->remove([
$cacheFilePaths->getFirstDirectory(),
$cacheFilePaths->getSecondDirectory(),
$cacheFilePaths->getFilePath(),
]);
}
public function clear(): void
{
$this->smartFileSystem->remove($this->directory);
}
private function getCacheFilePaths(string $key): CacheFilePaths
{
$keyHash = sha1($key);
$firstDirectory = sprintf('%s/%s', $this->directory, substr($keyHash, 0, 2));
$secondDirectory = sprintf('%s/%s', $firstDirectory, substr($keyHash, 2, 2));
$filePath = sprintf('%s/%s.php', $secondDirectory, $keyHash);
return new CacheFilePaths($firstDirectory, $secondDirectory, $filePath);
}
}