1 | <?php |
||
11 | final class PhpFileCache implements CacheInterface |
||
12 | { |
||
13 | const TYPE_PERSISTENT = true; |
||
14 | const TYPE_NOT_PERSISTENT = false; |
||
15 | |||
16 | const KEY_LIFETIME = 'lifetime'; |
||
17 | const KEY_CREATION_TIME = 'creationTime'; |
||
18 | const KEY_DATA = 'data'; |
||
19 | |||
20 | /** @var bool */ |
||
21 | private $contentHasChanged = false; |
||
22 | |||
23 | /** @var array */ |
||
24 | private $cache; |
||
25 | |||
26 | /** @var string */ |
||
27 | private $cacheFilePath; |
||
28 | |||
29 | /** @var int */ |
||
30 | private $mode; |
||
31 | |||
32 | /** @var int */ |
||
33 | private $hits; |
||
34 | |||
35 | /** @var int */ |
||
36 | private $misses; |
||
37 | |||
38 | /** @var bool */ |
||
39 | private $persistent; |
||
40 | |||
41 | /** |
||
42 | * @var FileSystemInterface |
||
43 | */ |
||
44 | private $fileSystem; |
||
45 | |||
46 | 13 | public function __construct( |
|
47 | string $cacheFileName, |
||
48 | int $mode = self::MODE_VAR_EXPORT, |
||
49 | bool $persistent = self::TYPE_PERSISTENT, |
||
50 | FileSystemInterface $fileSystem = null |
||
51 | ) { |
||
52 | 13 | $this->cacheFilePath = $cacheFileName; |
|
53 | 13 | $this->mode = $mode; |
|
54 | 13 | $this->persistent = $persistent; |
|
55 | 13 | $this->fileSystem = $fileSystem ?? new FileSystemAdapter(); |
|
56 | |||
57 | 13 | $this->cache = $this->unpack(); |
|
58 | 13 | } |
|
59 | |||
60 | /** |
||
61 | * Saves its data in the cache file at destruction time, if it has changed |
||
62 | */ |
||
63 | 13 | public function __destruct() |
|
64 | { |
||
65 | 13 | $this->deleteAllStale(); |
|
66 | |||
67 | 13 | if ($this->contentHasChanged) { |
|
68 | 10 | $this->pack(); |
|
69 | } |
||
70 | 12 | } |
|
71 | |||
72 | /** |
||
73 | * @throws CacheItemNotFoundException |
||
74 | * |
||
75 | * @return mixed |
||
76 | */ |
||
77 | 4 | public function fetch(string $id) |
|
88 | |||
89 | 7 | public function contains(string $id): bool |
|
90 | { |
||
91 | 7 | if (!array_key_exists($id, $this->cache) || $this->isStale($id)) { |
|
92 | 4 | return false; |
|
93 | } |
||
94 | |||
95 | 5 | return true; |
|
96 | } |
||
97 | |||
98 | 10 | public function save(string $id, $data, int $lifeTime = 0, int $creationTime = null): bool |
|
99 | { |
||
100 | 10 | $this->cache[$id] = [ |
|
101 | 10 | self::KEY_DATA => $data, |
|
102 | 10 | self::KEY_LIFETIME => $lifeTime, |
|
103 | 10 | self::KEY_CREATION_TIME => $creationTime ?? time(), |
|
104 | ]; |
||
105 | |||
106 | 10 | $this->contentHasChanged = true; |
|
107 | |||
108 | 10 | return true; |
|
109 | } |
||
110 | |||
111 | 3 | public function delete(string $id): bool |
|
112 | { |
||
113 | 3 | if (isset($this->cache[$id])) { |
|
114 | 3 | unset($this->cache[$id]); |
|
115 | 3 | $this->contentHasChanged = true; |
|
116 | } |
||
117 | |||
118 | 3 | return true; |
|
119 | } |
||
120 | |||
121 | 7 | public function getStats(): array |
|
122 | { |
||
123 | 7 | $this->deleteAllStale(); |
|
124 | |||
125 | return [ |
||
126 | 7 | static::STATS_HITS => $this->hits, |
|
127 | 7 | static::STATS_MISSES => $this->misses, |
|
128 | 7 | static::STATS_UPTIME => $this->fileSystem->fileExists($this->cacheFilePath) |
|
129 | 1 | ? $this->fileSystem->getFileCreationTimestamp($this->cacheFilePath) - time() |
|
130 | 7 | : 0, |
|
131 | 7 | static::STATS_MEMORY_USAGE => memory_get_usage(), |
|
132 | 7 | static::STATS_MEMORY_AVAILABLE => ini_get('memory_limit') - memory_get_usage(), |
|
133 | 7 | static::STATS_ITEM_COUNT => count($this->cache), |
|
134 | ]; |
||
135 | } |
||
136 | |||
137 | 4 | private function toArray(): array |
|
141 | |||
142 | 10 | private function pack() |
|
162 | |||
163 | 13 | private function unpack(): array |
|
183 | |||
184 | 13 | private function deleteAllStale() |
|
192 | |||
193 | 10 | private function isStale(string $id) |
|
200 | } |
||
201 |