|
1
|
|
|
<?php |
|
2
|
|
|
namespace Genkgo\Cache\Adapter; |
|
3
|
|
|
|
|
4
|
|
|
use Genkgo\Cache\CacheAdapterInterface; |
|
5
|
|
|
use Genkgo\Cache\SerializerInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class FileAdapter |
|
9
|
|
|
* @package Genkgo\Cache\Adapter |
|
10
|
|
|
*/ |
|
11
|
|
|
class FileAdapter implements CacheAdapterInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $directory; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var null|int |
|
19
|
|
|
*/ |
|
20
|
|
|
private $chmod; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var null|string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $directorySeparator; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var null|int |
|
27
|
|
|
*/ |
|
28
|
|
|
private $ttl; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var SerializerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $serializer; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $directory |
|
36
|
|
|
* @param SerializerInterface $serializer |
|
37
|
|
|
* @param null $chmod |
|
38
|
|
|
* @param null $directorySeparator |
|
39
|
|
|
* @param null $ttl |
|
40
|
|
|
*/ |
|
41
|
9 |
|
public function __construct( |
|
42
|
|
|
$directory, |
|
43
|
|
|
SerializerInterface $serializer, |
|
44
|
|
|
$chmod = null, |
|
45
|
|
|
$directorySeparator = null, |
|
46
|
|
|
$ttl = null |
|
47
|
|
|
) { |
|
48
|
9 |
|
$this->directory = $directory; |
|
49
|
9 |
|
$this->chmod = $chmod; |
|
50
|
9 |
|
$this->directorySeparator = $directorySeparator; |
|
51
|
9 |
|
$this->ttl = $ttl; |
|
52
|
9 |
|
$this->serializer = $serializer; |
|
53
|
9 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $key |
|
57
|
|
|
* @param $value |
|
58
|
|
|
*/ |
|
59
|
8 |
|
public function set($key, $value) |
|
60
|
|
|
{ |
|
61
|
8 |
|
$file = $this->getFilename($key); |
|
62
|
8 |
|
file_put_contents($file, $this->serializer->serialize($value)); |
|
63
|
|
|
|
|
64
|
8 |
|
if ($this->chmod !== null) { |
|
65
|
5 |
|
chmod($file, $this->chmod); |
|
66
|
5 |
|
} |
|
67
|
8 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $key |
|
71
|
|
|
* @return null|mixed |
|
72
|
|
|
*/ |
|
73
|
7 |
|
public function get($key) |
|
74
|
|
|
{ |
|
75
|
7 |
|
$file = $this->getFilename($key); |
|
76
|
7 |
|
if ($this->exists($file) && $this->valid($file)) { |
|
77
|
6 |
|
return $this->serializer->deserialize(file_get_contents($file)); |
|
78
|
|
|
} |
|
79
|
3 |
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $key |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public function delete($key) |
|
86
|
|
|
{ |
|
87
|
4 |
|
if (strpos($key, '*') !== false) { |
|
88
|
3 |
|
$this->deleteGlob($key); |
|
89
|
3 |
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
$file = $this->getFilename($key); |
|
93
|
1 |
|
if ($this->exists($file)) { |
|
94
|
1 |
|
unlink($file); |
|
95
|
1 |
|
} |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param $pattern |
|
100
|
|
|
*/ |
|
101
|
3 |
|
private function deleteGlob($pattern) |
|
102
|
|
|
{ |
|
103
|
3 |
|
list($directory, $file) = $this->getDirectoryAndFile($pattern); |
|
104
|
3 |
|
$list = new \GlobIterator($directory . '/' . $file); |
|
105
|
|
|
|
|
106
|
3 |
|
foreach ($list as $cacheItem) { |
|
107
|
3 |
|
if ($cacheItem->isDir()) { |
|
108
|
|
|
continue; |
|
109
|
|
|
} |
|
110
|
3 |
|
unlink($cacheItem->getPathName()); |
|
111
|
3 |
|
} |
|
112
|
3 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param $file |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
7 |
|
private function exists($file) |
|
119
|
|
|
{ |
|
120
|
7 |
|
return file_exists($file); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param $file |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
6 |
|
private function valid($file) |
|
128
|
|
|
{ |
|
129
|
6 |
|
return $this->ttl === null || (filemtime($file) + $this->ttl > time()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $key |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
9 |
|
private function getFilename($key) |
|
137
|
|
|
{ |
|
138
|
9 |
|
list($directory, $file) = $this->getDirectoryAndFile($key); |
|
139
|
9 |
|
return $directory . '/' . md5($file); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param $key |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
9 |
|
private function getDirectoryAndFile($key) |
|
147
|
|
|
{ |
|
148
|
9 |
|
$directory = $this->directory; |
|
149
|
|
|
|
|
150
|
9 |
|
if ($this->directorySeparator !== null) { |
|
151
|
2 |
|
while (($position = strpos($key, $this->directorySeparator)) !== false) { |
|
152
|
2 |
|
$dirName = md5(substr($key, 0, $position)); |
|
153
|
2 |
|
$directory = $directory . '/' . $dirName; |
|
154
|
2 |
|
$this->createSubDirectoryIfNotExists($directory); |
|
155
|
2 |
|
$key = substr($key, $position + 1); |
|
156
|
2 |
|
} |
|
157
|
2 |
|
} |
|
158
|
|
|
|
|
159
|
9 |
|
return [$directory, $key]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param $directory |
|
164
|
|
|
*/ |
|
165
|
2 |
|
private function createSubDirectoryIfNotExists($directory) |
|
166
|
|
|
{ |
|
167
|
2 |
|
if (file_exists($directory) === false) { |
|
168
|
1 |
|
mkdir($directory); |
|
169
|
1 |
|
if ($this->chmod !== null) { |
|
170
|
1 |
|
chmod($directory, $this->chmod); |
|
171
|
1 |
|
} |
|
172
|
1 |
|
} |
|
173
|
2 |
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|