-
Notifications
You must be signed in to change notification settings - Fork 8
/
FileCache.php
110 lines (95 loc) · 2.43 KB
/
FileCache.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
108
109
110
<?php
declare(strict_types = 1);
namespace App\Service;
/**
* This class manages cache for files being translated
*/
class FileCache
{
/**
* 5 hours
*/
private const CACHE_DURATION = 5 * 60 * 60;
/** @var Retriever */
private $retriever;
/** @var string */
private $directory;
/**
* @param Retriever $retriever
* @param string $directory Cache directory
*/
public function __construct(Retriever $retriever, string $directory)
{
$this->retriever = $retriever;
$this->directory = $directory;
}
/**
* Returns a path to the cached file, downloading it if needed.
* If the file is not found on Commons, an exception will be thrown.
*
* @param string $fileName
* @return string
*/
public function getPath(string $fileName): string
{
$this->tick();
$path = $this->fullPath($fileName);
if (!$this->statFile($path)) {
$content = $this->retriever->retrieve($fileName);
file_put_contents($path, $content);
}
return $path;
}
/**
* Returns content of the given file
*
* @param string $fileName
* @return string
*/
public function getContent(string $fileName): string
{
return file_get_contents($this->getPath($fileName));
}
/**
* Purges file cache of stale files with 1/100 probability, to prevent
* too many slowdowns
*/
protected function tick(): void
{
if (42 !== mt_rand(1, 100)) {
return;
}
foreach (glob($this->fullPath('*.*')) as $fileName) {
$this->statFile($fileName);
}
}
/**
* Checks whether the given file is present locally and not stale.
* Deletes the file if it's stale.
*
* @param string $fileName
* @return bool Whether the file is good to go
*/
protected function statFile(string $fileName): bool
{
$stat = @stat($fileName);
if (!$stat) {
return false;
}
if ($stat['mtime'] + self::CACHE_DURATION < time()) {
unlink($fileName);
return false;
}
return true;
}
/**
* Returns a full path for the given file
*
* @param string $fileName
* @return string
*/
public function fullPath(string $fileName): string
{
return $this->directory.DIRECTORY_SEPARATOR.$fileName;
}
}