|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the SVN-Buddy library. |
|
4
|
|
|
* For the full copyright and license information, please view |
|
5
|
|
|
* the LICENSE file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
|
8
|
|
|
* @link https://github.com/console-helpers/svn-buddy |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\SVNBuddy\Cache; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use ConsoleHelpers\ConsoleKit\ConsoleIO; |
|
15
|
|
|
use ConsoleHelpers\SVNBuddy\Helper\SizeHelper; |
|
16
|
|
|
|
|
17
|
|
|
class CacheManager |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Working directory. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $_workingDirectory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Console IO. |
|
29
|
|
|
* |
|
30
|
|
|
* @var ConsoleIO |
|
31
|
|
|
*/ |
|
32
|
|
|
private $_io; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Size helper. |
|
36
|
|
|
* |
|
37
|
|
|
* @var SizeHelper |
|
38
|
|
|
*/ |
|
39
|
|
|
private $_sizeHelper; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Create cache manager. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $working_directory Working directory. |
|
45
|
|
|
* @param SizeHelper $size_helper Size helper. |
|
46
|
|
|
* @param ConsoleIO $io Console IO. |
|
47
|
|
|
*/ |
|
48
|
25 |
|
public function __construct($working_directory, SizeHelper $size_helper, ConsoleIO $io = null) |
|
49
|
|
|
{ |
|
50
|
25 |
|
$this->_workingDirectory = $working_directory; |
|
51
|
25 |
|
$this->_sizeHelper = $size_helper; |
|
52
|
25 |
|
$this->_io = $io; |
|
53
|
25 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Sets value in cache. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $name Name. |
|
59
|
|
|
* @param mixed $value Value. |
|
60
|
|
|
* @param mixed $invalidator Invalidator. |
|
61
|
|
|
* @param integer $duration Duration in seconds. |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
11 |
|
public function setCache($name, $value, $invalidator = null, $duration = null) |
|
66
|
|
|
{ |
|
67
|
11 |
|
$duration = $this->durationIntoSeconds($duration); |
|
68
|
|
|
|
|
69
|
11 |
|
$storage = $this->_getStorage($name, $duration); |
|
70
|
10 |
|
$storage->set(array( |
|
71
|
10 |
|
'name' => $name, |
|
72
|
10 |
|
'invalidator' => $invalidator, |
|
73
|
10 |
|
'duration' => $duration, |
|
74
|
10 |
|
'expiration' => $duration ? time() + $duration : null, |
|
75
|
10 |
|
'data' => $value, |
|
76
|
|
|
)); |
|
77
|
10 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Gets value from cache. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $name Name. |
|
83
|
|
|
* @param mixed $invalidator Invalidator. |
|
84
|
|
|
* @param integer $duration Duration in seconds. |
|
85
|
|
|
* |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
10 |
|
public function getCache($name, $invalidator = null, $duration = null) |
|
89
|
|
|
{ |
|
90
|
10 |
|
$storage = $this->_getStorage($name, $this->durationIntoSeconds($duration)); |
|
91
|
10 |
|
$cache = $storage->get(); |
|
92
|
|
|
|
|
93
|
10 |
|
if ( !is_array($cache) || $cache['invalidator'] !== $invalidator ) { |
|
94
|
4 |
|
$storage->invalidate(); |
|
95
|
|
|
|
|
96
|
4 |
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
6 |
|
if ( $cache['expiration'] && $cache['expiration'] < time() ) { |
|
100
|
1 |
|
$storage->invalidate(); |
|
101
|
|
|
|
|
102
|
1 |
|
return null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
6 |
|
return $cache['data']; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Deletes value from cache. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $name Name. |
|
112
|
|
|
* @param integer $duration Duration in seconds. |
|
113
|
|
|
* |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
3 |
|
public function deleteCache($name, $duration = null) |
|
117
|
|
|
{ |
|
118
|
3 |
|
$storage = $this->_getStorage($name, $this->durationIntoSeconds($duration)); |
|
119
|
3 |
|
$storage->invalidate(); |
|
120
|
3 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Converts duration into seconds. |
|
124
|
|
|
* |
|
125
|
|
|
* @param integer $duration Duration in seconds. |
|
126
|
|
|
* |
|
127
|
|
|
* @return integer|null |
|
128
|
|
|
*/ |
|
129
|
15 |
|
protected function durationIntoSeconds($duration = null) |
|
130
|
|
|
{ |
|
131
|
15 |
|
if ( !isset($duration) ) { |
|
132
|
14 |
|
return null; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
2 |
|
if ( is_numeric($duration) ) { |
|
136
|
1 |
|
$duration .= ' seconds'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
2 |
|
$now = time(); |
|
140
|
|
|
|
|
141
|
2 |
|
return strtotime('+' . $duration, $now) - $now; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Returns file-based cache storage. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $name Cache name. |
|
148
|
|
|
* @param integer $duration Duration in seconds. |
|
149
|
|
|
* |
|
150
|
|
|
* @return ICacheStorage |
|
151
|
|
|
* @throws \InvalidArgumentException When namespace is missing in the name. |
|
152
|
|
|
*/ |
|
153
|
15 |
|
private function _getStorage($name, $duration = null) |
|
154
|
|
|
{ |
|
155
|
15 |
|
$name_parts = explode(':', $name, 2); |
|
156
|
|
|
|
|
157
|
15 |
|
if ( count($name_parts) != 2 ) { |
|
158
|
1 |
|
throw new \InvalidArgumentException('The $name parameter must be in "namespace:name" format.'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$filename_parts = array( |
|
162
|
14 |
|
$name_parts[0], |
|
163
|
14 |
|
substr(hash_hmac('sha1', $name_parts[1], 'svn-buddy'), 0, 8), |
|
164
|
14 |
|
'D' . (isset($duration) ? $duration : 'INF'), |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
14 |
|
$cache_filename = $this->_workingDirectory . DIRECTORY_SEPARATOR . implode('_', $filename_parts) . '.cache'; |
|
168
|
|
|
|
|
169
|
14 |
|
if ( isset($this->_io) && $this->_io->isVerbose() ) { |
|
170
|
2 |
|
$message = $cache_filename; |
|
171
|
|
|
|
|
172
|
2 |
|
if ( file_exists($cache_filename) ) { |
|
173
|
1 |
|
$message .= ' (hit: ' . $this->_sizeHelper->formatSize(filesize($cache_filename)) . ')'; |
|
174
|
|
|
} |
|
175
|
|
|
else { |
|
176
|
2 |
|
$message .= ' (miss)'; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
2 |
|
$this->_io->writeln(array( |
|
180
|
2 |
|
'', |
|
181
|
2 |
|
'<debug>[cache]: ' . $message . '</debug>', |
|
182
|
|
|
)); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
14 |
|
return new FileCacheStorage($cache_filename); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|