1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChangelogParser\Manager; |
4
|
|
|
|
5
|
|
|
class CacheManager { |
6
|
|
|
/** @var int **/ |
7
|
|
|
protected $cacheTime = 3600; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Time in seconds |
11
|
|
|
* |
12
|
|
|
* @param int $cacheTime |
13
|
|
|
*/ |
14
|
|
|
public function setCacheTime($cacheTime) { |
15
|
|
|
$this->cacheTime = $cacheTime; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return int |
20
|
|
|
*/ |
21
|
|
|
public function getCacheTime() { |
22
|
|
|
return $this->cacheTime; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $changelogFile |
27
|
|
|
* @param string $cacheFile |
28
|
|
|
* @param string $content |
29
|
|
|
*/ |
30
|
3 |
|
public function generateCache($changelogFile, $cacheFile, $content) { |
31
|
3 |
|
file_put_contents($this->generateCachePath($changelogFile, $cacheFile), $content); |
32
|
3 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $changelogFile |
36
|
|
|
* @param string $cacheFile |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
4 |
|
public function getCache($changelogFile, $cacheFile) { |
40
|
4 |
|
$path = $this->generateCachePath($changelogFile, $cacheFile); |
41
|
4 |
|
if (!is_file($path) || ($generationTime = filemtime($path)) === false || $generationTime < (time() - $this->cacheTime)) { |
42
|
3 |
|
clearstatcache(true, $path); |
43
|
3 |
|
return false; |
44
|
|
|
} |
45
|
1 |
|
$content = file_get_contents($path); |
46
|
1 |
|
clearstatcache(true, $path); |
47
|
1 |
|
return $content; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $changelogFile |
52
|
|
|
* @param string $cacheFile |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
5 |
|
private function generateCachePath($changelogFile, $cacheFile) { |
56
|
5 |
|
$changelogSignature = str_replace([':', ' ', '/', '\\'], '-', strtolower(basename(dirname($changelogFile)) . '-' . basename($changelogFile, '.md'))); |
57
|
|
|
|
58
|
5 |
|
return realpath(__DIR__ . "/../../data/cache"). '/' . $changelogSignature . "-$cacheFile.cache"; |
59
|
|
|
} |
60
|
|
|
} |