|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Cache; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* File based cache. |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class CFileCache |
|
10
|
|
|
{ |
|
11
|
|
|
use \Anax\TConfigure; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Create a key to use for the cache. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $class name of the class, including |
|
19
|
|
|
* namespace. |
|
20
|
|
|
* @param string $id unique id for item in each class. |
|
21
|
|
|
* |
|
22
|
|
|
* @return string the filename. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function createKey($class, $id) |
|
25
|
|
|
{ |
|
26
|
|
|
return str_replace('\\', '-', $class) . '#' . $id; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Generate a filename for the cached object. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $key to the cached object. |
|
35
|
|
|
* |
|
36
|
|
|
* @return string the filename. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function filename($key) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->config['basepath'] . '/' . $key; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get an item from the cache if available. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $key to the cached object. |
|
49
|
|
|
* @param boolean $age check the age or not, defaults to |
|
50
|
|
|
* false. |
|
51
|
|
|
* |
|
52
|
|
|
* @return mixed the cached object or false if it has aged |
|
53
|
|
|
* or null if it does not exists. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function get($key, $age = false) |
|
56
|
|
|
{ |
|
57
|
|
|
$file = $this->filename($key); |
|
58
|
|
|
|
|
59
|
|
|
if (is_file($file)) { |
|
60
|
|
|
if ($age) { |
|
61
|
|
|
$age = filemtime($file) + $this->config['age'] > time(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (!$age) { |
|
65
|
|
|
// json |
|
66
|
|
|
// text |
|
67
|
|
|
return unserialize(file_get_contents($file)); |
|
68
|
|
|
} |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
return null; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Put an item to the cache. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $key to the cached object. |
|
80
|
|
|
* @param mixed $item the object to be cached. |
|
81
|
|
|
* |
|
82
|
|
|
* @throws Exception if failing to write to cache. |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function put($key, $item) |
|
87
|
|
|
{ |
|
88
|
|
|
$file = $this->filename($key); |
|
89
|
|
|
|
|
90
|
|
|
// json |
|
91
|
|
|
// text |
|
92
|
|
|
if (!file_put_contents($file, serialize($item))) { |
|
93
|
|
|
throw new \Exception( |
|
94
|
|
|
t("Failed writing cache object '!key'.", [ |
|
95
|
|
|
'!key' => $key |
|
96
|
|
|
]) |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Prune a item from cache. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $key to the cached object. |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function prune($key) |
|
111
|
|
|
{ |
|
112
|
|
|
@unlink($this->filename($key)); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Prune all items from cache. |
|
119
|
|
|
* |
|
120
|
|
|
* @return int number of items removed. |
|
121
|
|
|
*/ |
|
122
|
|
|
public function pruneAll() |
|
123
|
|
|
{ |
|
124
|
|
|
$files = glob($this->config['basepath'] . '/*'); |
|
125
|
|
|
$items = count($files); |
|
126
|
|
|
array_map('unlink', $files); |
|
127
|
|
|
return $items; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: