Adjust some files to support new methods...
[phpbb.git] / phpBB / includes / acm / acm_apc.php
blobe4c4b79de3eb48856aebdfd1b49ed3b6bd94ff3b
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 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * ACM APC Based Caching
21 * @package acm
23 class acm
25 private $vars = array();
26 private $is_modified = false;
28 public $sql_rowset = array();
29 public $cache_dir = '';
31 /**
32 * Set cache path
34 function __construct()
36 $this->cache_dir = PHPBB_ROOT_PATH . 'cache/';
39 /**
40 * Load global cache
42 private function load()
44 // grab the global cache
45 if ($this->vars = apc_fetch('global'))
47 return true;
50 return false;
53 /**
54 * Unload cache object
56 public function unload()
58 $this->save();
59 unset($this->vars);
60 unset($this->sql_rowset);
62 $this->vars = array();
63 $this->sql_rowset = array();
66 /**
67 * Save modified objects
69 private function save()
71 if (!$this->is_modified)
73 return;
76 apc_store('global', $this->vars, 31536000);
78 $this->is_modified = false;
81 /**
82 * Tidy cache
84 public function tidy()
86 // cache has auto GC, no need to have any code here :)
88 set_config('cache_last_gc', time(), true);
91 /**
92 * Get saved cache object
94 public function get($var_name)
96 if ($var_name[0] === '_')
98 return apc_fetch($var_name);
100 else
102 if (!sizeof($this->vars))
104 $this->load();
106 return (isset($this->vars[$var_name])) ? $this->vars[$var_name] : false;
111 * Put data into cache
113 public function put($var_name, $var, $ttl = 31536000)
115 if ($var_name[0] === '_')
117 apc_store($var_name, $var, $ttl);
119 else
121 $this->vars[$var_name] = $var;
122 $this->is_modified = true;
127 * Purge cache data
129 public function purge()
131 // Purge all phpbb cache files
132 $dir = @opendir($this->cache_dir);
134 if (!$dir)
136 return;
139 while (($entry = readdir($dir)) !== false)
141 if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
143 continue;
146 @unlink($this->cache_dir . $entry);
148 closedir($dir);
150 apc_clear_cache('user');
152 unset($this->vars);
153 unset($this->sql_rowset);
155 $this->vars = array();
156 $this->var_expires = array();
157 $this->sql_rowset = array();
159 $this->is_modified = false;
163 * Destroy cache data
165 public function destroy($var_name, $table = '')
167 if ($var_name === 'sql' && !empty($table))
169 if (!is_array($table))
171 $table = array($table);
174 foreach ($table as $table_name)
176 // gives us the md5s that we want
177 $temp = apc_fetch('sql_' . $table_name);
178 if ($temp === false)
180 continue;
183 // delete each query ref
184 foreach ($temp as $md5_id => $void)
186 apc_delete('sql_' . $md5_id);
189 // delete the table ref
190 apc_delete('sql_' . $table_name);
193 return;
196 if ($var_name[0] === '_')
198 apc_delete($var_name);
200 else if (isset($this->vars[$var_name]))
202 $this->is_modified = true;
203 unset($this->vars[$var_name]);
205 // We save here to let the following cache hits succeed
206 $this->save();
211 * Load cached sql query
213 public function sql_load($query)
215 // Remove extra spaces and tabs
216 $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
217 $query_id = sizeof($this->sql_rowset);
219 $temp = apc_fetch('sql_' . md5($query));
221 if ($temp === false)
223 return false;
226 $this->sql_rowset[$query_id] = $temp;
228 return $query_id;
232 * Save sql query
234 public function sql_save($query, &$query_result, $ttl)
236 global $db;
238 // Remove extra spaces and tabs
239 $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
241 // determine which tables this query belongs to:
243 // grab all the FROM tables, avoid getting a LEFT JOIN
244 preg_match('/FROM \(?(\w+(?: (?!LEFT JOIN)\w+)?(?:, ?\w+(?: (?!LEFT JOIN)\w+)?)*)\)?/', $query, $regs);
245 $tables = array_map('trim', explode(',', $regs[1]));
247 // now get the LEFT JOIN
248 preg_match_all('/LEFT JOIN\s+(\w+)(?: \w+)?/', $query, $result, PREG_PATTERN_ORDER);
249 $tables = array_merge($tables, $result[1]);
251 $query_hash = md5($query);
253 foreach ($tables as $table_name)
255 if (($pos = strpos($table_name, ' ')) !== false)
257 $table_name = substr($table_name, 0, $pos);
260 $temp = apc_fetch('sql_' . $table_name);
261 if ($temp === false)
263 $temp = array();
265 $temp[$query_hash] = true;
266 apc_store('sql_' . $table_name, $temp, $ttl);
269 // store them in the right place
270 $query_id = sizeof($this->sql_rowset);
271 $this->sql_rowset[$query_id] = array();
273 while ($row = $db->sql_fetchrow($query_result))
275 $this->sql_rowset[$query_id][] = $row;
277 $db->sql_freeresult($query_result);
279 apc_store('sql_' . $query_hash, $this->sql_rowset[$query_id], $ttl);
281 $query_result = $query_id;
285 * Fetch row from cache (database)
287 public function sql_fetchrow($query_id)
289 list(, $row) = each($this->sql_rowset[$query_id]);
291 return ($row !== NULL) ? $row : false;
295 * Fetch a field from the current row of a cached database result (database)
297 public function sql_fetchfield($query_id, $field)
299 $row = current($this->sql_rowset[$query_id]);
301 return ($row !== false && isset($row[$field])) ? $row[$field] : false;
305 * Free memory used for a cached database result (database)
307 public function sql_freeresult($query_id)
309 if (!isset($this->sql_rowset[$query_id]))
311 return false;
314 unset($this->sql_rowset[$query_id]);
316 return true;