Even more documentation in various files
[lhc/web/wiklou.git] / includes / objectcache / DBABagOStuff.php
1 <?php
2
3 /**
4 * Cache that uses DBA as a backend.
5 * Slow due to the need to constantly open and close the file to avoid holding
6 * writer locks. Intended for development use only, as a memcached workalike
7 * for systems that don't have it.
8 *
9 * @ingroup Cache
10 */
11 class DBABagOStuff extends BagOStuff {
12 var $mHandler, $mFile, $mReader, $mWriter, $mDisabled;
13
14 public function __construct( $dir = false ) {
15 global $wgDBAhandler;
16
17 if ( $dir === false ) {
18 global $wgTmpDirectory;
19 $dir = $wgTmpDirectory;
20 }
21
22 $this->mFile = "$dir/mw-cache-" . wfWikiID();
23 $this->mFile .= '.db';
24 wfDebug( __CLASS__ . ": using cache file {$this->mFile}\n" );
25 $this->mHandler = $wgDBAhandler;
26 }
27
28 /**
29 * Encode value and expiry for storage
30 * @param $value
31 * @param $expiry
32 *
33 * @return string
34 */
35 function encode( $value, $expiry ) {
36 # Convert to absolute time
37 $expiry = $this->convertExpiry( $expiry );
38
39 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
40 }
41
42 /**
43 * @return array list containing value first and expiry second
44 */
45 function decode( $blob ) {
46 if ( !is_string( $blob ) ) {
47 return array( null, 0 );
48 } else {
49 return array(
50 unserialize( substr( $blob, 11 ) ),
51 intval( substr( $blob, 0, 10 ) )
52 );
53 }
54 }
55
56 function getReader() {
57 if ( file_exists( $this->mFile ) ) {
58 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
59 } else {
60 $handle = $this->getWriter();
61 }
62
63 if ( !$handle ) {
64 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
65 }
66
67 return $handle;
68 }
69
70 function getWriter() {
71 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
72
73 if ( !$handle ) {
74 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
75 }
76
77 return $handle;
78 }
79
80 function get( $key ) {
81 wfProfileIn( __METHOD__ );
82 wfDebug( __METHOD__ . "($key)\n" );
83
84 $handle = $this->getReader();
85 if ( !$handle ) {
86 wfProfileOut( __METHOD__ );
87 return null;
88 }
89
90 $val = dba_fetch( $key, $handle );
91 list( $val, $expiry ) = $this->decode( $val );
92
93 # Must close ASAP because locks are held
94 dba_close( $handle );
95
96 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
97 # Key is expired, delete it
98 $handle = $this->getWriter();
99 dba_delete( $key, $handle );
100 dba_close( $handle );
101 wfDebug( __METHOD__ . ": $key expired\n" );
102 $val = null;
103 }
104
105 wfProfileOut( __METHOD__ );
106 return $val;
107 }
108
109 function set( $key, $value, $exptime = 0 ) {
110 wfProfileIn( __METHOD__ );
111 wfDebug( __METHOD__ . "($key)\n" );
112
113 $blob = $this->encode( $value, $exptime );
114
115 $handle = $this->getWriter();
116 if ( !$handle ) {
117 wfProfileOut( __METHOD__ );
118 return false;
119 }
120
121 $ret = dba_replace( $key, $blob, $handle );
122 dba_close( $handle );
123
124 wfProfileOut( __METHOD__ );
125 return $ret;
126 }
127
128 function delete( $key, $time = 0 ) {
129 wfProfileIn( __METHOD__ );
130 wfDebug( __METHOD__ . "($key)\n" );
131
132 $handle = $this->getWriter();
133 if ( !$handle ) {
134 wfProfileOut( __METHOD__ );
135 return false;
136 }
137
138 $ret = dba_delete( $key, $handle );
139 dba_close( $handle );
140
141 wfProfileOut( __METHOD__ );
142 return $ret;
143 }
144
145 function add( $key, $value, $exptime = 0 ) {
146 wfProfileIn( __METHOD__ );
147
148 $blob = $this->encode( $value, $exptime );
149
150 $handle = $this->getWriter();
151
152 if ( !$handle ) {
153 wfProfileOut( __METHOD__ );
154 return false;
155 }
156
157 $ret = dba_insert( $key, $blob, $handle );
158
159 # Insert failed, check to see if it failed due to an expired key
160 if ( !$ret ) {
161 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
162
163 if ( $expiry < time() ) {
164 # Yes expired, delete and try again
165 dba_delete( $key, $handle );
166 $ret = dba_insert( $key, $blob, $handle );
167 # This time if it failed then it will be handled by the caller like any other race
168 }
169 }
170
171 dba_close( $handle );
172
173 wfProfileOut( __METHOD__ );
174 return $ret;
175 }
176
177 function keys() {
178 $reader = $this->getReader();
179 $k1 = dba_firstkey( $reader );
180
181 if ( !$k1 ) {
182 return array();
183 }
184
185 $result[] = $k1;
186
187 while ( $key = dba_nextkey( $reader ) ) {
188 $result[] = $key;
189 }
190
191 return $result;
192 }
193 }
194