6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * ACM APC Based Caching
25 private $vars = array();
26 private $is_modified = false;
28 public $sql_rowset = array();
29 public $cache_dir = '';
34 function __construct()
36 $this->cache_dir
= PHPBB_ROOT_PATH
. 'cache/';
42 private function load()
44 // grab the global cache
45 if ($this->vars
= apc_fetch('global'))
56 public function unload()
60 unset($this->sql_rowset
);
62 $this->vars
= array();
63 $this->sql_rowset
= array();
67 * Save modified objects
69 private function save()
71 if (!$this->is_modified
)
76 apc_store('global', $this->vars
, 31536000);
78 $this->is_modified
= false;
84 public function tidy()
86 // cache has auto GC, no need to have any code here :)
88 set_config('cache_last_gc', time(), true);
92 * Get saved cache object
94 public function get($var_name)
96 if ($var_name[0] === '_')
98 return apc_fetch($var_name);
102 if (!sizeof($this->vars
))
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);
121 $this->vars
[$var_name] = $var;
122 $this->is_modified
= true;
129 public function purge()
131 // Purge all phpbb cache files
132 $dir = @opendir
($this->cache_dir
);
139 while (($entry = readdir($dir)) !== false)
141 if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
146 @unlink
($this->cache_dir
. $entry);
150 apc_clear_cache('user');
153 unset($this->sql_rowset
);
155 $this->vars
= array();
156 $this->var_expires
= array();
157 $this->sql_rowset
= array();
159 $this->is_modified
= false;
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);
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);
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
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));
226 $this->sql_rowset
[$query_id] = $temp;
234 public function sql_save($query, &$query_result, $ttl)
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);
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]))
314 unset($this->sql_rowset
[$query_id]);