1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\Asana\Api; |
4
|
|
|
|
5
|
|
|
use DateInterval; |
6
|
|
|
use DateTime; |
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A bare bones PSR-16 "compliant" file-based cache for {@see SimpleCache}. |
11
|
|
|
* |
12
|
|
|
* Use this if you don't have a better cache. |
13
|
|
|
* |
14
|
|
|
* This is not safe for concurrency. |
15
|
|
|
*/ |
16
|
|
|
final class FileCache implements CacheInterface { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $dir; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var LoggerInterface |
25
|
|
|
*/ |
26
|
|
|
private $logger; |
27
|
|
|
|
28
|
|
|
public function __construct (string $dir, LoggerInterface $logger) { |
29
|
|
|
$this->dir = $dir; |
30
|
|
|
$this->logger = $logger; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function _path ($key) { |
34
|
|
|
$file = "{$this->dir}/{$key}~"; |
35
|
|
|
clearstatcache(true, $file); |
36
|
|
|
return $file; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function clear () { |
40
|
|
|
// unused |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function delete ($key) { |
44
|
|
|
$this->logger->log('CACHE DELETE', $key); |
45
|
|
|
unlink($this->_path($key)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function deleteMultiple ($keys) { |
49
|
|
|
// unused |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function get ($key, $default = null) { |
53
|
|
|
$file = $this->_path($key); |
54
|
|
|
if (file_exists($file)) { |
55
|
|
|
if (filemtime($file) > time()) { |
56
|
|
|
$this->logger->log('CACHE HIT', $key); |
57
|
|
|
return unserialize(file_get_contents($this->_path($key))); |
58
|
|
|
} |
59
|
|
|
$this->logger->log('CACHE EXPIRE', $key); |
60
|
|
|
unlink($file); |
61
|
|
|
} |
62
|
|
|
else { |
63
|
|
|
$this->logger->log('CACHE MISS', $key); |
64
|
|
|
} |
65
|
|
|
return $default; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getMultiple ($keys, $default = null) { |
69
|
|
|
// unused |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function has ($key) { |
73
|
|
|
// unused |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function set ($key, $value, $ttl = null) { |
77
|
|
|
assert($ttl instanceof DateInterval); |
78
|
|
|
$this->logger->log('CACHE SET', $key); |
79
|
|
|
$path = $this->_path($key); |
80
|
|
|
if (!is_dir(dirname($path))) { |
81
|
|
|
mkdir(dirname($path), 0770, true); |
82
|
|
|
} |
83
|
|
|
// if a gid ref is being stashed. make a symlink for quality of life. |
84
|
|
|
if (is_scalar($value)) { |
85
|
|
|
$link = substr($path, 0, -1) . '.ref'; |
86
|
|
|
$real = $this->_path("asana/{$value}"); |
87
|
|
|
if (!is_link($link)) { |
88
|
|
|
symlink($real, $link); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
file_put_contents($path, serialize($value)); |
92
|
|
|
chmod($path, 0660); |
93
|
|
|
touch($path, (new DateTime())->add($ttl)->getTimeStamp()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setMultiple ($values, $ttl = null) { |
97
|
|
|
// unused |
98
|
|
|
} |
99
|
|
|
} |