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