1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Gearman Bundle for Symfony2 / Symfony3 |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* Feel free to edit as you please, and have fun. |
10
|
|
|
* |
11
|
|
|
* @author Marc Morera <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Mkk\GearmanBundle\Service; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Cache\Cache; |
17
|
|
|
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface; |
18
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Gearman cache loader class |
22
|
|
|
* |
23
|
|
|
* This class has responsability of loading all gearman data structure |
24
|
|
|
* and cache it if needed. |
25
|
|
|
* |
26
|
|
|
* Also provides this data to external services |
27
|
|
|
*/ |
28
|
|
|
class GearmanCacheWrapper implements CacheClearerInterface, CacheWarmerInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var GearmanParser |
32
|
|
|
* |
33
|
|
|
* Gearman file parser |
34
|
|
|
*/ |
35
|
|
|
private $gearmanParser; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Cache |
39
|
|
|
* |
40
|
|
|
* Cache instance |
41
|
|
|
*/ |
42
|
|
|
private $cache; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
* |
47
|
|
|
* Cache id |
48
|
|
|
*/ |
49
|
|
|
private $cacheId; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
* |
54
|
|
|
* WorkerCollection with all workers and jobs available |
55
|
|
|
*/ |
56
|
|
|
private $workerCollection; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Construct method |
60
|
|
|
* |
61
|
|
|
* @param GearmanParser $gearmanParser Gearman Parser |
62
|
|
|
* @param Cache $cache Cache instance |
63
|
|
|
* @param string $cacheId Cache id |
64
|
|
|
*/ |
65
|
4 |
|
public function __construct( |
66
|
|
|
GearmanParser $gearmanParser, |
67
|
|
|
Cache $cache, |
68
|
|
|
$cacheId |
69
|
|
|
) |
70
|
|
|
{ |
71
|
4 |
|
$this->gearmanParser = $gearmanParser; |
72
|
4 |
|
$this->cache = $cache; |
|
|
|
|
73
|
4 |
|
$this->cacheId = $cacheId; |
|
|
|
|
74
|
4 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return gearman file parser |
78
|
|
|
* |
79
|
|
|
* @return GearmanParser |
80
|
|
|
*/ |
81
|
1 |
|
public function getGearmanParser() |
82
|
|
|
{ |
83
|
1 |
|
return $this->gearmanParser; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return cache |
88
|
|
|
* |
89
|
|
|
* @return Cache Cache |
90
|
|
|
*/ |
91
|
|
|
public function getCache() |
92
|
|
|
{ |
93
|
|
|
return $this->cache; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return cache id |
98
|
|
|
* |
99
|
|
|
* @return string Cache id |
100
|
|
|
*/ |
101
|
|
|
public function getCacheId() |
102
|
|
|
{ |
103
|
|
|
return $this->cacheId; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return workerCollection |
108
|
|
|
* |
109
|
|
|
* @return array all available workers |
110
|
|
|
*/ |
111
|
2 |
|
public function getWorkers() |
112
|
|
|
{ |
113
|
2 |
|
return $this->workerCollection; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* loads Gearman cache, only if is not loaded yet |
118
|
|
|
* |
119
|
|
|
* @param Cache $cache Cache instance |
120
|
|
|
* @param string $cacheId Cache id |
121
|
|
|
* |
122
|
|
|
* @return GearmanCacheWrapper self Object |
123
|
|
|
*/ |
124
|
4 |
|
public function load(Cache $cache, $cacheId) |
125
|
|
|
{ |
126
|
4 |
|
if ($cache->contains($cacheId)) { |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Cache contains gearman structure |
130
|
|
|
*/ |
131
|
3 |
|
$this->workerCollection = $cache->fetch($cacheId); |
|
|
|
|
132
|
|
|
|
133
|
|
|
} else { |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Cache is empty. |
137
|
|
|
* |
138
|
|
|
* Full structure must be generated and cached |
139
|
|
|
*/ |
140
|
1 |
|
$this->workerCollection = $this |
141
|
1 |
|
->getGearmanParser() |
142
|
1 |
|
->load() |
143
|
1 |
|
->toArray(); |
144
|
|
|
|
145
|
1 |
|
$cache->save($cacheId, $this->workerCollection); |
146
|
|
|
} |
147
|
|
|
|
148
|
4 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* flush all cache |
153
|
|
|
* |
154
|
|
|
* @param Cache $cache Cache instance |
155
|
|
|
* @param string $cacheId Cache id |
156
|
|
|
* |
157
|
|
|
* @return GearmanCacheWrapper self Object |
158
|
|
|
*/ |
159
|
|
|
public function flush(Cache $cache, $cacheId) |
160
|
|
|
{ |
161
|
|
|
$cache->delete($cacheId); |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Cache clear implementation |
168
|
|
|
* |
169
|
|
|
* @param string $cacheDir The cache directory |
170
|
|
|
* |
171
|
|
|
* @return GearmanCacheWrapper self Object |
172
|
|
|
*/ |
173
|
|
|
public function clear($cacheDir) |
174
|
|
|
{ |
175
|
|
|
$this->flush($this->getCache(), $this->getCacheId()); |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Warms up the cache. |
182
|
|
|
* |
183
|
|
|
* @param string $cacheDir The cache directory |
184
|
|
|
* |
185
|
|
|
* @return GearmanCacheWrapper self Object |
186
|
|
|
*/ |
187
|
|
|
public function warmUp($cacheDir) |
188
|
|
|
{ |
189
|
|
|
$this->load($this->getCache(), $this->getCacheId()); |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Checks whether this warmer is optional or not. |
196
|
|
|
* |
197
|
|
|
* Optional warmers can be ignored on certain conditions. |
198
|
|
|
* |
199
|
|
|
* A warmer should return true if the cache can be |
200
|
|
|
* generated incrementally and on-demand. |
201
|
|
|
* |
202
|
|
|
* As GearmanBundle loads cache incrementaly so is optional |
203
|
|
|
* |
204
|
|
|
* @return Boolean true if the warmer is optional, false otherwise |
205
|
|
|
*/ |
206
|
1 |
|
public function isOptional() |
207
|
|
|
{ |
208
|
1 |
|
return true; |
209
|
|
|
} |
210
|
|
|
} |
|
|
|
|
211
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.