1
|
|
|
<?php namespace Comodojo\Dispatcher\Components;
|
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel;
|
4
|
|
|
use \comodojo\Dispatcher\Components\Configuration;
|
5
|
|
|
use \Comodojo\Cache\Providers\ProviderInterface;
|
6
|
|
|
use \Comodojo\Cache\Providers\Filesystem;
|
7
|
|
|
use \Comodojo\Cache\Providers\Memcached;
|
8
|
|
|
use \Comodojo\Cache\Providers\Apc;
|
9
|
|
|
use \Comodojo\Cache\Providers\PhpRedis;
|
10
|
|
|
use \Comodojo\Cache\Cache;
|
11
|
|
|
use \Psr\Log\LoggerInterface;
|
12
|
|
|
|
13
|
|
|
/**
|
14
|
|
|
* @package Comodojo Dispatcher
|
15
|
|
|
* @author Marco Giovinazzi <[email protected]>
|
16
|
|
|
* @author Marco Castiello <[email protected]>
|
17
|
|
|
* @license GPL-3.0+
|
18
|
|
|
*
|
19
|
|
|
* LICENSE:
|
20
|
|
|
*
|
21
|
|
|
* This program is free software: you can redistribute it and/or modify
|
22
|
|
|
* it under the terms of the GNU Affero General Public License as
|
23
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
24
|
|
|
* License, or (at your option) any later version.
|
25
|
|
|
*
|
26
|
|
|
* This program is distributed in the hope that it will be useful,
|
27
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
28
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
29
|
|
|
* GNU Affero General Public License for more details.
|
30
|
|
|
*
|
31
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
32
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
33
|
|
|
*/
|
34
|
|
|
|
35
|
|
|
class CacheManager extends DispatcherClassModel {
|
36
|
|
|
|
37
|
|
|
protected static $algorithms = array(
|
38
|
|
|
'PICK_LAST' => Cache::PICK_LAST,
|
39
|
|
|
'PICK_RANDOM' => Cache::PICK_RANDOM,
|
40
|
|
|
'PICK_BYWEIGHT' => Cache::PICK_BYWEIGHT,
|
41
|
|
|
'PICK_ALL' => Cache::PICK_ALL,
|
42
|
|
|
'PICK_TRAVERSE' => Cache::PICK_TRAVERSE,
|
43
|
|
|
'PICK_FIRST' => Cache::PICK_FIRST
|
44
|
|
|
);
|
45
|
|
|
|
46
|
1 |
|
public function init() {
|
47
|
|
|
|
48
|
1 |
|
$cache = $this->configuration->get('cache');
|
49
|
|
|
|
50
|
1 |
|
$algorithm = self::getAlgorithm( empty($cache['algorithm']) ? null : $cache['algorithm']);
|
51
|
|
|
|
52
|
1 |
|
$manager = new Cache($algorithm, $this->logger);
|
53
|
|
|
|
54
|
1 |
|
if ( !empty($cache) && !empty($cache['providers']) && is_array($cache['providers']) ) {
|
55
|
|
|
|
56
|
1 |
|
foreach ($cache['providers'] as $provider => $parameters) {
|
57
|
|
|
|
58
|
1 |
|
list($handler, $weight) = $this->getHandler($provider, $parameters);
|
59
|
|
|
|
60
|
1 |
|
if ( $handler instanceof ProviderInterface ) $manager->addProvider($handler, $weight);
|
61
|
|
|
|
62
|
1 |
|
}
|
63
|
|
|
|
64
|
1 |
|
}
|
65
|
|
|
|
66
|
1 |
|
return $manager;
|
67
|
|
|
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
/**
|
71
|
|
|
* Create the Cache Manager
|
72
|
|
|
*
|
73
|
|
|
* @return \Comodojo\Cache\Cache
|
74
|
|
|
*/
|
75
|
1 |
|
public static function create(Configuration $configuration, LoggerInterface $logger) {
|
76
|
|
|
|
77
|
1 |
|
$cache = new CacheManager($configuration, $logger);
|
78
|
|
|
|
79
|
1 |
|
return $cache->init();
|
80
|
|
|
|
81
|
|
|
}
|
82
|
|
|
|
83
|
1 |
|
protected function getHandler($provider, $parameters) {
|
84
|
|
|
|
85
|
1 |
|
if ( empty($parameters['type']) ) return array(null, null);
|
86
|
|
|
|
87
|
1 |
|
$weight = empty($parameters['weight']) ? 0 : $parameters['weight'];
|
88
|
|
|
|
89
|
1 |
|
switch ( $parameters['type'] ) {
|
90
|
|
|
|
91
|
1 |
|
case 'Filesystem':
|
|
|
|
|
92
|
|
|
|
93
|
1 |
|
$handler = $this->getFilesystemProvider($provider, $parameters);
|
94
|
|
|
|
95
|
1 |
|
break;
|
96
|
|
|
|
97
|
|
|
case 'Apc':
|
|
|
|
|
98
|
|
|
|
99
|
|
|
$handler = $this->getApcProvider($provider, $parameters);
|
100
|
|
|
|
101
|
|
|
break;
|
102
|
|
|
|
103
|
|
|
case 'Memcached':
|
|
|
|
|
104
|
|
|
|
105
|
|
|
$handler = $this->getMemcachedProvider($provider, $parameters);
|
106
|
|
|
|
107
|
|
|
break;
|
108
|
|
|
|
109
|
|
|
case 'PhpRedis':
|
|
|
|
|
110
|
|
|
|
111
|
|
|
$handler = $this->getPhpRedisProvider($provider, $parameters);
|
112
|
|
|
|
113
|
|
|
break;
|
114
|
|
|
|
115
|
|
|
default:
|
116
|
|
|
$handler = null;
|
117
|
|
|
break;
|
118
|
1 |
|
}
|
119
|
|
|
|
120
|
1 |
|
return array($handler, $weight);
|
121
|
|
|
|
122
|
|
|
}
|
123
|
|
|
|
124
|
1 |
|
protected function getFilesystemProvider($provider, $parameters) {
|
125
|
|
|
|
126
|
1 |
|
$base = $this->configuration->get('base-path');
|
127
|
|
|
|
128
|
1 |
|
if ( empty($parameters['folder']) || empty($base) ) {
|
129
|
|
|
$this->logger->warning("Wrong cache provider, disabling $provider", $parameters);
|
130
|
|
|
return null;
|
131
|
|
|
}
|
132
|
|
|
|
133
|
1 |
|
$target = $base.'/'.$parameters['folder'];
|
134
|
|
|
|
135
|
1 |
|
return new Filesystem($target);
|
136
|
|
|
|
137
|
|
|
}
|
138
|
|
|
|
139
|
|
|
protected function getApcProvider($provider, $parameters) {
|
|
|
|
|
140
|
|
|
|
141
|
|
|
return new Apc();
|
142
|
|
|
|
143
|
|
|
}
|
144
|
|
|
|
145
|
|
|
protected function getMemcachedProvider($provider, $parameters) {
|
|
|
|
|
146
|
|
|
|
147
|
|
|
$server = empty($parameters['server']) ? '127.0.0.1' : $parameters['server'];
|
148
|
|
|
$port = empty($parameters['port']) ? '1121' : $parameters['port'];
|
149
|
|
|
$weight = empty($parameters['weight']) ? 0 : $parameters['weight'];
|
150
|
|
|
$persistent_id = empty($parameters['persistent_id']) ? null : $parameters['persistent_id'];
|
151
|
|
|
|
152
|
|
|
return new Memcached($server, $port, $weight, $persistent_id);
|
153
|
|
|
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
protected function getPhpRedisProvider($provider, $parameters) {
|
|
|
|
|
157
|
|
|
|
158
|
|
|
$server = empty($parameters['server']) ? '127.0.0.1' : $parameters['server'];
|
159
|
|
|
$port = empty($parameters['port']) ? '1121' : $parameters['port'];
|
160
|
|
|
$timeout = empty($parameters['timeout']) ? 0 : $parameters['timeout'];
|
161
|
|
|
|
162
|
|
|
return new PhpRedis($server, $port, $timeout);
|
163
|
|
|
|
164
|
|
|
}
|
165
|
|
|
|
166
|
|
|
/**
|
167
|
|
|
* Map provided log level to level code
|
168
|
|
|
*
|
169
|
|
|
* @param string $algorithm
|
170
|
|
|
*
|
171
|
|
|
* @return integer
|
172
|
|
|
*/
|
173
|
1 |
|
protected static function getAlgorithm($algorithm = null) {
|
174
|
|
|
|
175
|
1 |
|
$algorithm = strtoupper($algorithm);
|
176
|
|
|
|
177
|
1 |
|
if ( array_key_exists($algorithm, self::$algorithms) ) return self::$algorithms[$algorithm];
|
178
|
|
|
|
179
|
|
|
return self::$algorithms['PICK_FIRST'];
|
180
|
|
|
|
181
|
|
|
}
|
182
|
|
|
|
183
|
|
|
}
|
184
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.