58c434c58a7ebd43dbe204c5c194bbbfdc0b2760
[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 *
31 * @return string
32 */
33 function encode( $value, $expiry ) {
34 # Convert to absolute time
35 $expiry = $this->convertExpiry( $expiry );
36
37 return sprintf( '%010u', intval( $expiry ) ) . ' ' . serialize( $value );
38 }
39
40 /**
41 * @return array list containing value first and expiry second
42 */
43 function decode( $blob ) {
44 if ( !is_string( $blob ) ) {
45 return array( null, 0 );
46 } else {
47 return array(
48 unserialize( substr( $blob, 11 ) ),
49 intval( substr( $blob, 0, 10 ) )
50 );
51 }
52 }
53
54 function getReader() {
55 if ( file_exists( $this->mFile ) ) {
56 $handle = dba_open( $this->mFile, 'rl', $this->mHandler );
57 } else {
58 $handle = $this->getWriter();
59 }
60
61 if ( !$handle ) {
62 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
63 }
64
65 return $handle;
66 }
67
68 function getWriter() {
69 $handle = dba_open( $this->mFile, 'cl', $this->mHandler );
70
71 if ( !$handle ) {
72 wfDebug( "Unable to open DBA cache file {$this->mFile}\n" );
73 }
74
75 return $handle;
76 }
77
78 function get( $key ) {
79 wfProfileIn( __METHOD__ );
80 wfDebug( __METHOD__ . "($key)\n" );
81
82 $handle = $this->getReader();
83 if ( !$handle ) {
84 wfProfileOut( __METHOD__ );
85 return null;
86 }
87
88 $val = dba_fetch( $key, $handle );
89 list( $val, $expiry ) = $this->decode( $val );
90
91 # Must close ASAP because locks are held
92 dba_close( $handle );
93
94 if ( !is_null( $val ) && $expiry && $expiry < time() ) {
95 # Key is expired, delete it
96 $handle = $this->getWriter();
97 dba_delete( $key, $handle );
98 dba_close( $handle );
99 wfDebug( __METHOD__ . ": $key expired\n" );
100 $val = null;
101 }
102
103 wfProfileOut( __METHOD__ );
104 return $val;
105 }
106
107 function set( $key, $value, $exptime = 0 ) {
108 wfProfileIn( __METHOD__ );
109 wfDebug( __METHOD__ . "($key)\n" );
110
111 $blob = $this->encode( $value, $exptime );
112
113 $handle = $this->getWriter();
114 if ( !$handle ) {
115 wfProfileOut( __METHOD__ );
116 return false;
117 }
118
119 $ret = dba_replace( $key, $blob, $handle );
120 dba_close( $handle );
121
122 wfProfileOut( __METHOD__ );
123 return $ret;
124 }
125
126 function delete( $key, $time = 0 ) {
127 wfProfileIn( __METHOD__ );
128 wfDebug( __METHOD__ . "($key)\n" );
129
130 $handle = $this->getWriter();
131 if ( !$handle ) {
132 wfProfileOut( __METHOD__ );
133 return false;
134 }
135
136 $ret = dba_delete( $key, $handle );
137 dba_close( $handle );
138
139 wfProfileOut( __METHOD__ );
140 return $ret;
141 }
142
143 function add( $key, $value, $exptime = 0 ) {
144 wfProfileIn( __METHOD__ );
145
146 $blob = $this->encode( $value, $exptime );
147
148 $handle = $this->getWriter();
149
150 if ( !$handle ) {
151 wfProfileOut( __METHOD__ );
152 return false;
153 }
154
155 $ret = dba_insert( $key, $blob, $handle );
156
157 # Insert failed, check to see if it failed due to an expired key
158 if ( !$ret ) {
159 list( $value, $expiry ) = $this->decode( dba_fetch( $key, $handle ) );
160
161 if ( $expiry < time() ) {
162 # Yes expired, delete and try again
163 dba_delete( $key, $handle );
164 $ret = dba_insert( $key, $blob, $handle );
165 # This time if it failed then it will be handled by the caller like any other race
166 }
167 }
168
169 dba_close( $handle );
170
171 wfProfileOut( __METHOD__ );
172 return $ret;
173 }
174
175 function keys() {
176 $reader = $this->getReader();
177 $k1 = dba_firstkey( $reader );
178
179 if ( !$k1 ) {
180 return array();
181 }
182
183 $result[] = $k1;
184
185 while ( $key = dba_nextkey( $reader ) ) {
186 $result[] = $key;
187 }
188
189 return $result;
190 }
191 }
192