1
|
|
|
<?php namespace Comodojo\Dispatcher\Cache;
|
2
|
|
|
|
3
|
|
|
use \Monolog\Logger;
|
4
|
|
|
use \Comodojo\Cache\CacheManager;
|
5
|
|
|
use \Comodojo\Cache\FileCache;
|
6
|
|
|
|
7
|
|
|
/**
|
8
|
|
|
*
|
9
|
|
|
*
|
10
|
|
|
* @package Comodojo Framework
|
11
|
|
|
* @author Marco Giovinazzi <[email protected]>
|
12
|
|
|
* @author Marco Castiello <[email protected]>
|
13
|
|
|
* @license GPL-3.0+
|
14
|
|
|
*
|
15
|
|
|
* LICENSE:
|
16
|
|
|
*
|
17
|
|
|
* This program is free software: you can redistribute it and/or modify
|
18
|
|
|
* it under the terms of the GNU Affero General Public License as
|
19
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
20
|
|
|
* License, or (at your option) any later version.
|
21
|
|
|
*
|
22
|
|
|
* This program is distributed in the hope that it will be useful,
|
23
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
24
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
25
|
|
|
* GNU Affero General Public License for more details.
|
26
|
|
|
*
|
27
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
28
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
29
|
|
|
*/
|
30
|
|
|
|
31
|
|
|
class DispatcherCache {
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* Create the Cache Manager
|
35
|
|
|
*
|
36
|
|
|
* @return \Comodojo\Cache\CacheManager
|
37
|
|
|
*/
|
38
|
|
|
public static function create(Configuration $configuration, Logger $logger) {
|
|
|
|
|
39
|
|
|
|
40
|
|
|
$enabled = $configuration->get('dispatcher-cache-enabled');
|
41
|
|
|
|
42
|
|
|
$ttl = $configuration->get('dispatcher-cache-ttl');
|
43
|
|
|
|
44
|
|
|
if ( $ttl !== null && !defined('COMODOJO_CACHE_DEFAULT_TTL') ) {
|
45
|
|
|
|
46
|
|
|
define('COMODOJO_CACHE_DEFAULT_TTL', $ttl);
|
47
|
|
|
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
$folder = $configuration->get('dispatcher-cache-folder');
|
51
|
|
|
|
52
|
|
|
$algorithm = self::getAlgorithm( $configuration->get('dispatcher-cache-algorithm') );
|
53
|
|
|
|
54
|
|
|
$manager = new CacheManager( $algorithm );
|
55
|
|
|
|
56
|
|
|
if ( $enabled === true ) {
|
57
|
|
|
|
58
|
|
|
$manager->addProvider( new FileCache($folder) );
|
59
|
|
|
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
return $manager;
|
63
|
|
|
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
/**
|
67
|
|
|
* Map provided log level to level code
|
68
|
|
|
*
|
69
|
|
|
* @param string $level
|
|
|
|
|
70
|
|
|
*
|
71
|
|
|
* @return integer
|
72
|
|
|
*/
|
73
|
|
|
protected static function getAlgorithm($algorithm) {
|
74
|
|
|
|
75
|
|
|
switch ( strtoupper($algorithm) ) {
|
76
|
|
|
|
77
|
|
|
case 'PICK_LAST':
|
78
|
|
|
$selected = CacheManager::PICK_LAST;
|
79
|
|
|
break;
|
80
|
|
|
|
81
|
|
|
case 'PICK_RANDOM':
|
82
|
|
|
$selected = CacheManager::PICK_RANDOM;
|
83
|
|
|
break;
|
84
|
|
|
|
85
|
|
|
case 'PICK_BYWEIGHT':
|
86
|
|
|
$selected = CacheManager::PICK_BYWEIGHT;
|
87
|
|
|
break;
|
88
|
|
|
|
89
|
|
|
case 'PICK_ALL':
|
90
|
|
|
$selected = CacheManager::PICK_ALL;
|
91
|
|
|
break;
|
92
|
|
|
|
93
|
|
|
case 'PICK_FIRST':
|
94
|
|
|
default:
|
95
|
|
|
$selected = CacheManager::PICK_FIRST;
|
96
|
|
|
break;
|
97
|
|
|
|
98
|
|
|
}
|
99
|
|
|
|
100
|
|
|
return $selected;
|
101
|
|
|
|
102
|
|
|
}
|
103
|
|
|
|
104
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.