1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW; |
4
|
|
|
|
5
|
|
|
use ObjectCache; |
6
|
|
|
use Onoi\Cache\CacheFactory as OnoiCacheFactory; |
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @license GNU GPL v2+ |
11
|
|
|
* @since 2.2 |
12
|
|
|
* |
13
|
|
|
* @author mwjames |
14
|
|
|
*/ |
15
|
|
|
class CacheFactory { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string|integer |
19
|
|
|
*/ |
20
|
|
|
private $mainCacheType; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @since 2.2 |
24
|
|
|
* |
25
|
|
|
* @param string|integer|null $mainCacheType |
26
|
|
|
*/ |
27
|
348 |
|
public function __construct( $mainCacheType = null ) { |
|
|
|
|
28
|
348 |
|
$this->mainCacheType = $mainCacheType; |
29
|
|
|
|
30
|
348 |
|
if ( $this->mainCacheType === null ) { |
31
|
299 |
|
$this->mainCacheType = $GLOBALS['smwgMainCacheType']; |
32
|
|
|
} |
33
|
348 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @since 2.2 |
37
|
|
|
* |
38
|
|
|
* @return string|integer |
39
|
|
|
*/ |
40
|
303 |
|
public function getMainCacheType() { |
41
|
303 |
|
return $this->mainCacheType; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @since 2.2 |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
307 |
|
public function getCachePrefix() { |
|
|
|
|
50
|
307 |
|
return $GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @since 2.2 |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
32 |
|
public function getFactboxCacheKey( $key ) { |
61
|
32 |
|
return $this->getCachePrefix() . ':smw:fc:' . md5( $key ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @since 2.2 |
66
|
|
|
* |
67
|
|
|
* @param string $key |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
209 |
|
public function getPurgeCacheKey( $key ) { |
72
|
209 |
|
return $this->getCachePrefix() . ':smw:arc:' . md5( $key ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @since 2.2 |
77
|
|
|
* |
78
|
|
|
* @param array $cacheOptions |
79
|
|
|
* |
80
|
|
|
* @return stdClass |
81
|
|
|
* @throws RuntimeException |
82
|
|
|
*/ |
83
|
191 |
|
public function newCacheOptions( array $cacheOptions ) { |
84
|
|
|
|
85
|
191 |
|
if ( !isset( $cacheOptions['useCache'] ) || !isset( $cacheOptions['ttl'] ) ) { |
86
|
1 |
|
throw new RuntimeException( "Cache options is missing a useCache/ttl parameter" ); |
87
|
|
|
} |
88
|
|
|
|
89
|
190 |
|
return (object)$cacheOptions; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @since 2.2 |
94
|
|
|
* |
95
|
|
|
* @param integer $cacheSize |
96
|
|
|
* |
97
|
|
|
* @return Cache |
98
|
|
|
*/ |
99
|
342 |
|
public function newFixedInMemoryCache( $cacheSize = 500 ) { |
100
|
342 |
|
return OnoiCacheFactory::getInstance()->newFixedInMemoryLruCache( $cacheSize ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @since 2.2 |
105
|
|
|
* |
106
|
|
|
* @return Cache |
107
|
|
|
*/ |
108
|
1 |
|
public function newNullCache() { |
109
|
1 |
|
return OnoiCacheFactory::getInstance()->newNullCache(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @since 2.2 |
114
|
|
|
* |
115
|
|
|
* @param integer|string $mediaWikiCacheType |
116
|
|
|
* |
117
|
|
|
* @return Cache |
118
|
|
|
*/ |
119
|
306 |
|
public function newMediaWikiCompositeCache( $mediaWikiCacheType = null ) { |
120
|
|
|
|
121
|
306 |
|
$compositeCache = OnoiCacheFactory::getInstance()->newCompositeCache( array( |
122
|
306 |
|
$this->newFixedInMemoryCache( 500 ), |
123
|
|
|
$this->newMediaWikiCache( $mediaWikiCacheType ) |
124
|
|
|
) ); |
125
|
306 |
|
|
126
|
306 |
|
return $compositeCache; |
127
|
306 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
306 |
|
* @since 2.5 |
131
|
|
|
* |
132
|
|
|
* @param integer|string $mediaWikiCacheType |
133
|
|
|
* |
134
|
|
|
* @return Cache |
135
|
|
|
*/ |
136
|
|
|
public function newMediaWikiCache( $mediaWikiCacheType = null ) { |
137
|
|
|
|
138
|
|
|
$mediaWikiCache = ObjectCache::getInstance( |
139
|
|
|
( $mediaWikiCacheType === null ? $this->getMainCacheType() : $mediaWikiCacheType ) |
140
|
|
|
); |
141
|
|
|
|
142
|
6 |
|
return OnoiCacheFactory::getInstance()->newMediaWikiCache( $mediaWikiCache ); |
143
|
6 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @since 2.5 |
147
|
|
|
* |
148
|
|
|
* @param integer|null $cacheType |
149
|
|
|
* |
150
|
|
|
* @return Cache |
151
|
|
|
*/ |
152
|
|
|
public function newCacheByType( $cacheType = null ) { |
153
|
|
|
|
154
|
|
|
if ( $cacheType === CACHE_NONE || $cacheType === null ) { |
155
|
|
|
return $this->newNullCache(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->newMediaWikiCache( $cacheType ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @since 2.4 |
163
|
|
|
* |
164
|
|
|
* @param string $namespace |
165
|
|
|
* @param string|integer|null $cacheType |
166
|
|
|
* @param integer $cacheLifetime |
167
|
|
|
* |
168
|
|
|
* @return BlobStore |
169
|
|
|
*/ |
170
|
|
|
public function newBlobStore( $namespace, $cacheType = null, $cacheLifetime = 0 ) { |
171
|
|
|
return ApplicationFactory::getInstance()->create( 'BlobStore', $namespace, $cacheType, $cacheLifetime ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: