1
|
|
|
<?php |
2
|
|
|
namespace Opine\Cache; |
3
|
|
|
|
4
|
|
|
class FileCache { |
5
|
|
|
private $root; |
6
|
|
|
|
7
|
7 |
|
private function getKeyFilePath (string $name):string |
8
|
|
|
{ |
9
|
7 |
|
return $this->root . md5($name) . '.txt'; |
10
|
|
|
} |
11
|
|
|
|
12
|
8 |
|
public function __construct (string $root) |
13
|
|
|
{ |
14
|
8 |
|
$this->root = $root . '/../var/cache/'; |
15
|
8 |
|
} |
16
|
|
|
|
17
|
|
|
// just here for Memcache compatibilty |
18
|
7 |
|
public function pconnect($host, $port):bool |
|
|
|
|
19
|
|
|
{ |
20
|
7 |
|
return true; |
21
|
|
|
} |
22
|
|
|
|
23
|
2 |
|
public function delete ($key):bool |
24
|
|
|
{ |
25
|
2 |
|
$path = $this->getKeyFilePath($key); |
26
|
2 |
|
if (!file_exists($path)) { |
27
|
|
|
return false; |
28
|
|
|
} |
29
|
2 |
|
return unlink($path); |
30
|
|
|
} |
31
|
|
|
|
32
|
3 |
|
public function set($key, $value, $flag, $expire):bool |
|
|
|
|
33
|
|
|
{ |
34
|
3 |
|
$path = $this->getKeyFilePath($key); |
35
|
3 |
|
if (!is_string((string)$value)) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
3 |
|
$value = (string)$value; |
39
|
3 |
|
$result = @file_put_contents($path, $value); |
40
|
3 |
|
$root = rtrim($this->root, '/'); |
41
|
3 |
|
if ($result === false && !file_exists($root)) { |
42
|
|
|
mkdir($root); |
43
|
|
|
file_put_contents($path, $value); |
44
|
|
|
} |
45
|
3 |
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
public function get ($key) |
49
|
|
|
{ |
50
|
6 |
|
if (is_array($key)) { |
51
|
2 |
|
$response = []; |
52
|
2 |
|
foreach ($key as $value) { |
53
|
2 |
|
$path = $this->getKeyFilePath($value); |
54
|
2 |
|
if (!file_exists($path)) { |
55
|
1 |
|
$response[$value] = false; |
56
|
1 |
|
continue; |
57
|
|
|
} |
58
|
2 |
|
$response[$value] = file_get_contents($path); |
59
|
|
|
} |
60
|
2 |
|
return $response; |
61
|
|
|
} |
62
|
4 |
|
$path = $this->getKeyFilePath($key); |
63
|
4 |
|
if (!file_exists($path)) { |
64
|
3 |
|
return false; |
65
|
|
|
} |
66
|
2 |
|
return file_get_contents($path); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.