1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Cache; |
4
|
|
|
|
5
|
|
|
use BagOStuff; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* MediaWiki BagOStuff decorator |
9
|
|
|
* |
10
|
|
|
* @license GNU GPL v2+ |
11
|
|
|
* @since 1.0 |
12
|
|
|
* |
13
|
|
|
* @author mwjames |
14
|
|
|
*/ |
15
|
|
|
class MediaWikiCache implements Cache { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var BagOStuff |
19
|
|
|
*/ |
20
|
|
|
private $cache = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @note MediaWiki's BagOStuff doesn't know any has/contains function therefore |
24
|
|
|
* we need to use an internal array the fetch and temporarily store the results |
25
|
|
|
* to ensure no expensive lookups occur for the same key |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $inMemoryCache = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var integer |
33
|
|
|
*/ |
34
|
|
|
private $cacheInserts = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var integer |
38
|
|
|
*/ |
39
|
|
|
private $cacheDeletes = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var integer |
43
|
|
|
*/ |
44
|
|
|
private $cacheHits = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var integer |
48
|
|
|
*/ |
49
|
|
|
private $cacheMisses = 0; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @since 1.0 |
53
|
|
|
* |
54
|
|
|
* @param BagOStuff $cache |
55
|
|
|
*/ |
56
|
6 |
|
public function __construct( BagOStuff $cache ) { |
57
|
6 |
|
$this->cache = $cache; |
58
|
6 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @since 1.0 |
62
|
|
|
* |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
1 |
|
public function fetch( $id ) { |
66
|
|
|
|
67
|
1 |
|
if ( $this->contains( $id ) ) { |
68
|
1 |
|
$this->cacheHits++; |
69
|
1 |
|
return $this->inMemoryCache[ $id ]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->cacheMisses++; |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @since 1.0 |
78
|
|
|
* |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
4 |
|
public function contains( $id ) { |
82
|
|
|
|
83
|
4 |
|
if ( isset ( $this->inMemoryCache[ $id ] ) || array_key_exists( $id, $this->inMemoryCache ) ) { |
84
|
1 |
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
$this->inMemoryCache[ $id ] = $this->cache->get( $id ); |
88
|
|
|
|
89
|
4 |
|
return !$this->inMemoryCache[ $id ] ? false : true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @since 1.0 |
94
|
|
|
* |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
3 |
|
public function save( $id, $data, $ttl = 0 ) { |
98
|
3 |
|
$this->cacheInserts++; |
99
|
3 |
|
$this->cache->set( $id, $data, $ttl ); |
100
|
3 |
|
unset( $this->inMemoryCache[ $id ] ); |
101
|
3 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @since 1.0 |
105
|
|
|
* |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
*/ |
108
|
3 |
|
public function delete( $id ) { |
109
|
3 |
|
$this->cacheDeletes++; |
110
|
3 |
|
$this->cache->delete( $id ); |
111
|
3 |
|
unset( $this->inMemoryCache[ $id ] ); |
112
|
3 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @since 1.0 |
116
|
|
|
* |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
1 |
|
public function getStats() { |
120
|
|
|
return array( |
121
|
1 |
|
'inserts' => $this->cacheInserts, |
122
|
1 |
|
'deletes' => $this->cacheDeletes, |
123
|
1 |
|
'hits' => $this->cacheHits, |
124
|
1 |
|
'misses' => $this->cacheMisses |
125
|
1 |
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @since 1.2 |
130
|
|
|
* |
131
|
|
|
* {@inheritDoc} |
132
|
|
|
*/ |
133
|
1 |
|
public function getName() { |
134
|
1 |
|
return __CLASS__ . '::' . get_class( $this->cache ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|