1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW; |
4
|
|
|
|
5
|
|
|
use SMW\Utils\StatsFormatter; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A multipurpose non-persistent static pool cache to keep selected items for |
9
|
|
|
* the duration of a request cacheable. |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 2.3 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class InMemoryPoolCache { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Stats as plain string |
20
|
|
|
*/ |
21
|
|
|
const FORMAT_PLAIN = StatsFormatter::FORMAT_PLAIN; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Stats as JSON output |
25
|
|
|
*/ |
26
|
|
|
const FORMAT_JSON = StatsFormatter::FORMAT_JSON; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Stats as HTML list output |
30
|
|
|
*/ |
31
|
|
|
const FORMAT_HTML = StatsFormatter::FORMAT_HTML; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var InMemoryPoolCache |
35
|
|
|
*/ |
36
|
|
|
private static $instance = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var CacheFactory |
40
|
|
|
*/ |
41
|
|
|
private $cacheFactory = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $poolCacheList = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @since 2.3 |
50
|
|
|
* |
51
|
|
|
* @param CacheFactory $cacheFactory |
52
|
|
|
*/ |
53
|
3 |
|
public function __construct( CacheFactory $cacheFactory ) { |
54
|
3 |
|
$this->cacheFactory = $cacheFactory; |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @since 2.3 |
59
|
|
|
* |
60
|
|
|
* @return InMemoryPoolCache |
61
|
|
|
*/ |
62
|
336 |
|
public static function getInstance() { |
63
|
|
|
|
64
|
336 |
|
if ( self::$instance === null ) { |
65
|
2 |
|
self::$instance = new self( ApplicationFactory::getInstance()->newCacheFactory() ); |
66
|
|
|
} |
67
|
|
|
|
68
|
336 |
|
return self::$instance; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @since 2.3 |
73
|
|
|
*/ |
74
|
3 |
|
public static function clear() { |
75
|
3 |
|
self::$instance = null; |
76
|
3 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @since 2.3 |
80
|
|
|
* |
81
|
|
|
* @param string $poolCacheName |
82
|
|
|
*/ |
83
|
299 |
|
public function resetPoolCacheById( $poolCacheName = '' ) { |
84
|
299 |
|
foreach ( $this->poolCacheList as $key => $value ) { |
85
|
299 |
|
if ( $key === $poolCacheName || $poolCacheName === '' ) { |
86
|
299 |
|
unset( $this->poolCacheList[$key] ); |
87
|
|
|
} |
88
|
|
|
} |
89
|
299 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @since 2.4 |
93
|
|
|
* |
94
|
|
|
* @param string|null $format |
95
|
|
|
* |
96
|
|
|
* @return string|array |
97
|
|
|
*/ |
98
|
1 |
|
public function getStats( $format = null ) { |
99
|
1 |
|
return StatsFormatter::format( $this->computeStats(), $format ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @deprecated since 2.5, use InMemoryPoolCache::getPoolCacheById |
104
|
|
|
* @since 2.3 |
105
|
|
|
* |
106
|
|
|
* @param string $poolCacheName |
107
|
|
|
* @param integer $cacheSize |
108
|
|
|
* |
109
|
|
|
* @return Cache |
110
|
295 |
|
*/ |
111
|
295 |
|
public function getPoolCacheFor( $poolCacheName, $cacheSize = 500 ) { |
112
|
|
|
return $this->getPoolCacheById( $poolCacheName, $cacheSize ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @since 2.5 |
117
|
|
|
* |
118
|
|
|
* @param string $poolCacheId |
119
|
|
|
* @param integer $cacheSize |
120
|
|
|
* |
121
|
|
|
* @return Cache |
122
|
324 |
|
*/ |
123
|
|
|
public function getPoolCacheById( $poolCacheId, $cacheSize = 500 ) { |
124
|
324 |
|
|
125
|
268 |
|
if ( !isset( $this->poolCacheList[$poolCacheId] ) ) { |
126
|
|
|
$this->poolCacheList[$poolCacheId] = $this->cacheFactory->newFixedInMemoryCache( $cacheSize ); |
127
|
|
|
} |
128
|
324 |
|
|
129
|
|
|
return $this->poolCacheList[$poolCacheId]; |
130
|
|
|
} |
131
|
1 |
|
|
132
|
|
|
private function computeStats() { |
133
|
1 |
|
|
134
|
1 |
|
ksort( $this->poolCacheList ); |
135
|
|
|
$stats = array(); |
136
|
1 |
|
|
137
|
1 |
|
foreach ( $this->poolCacheList as $key => $value ) { |
138
|
|
|
$stats[$key] = array(); |
139
|
1 |
|
|
140
|
1 |
|
$hits = 0; |
141
|
|
|
$misses = 0; |
142
|
1 |
|
|
143
|
1 |
|
foreach ( $value->getStats() as $k => $v ) { |
144
|
|
|
$stats[$key][$k] = $v; |
145
|
1 |
|
|
146
|
1 |
|
if ( $k === 'hits' ) { |
147
|
|
|
$hits = $v; |
148
|
|
|
} |
149
|
1 |
|
|
150
|
1 |
|
if ( $k === 'inserts' ) { |
151
|
|
|
$misses = $v; |
152
|
|
|
} |
153
|
1 |
|
|
154
|
1 |
|
if ( $k === 'misses' && $v > 0 ) { |
155
|
|
|
$misses = $v; |
156
|
|
|
} |
157
|
|
|
} |
158
|
1 |
|
|
159
|
|
|
$hitRatio = $hits > 0 ? round( $hits / ( $hits + $misses ), 4 ) : 0; |
160
|
1 |
|
|
161
|
1 |
|
$stats[$key]['hit ratio'] = $hitRatio; |
162
|
|
|
$stats[$key]['miss ratio'] = $hitRatio > 0 ? round( 1 - $hitRatio, 4 ) : 0; |
163
|
|
|
} |
164
|
1 |
|
|
165
|
|
|
return $stats; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|