Completed
Push — master ( c90943...b193d1 )
by mw
260:02 queued 224:53
created

InMemoryPoolCache::computeStats()   D

Complexity

Conditions 9
Paths 37

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 37
nop 0
dl 0
loc 35
ccs 0
cts 0
cp 0
crap 90
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
namespace SMW;
4
5
/**
6
 * A multipurpose non-persistent static pool cache to keep selected items for
7
 * the duration of a request cacheable.
8
 *
9
 * @license GNU GPL v2+
10
 * @since 2.3
11
 *
12
 * @author mwjames
13
 */
14
class InMemoryPoolCache {
15
16
	/**
17
	 * Stats as plain string
18
	 */
19
	const FORMAT_PLAIN = 'plain';
20
21
	/**
22
	 * Stats as JSON output
23
	 */
24
	const FORMAT_JSON = 'json';
25
26
	/**
27
	 * Stats as HTML list output
28
	 */
29
	const FORMAT_HTML = 'html';
30
31
	/**
32
	 * @var InMemoryPoolCache
33
	 */
34
	private static $instance = null;
35
36 2
	/**
37 2
	 * @var CacheFactory
38 2
	 */
39
	private $cacheFactory = null;
40
41
	/**
42
	 * @var array
43
	 */
44
	private $poolCacheList = array();
45 297
46
	/**
47 297
	 * @since 2.3
48 1
	 *
49
	 * @param CacheFactory $cacheFactory
50
	 */
51 297
	public function __construct( CacheFactory $cacheFactory ) {
52
		$this->cacheFactory = $cacheFactory;
53
	}
54
55
	/**
56
	 * @since 2.3
57 2
	 *
58 2
	 * @return InMemoryPoolCache
59 2
	 */
60
	public static function getInstance() {
61
62
		if ( self::$instance === null ) {
63
			self::$instance = new self( ApplicationFactory::getInstance()->newCacheFactory() );
64
		}
65
66 276
		return self::$instance;
67 276
	}
68 276
69 276
	/**
70
	 * @since 2.3
71
	 */
72 276
	public static function clear() {
73
		self::$instance = null;
74
	}
75
76
	/**
77
	 * @since 2.3
78
	 *
79 1
	 * @param string $poolCacheName
80
	 */
81 1
	public function resetPoolCacheFor( $poolCacheName = '' ) {
82
		foreach ( $this->poolCacheList as $key => $value ) {
83 1
			if ( $key === $poolCacheName || $poolCacheName === '' ) {
84 1
				unset( $this->poolCacheList[$key] );
85
			}
86
		}
87 1
	}
88
89
	/**
90
	 * @since 2.3
91
	 *
92
	 * @return array
93
	 */
94
	public function getStats() {
95 1
96
		$stats = array();
97 1
98 1
		foreach ( $this->poolCacheList as $key => $value ) {
99
			$stats[$key] = $value->getStats();
100 1
		}
101 1
102
		return $stats;
103 1
	}
104 1
105
	/**
106 1
	 * @since 2.4
107 1
	 *
108
	 * @param string $format
109 1
	 *
110 1
	 * @return string
111
	 */
112
	public function getFormattedStats( $format = self::FORMAT_PLAIN ) {
113 1
114 1
		$stats = $this->computeStats();
115
		$output = '';
116
117 1
		if ( $format === self::FORMAT_PLAIN ) {
118 1
			foreach ( $stats as $key => $value ) {
119
				$output .= '- ' . $key . "\n";
120
121
				foreach ( $value as $k => $v ) {
122 1
					$output .= '  - ' . $k . ': ' . $v . "\n" ;
123 1
				}
124
			}
125
		}
126 1
127
		if ( $format === self::FORMAT_HTML ) {
128
			$output .= '<ul>';
129
			foreach ( $stats as $key => $value ) {
130
				$output .= '<li>' . $key . '<ul>';
131
				foreach ( $value as $k => $v ) {
132
					$output .= '<li>' . $k . ': ' . $v . "</li>" ;
133
				}
134
				$output .= '</ul></li>';
135
			}
136
			$output .= '</ul>';
137 273
		}
138 273
139
		if ( $format === self::FORMAT_JSON ) {
140
			$output .= json_encode( $stats, JSON_PRETTY_PRINT );
141
		}
142
143
		return $output;
144
	}
145
146
	/**
147
	 * @since 2.3
148
	 *
149 281
	 * @param string $poolCacheName
150
	 * @param integer $cacheSize
151 281
	 *
152 246
	 * @return Cache
153
	 */
154
	public function getPoolCacheFor( $poolCacheName, $cacheSize = 500 ) {
155 281
		return $this->getPoolCacheById( $poolCacheName, $cacheSize );
156
	}
157
158
	/**
159
	 * @since 2.3
160
	 *
161
	 * @param string $poolCacheId
162
	 * @param integer $cacheSize
163
	 *
164
	 * @return Cache
165
	 */
166
	public function getPoolCacheById( $poolCacheId, $cacheSize = 500 ) {
167
168
		if ( !isset( $this->poolCacheList[$poolCacheId] ) ) {
169
			$this->poolCacheList[$poolCacheId] = $this->cacheFactory->newFixedInMemoryCache( $cacheSize );
170
		}
171
172
		return $this->poolCacheList[$poolCacheId];
173
	}
174
175
	private function computeStats() {
176
177
		ksort( $this->poolCacheList );
178
		$stats = array();
179
180
		foreach ( $this->poolCacheList as $key => $value ) {
181
			$stats[$key] = array();
182
183
			$hits = 0;
184
			$misses = 0;
185
186
			foreach ( $value->getStats() as $k => $v ) {
187
				$stats[$key][$k] = $v;
188
189
				if ( $k === 'hits' ) {
190
					$hits = $v;
191
				}
192
193
				if ( $k === 'inserts' ) {
194
					$misses = $v;
195
				}
196
197
				if ( $k === 'misses' && $v > 0 ) {
198
					$misses = $v;
199
				}
200
			}
201
202
			$hitRatio = $hits > 0 ? round( $hits / ( $hits + $misses ), 4 ) : 0;
203
204
			$stats[$key]['hit ratio'] = $hitRatio;
205
			$stats[$key]['miss ratio'] = $hitRatio > 0 ? round( 1 - $hitRatio, 4 ) : 0;
206
		}
207
208
		return $stats;
209
	}
210
211
}
212