Update code_sniffer build.xml file to be executable on our system
[phpbb.git] / phpBB / includes / acm / acm_eaccelerator.php
blobc6d83da049b39e14938aaee84ba7987994415176
1 <?php
2 /**
4 * @package acm
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * ACM Memcache Based Caching
13 * @package acm
15 class acm
17 var $vars = array();
18 var $is_modified = false;
20 var $sql_rowset = array();
21 var $sql_row_pointer = array();
22 var $cache_dir = '';
24 /**
25 * Set cache path
27 function acm()
29 $this->cache_dir = $phpbb_root_path . 'cache/';
32 /**
33 * Load global cache
35 function load()
37 // grab the global cache
38 $temp = eaccelerator_get('global');
40 if ($temp !== null)
42 $this->vars = $temp;
44 else
46 return false;
49 return true;
52 /**
53 * Unload cache object
55 function unload()
57 $this->save();
58 unset($this->vars);
59 unset($this->sql_rowset);
60 unset($this->sql_row_pointer);
63 /**
64 * Save modified objects
66 function save()
68 if (!$this->is_modified)
70 return;
73 eaccelerator_put('global', $this->vars, 31536000);
75 $this->is_modified = false;
78 /**
79 * Tidy cache
81 function tidy()
83 eaccelerator_gc();
85 set_config('cache_last_gc', time(), true);
88 /**
89 * Get saved cache object
91 function get($var_name)
93 if ($var_name[0] == '_')
95 $temp = eaccelerator_get($var_name);
97 if ($temp !== null)
99 return $temp;
101 else
103 return false;
106 else
108 if (!sizeof($this->vars))
110 $this->load();
112 return (isset($this->vars[$var_name])) ? $this->vars[$var_name] : false;
117 * Put data into cache
119 function put($var_name, $var, $ttl = 31536000)
121 if ($var_name[0] == '_')
123 eaccelerator_put($var_name, $var, $ttl);
125 else
127 $this->vars[$var_name] = $var;
128 $this->is_modified = true;
133 * Purge cache data
135 function purge()
137 // Purge all phpbb cache files
138 $dir = @opendir($this->cache_dir);
140 if (!$dir)
142 return;
145 while (($entry = readdir($dir)) !== false)
147 if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
149 continue;
152 @unlink($this->cache_dir . $entry);
154 closedir($dir);
156 foreach (eaccelerator_list_keys() as $var)
158 eaccelerator_rm(substr($var['name'], 1));
161 unset($this->vars);
162 unset($this->sql_rowset);
163 unset($this->sql_row_pointer);
165 $this->is_modified = false;
169 * Destroy cache data
171 function destroy($var_name, $table = '')
173 if ($var_name == 'sql' && !empty($table))
175 if (!is_array($table))
177 $table = array($table);
180 foreach ($table as $table_name)
182 // gives us the md5s that we want
183 eaccelerator_lock('sql_' . $table_name);
184 $temp = eaccelerator_get('sql_' . $table_name);
185 if ($temp === null)
187 continue;
190 // delete each query ref
191 foreach ($temp as $md5_id => $void)
193 eaccelerator_lock('sql_' . $md5_id);
194 eaccelerator_rm('sql_' . $md5_id);
195 eaccelerator_unlock('sql_' . $md5_id);
198 // delete the table ref
199 eaccelerator_rm('sql_' . $table_name);
200 eaccelerator_unlock('sql_' . $table_name);
203 return;
206 if ($var_name[0] == '_')
208 eaccelerator_rm($var_name);
210 else if (isset($this->vars[$var_name]))
212 $this->is_modified = true;
213 unset($this->vars[$var_name]);
215 // We save here to let the following cache hits succeed
216 $this->save();
221 * Load cached sql query
223 function sql_load($query)
225 // Remove extra spaces and tabs
226 $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
227 $query_id = sizeof($this->sql_rowset);
229 $temp = eaccelerator_get('sql_' . md5($query));
231 if ($temp === null)
233 return false;
236 $this->sql_rowset[$query_id] = $temp;
238 $this->sql_row_pointer[$query_id] = 0;
240 return $query_id;
244 * Save sql query
246 function sql_save($query, &$query_result, $ttl)
248 // Remove extra spaces and tabs
249 $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
251 // determine which tables this query belongs to
252 preg_match('/FROM \\(?(\\w+(?: \\w+)?(?:, ?\\w+(?: \\w+)?)*)\\)?/', $query, $regs);
253 $tables = array_map('trim', explode(',', $regs[1]));
255 foreach ($tables as $table_name)
257 if (($pos = strpos($table_name, ' ')) !== false)
259 $table_name = substr($table_name, 0, $pos);
262 $temp = eaccelerator_get('sql_' . $table_name);
263 if ($temp === null)
265 $temp = array();
267 $temp[md5($query)] = true;
268 eaccelerator_put('sql_' . $table_name, $temp, $ttl);
271 // store them in the right place
272 $query_id = sizeof($this->sql_rowset);
273 $this->sql_rowset[$query_id] = array();
274 $this->sql_row_pointer[$query_id] = 0;
276 while ($row = phpbb::$db->sql_fetchrow($query_result))
278 $this->sql_rowset[$query_id][] = $row;
280 phpbb::$db->sql_freeresult($query_result);
282 eaccelerator_put('sql_' . md5($query), $this->sql_rowset[$query_id], $ttl);
284 $query_result = $query_id;
288 * Ceck if a given sql query exist in cache
290 function sql_exists($query_id)
292 return isset($this->sql_rowset[$query_id]);
296 * Fetch row from cache (database)
298 function sql_fetchrow($query_id)
300 if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
302 return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
305 return false;
309 * Fetch a field from the current row of a cached database result (database)
311 function sql_fetchfield($query_id, $field)
313 if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
315 return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field] : false;
318 return false;
322 * Seek a specific row in an a cached database result (database)
324 function sql_rowseek($rownum, $query_id)
326 if ($rownum >= sizeof($this->sql_rowset[$query_id]))
328 return false;
331 $this->sql_row_pointer[$query_id] = $rownum;
332 return true;
336 * Free memory used for a cached database result (database)
338 function sql_freeresult($query_id)
340 if (!isset($this->sql_rowset[$query_id]))
342 return false;
345 unset($this->sql_rowset[$query_id]);
346 unset($this->sql_row_pointer[$query_id]);
348 return true;