summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/InMemoryPoolCache.php
blob: a53b7b987883cbacc36c5e2007c1d16647b30f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

namespace SMW;

use SMW\Utils\StatsFormatter;

/**
 * A multipurpose non-persistent static pool cache to keep selected items for
 * the duration of a request cacheable.
 *
 * @license GNU GPL v2+
 * @since 2.3
 *
 * @author mwjames
 */
class InMemoryPoolCache {

	/**
	 * Stats as plain string
	 */
	const FORMAT_PLAIN = StatsFormatter::FORMAT_PLAIN;

	/**
	 * Stats as JSON output
	 */
	const FORMAT_JSON = StatsFormatter::FORMAT_JSON;

	/**
	 * Stats as HTML list output
	 */
	const FORMAT_HTML = StatsFormatter::FORMAT_HTML;

	/**
	 * @var InMemoryPoolCache
	 */
	private static $instance = null;

	/**
	 * @var CacheFactory
	 */
	private $cacheFactory = null;

	/**
	 * @var array
	 */
	private $poolCacheList = [];

	/**
	 * @since 2.3
	 *
	 * @param CacheFactory $cacheFactory
	 */
	public function __construct( CacheFactory $cacheFactory ) {
		$this->cacheFactory = $cacheFactory;
	}

	/**
	 * @since 2.3
	 *
	 * @return InMemoryPoolCache
	 */
	public static function getInstance() {

		if ( self::$instance === null ) {
			self::$instance = new self( ApplicationFactory::getInstance()->newCacheFactory() );
		}

		return self::$instance;
	}

	/**
	 * @since 2.3
	 */
	public static function clear() {
		self::$instance = null;
	}

	/**
	 * @since 2.3
	 *
	 * @param string $poolCacheName
	 */
	public function resetPoolCacheById( $poolCacheName = '' ) {
		foreach ( $this->poolCacheList as $key => $value ) {
			if ( $key === $poolCacheName || $poolCacheName === '' ) {
				unset( $this->poolCacheList[$key] );
			}
		}
	}

	/**
	 * @since 2.4
	 *
	 * @param string|null $format
	 *
	 * @return string|array
	 */
	public function getStats( $format = null ) {
		return StatsFormatter::format( $this->computeStats(), $format );
	}

	/**
	 * @deprecated since 2.5, use InMemoryPoolCache::getPoolCacheById
	 * @since 2.3
	 *
	 * @param string $poolCacheName
	 * @param integer $cacheSize
	 *
	 * @return Cache
	 */
	public function getPoolCacheFor( $poolCacheName, $cacheSize = 500 ) {
		return $this->getPoolCacheById( $poolCacheName, $cacheSize );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $poolCacheId
	 * @param integer $cacheSize
	 *
	 * @return Cache
	 */
	public function getPoolCacheById( $poolCacheId, $cacheSize = 500 ) {

		if ( !isset( $this->poolCacheList[$poolCacheId] ) ) {
			$this->poolCacheList[$poolCacheId] = $this->cacheFactory->newFixedInMemoryCache( $cacheSize );
		}

		return $this->poolCacheList[$poolCacheId];
	}

	private function computeStats() {

		ksort( $this->poolCacheList );
		$stats = [];

		foreach ( $this->poolCacheList as $key => $value ) {
			$stats[$key] = [];

			$hits = 0;
			$misses = 0;

			foreach ( $value->getStats() as $k => $v ) {
				$stats[$key][$k] = $v;

				if ( $k === 'hits' ) {
					$hits = $v;
				}

				if ( $k === 'inserts' ) {
					$misses = $v;
				}

				if ( $k === 'misses' && $v > 0 ) {
					$misses = $v;
				}
			}

			$hitRatio = $hits > 0 ? round( $hits / ( $hits + $misses ), 4 ) : 0;

			$stats[$key]['hit ratio'] = $hitRatio;
			$stats[$key]['miss ratio'] = $hitRatio > 0 ? round( 1 - $hitRatio, 4 ) : 0;
		}

		return $stats;
	}

}