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 Abstract Memory Class
28 var $is_modified = false;
30 var $sql_rowset = array();
31 var $sql_row_pointer = array();
39 global $phpbb_root_path, $dbname, $table_prefix;
41 $this->cache_dir = $phpbb_root_path . 'cache/';
42 $this->key_prefix = substr(md5($dbname . $table_prefix), 0, 8) . '_';
44 if (!isset($this->extension) || !extension_loaded($this->extension))
48 trigger_error("Could not find required extension [{$this->extension}] for the ACM module $acm_type.", E_USER_ERROR);
51 if (isset($this->function) && !function_exists($this->function))
55 trigger_error("The required function [{$this->function}] is not available for the ACM module $acm_type.", E_USER_ERROR);
64 // grab the global cache
65 $this->vars = $this->_read('global');
67 if ($this->vars !== false)
82 unset($this->sql_rowset);
83 unset($this->sql_row_pointer);
85 $this->vars = array();
86 $this->sql_rowset = array();
87 $this->sql_row_pointer = array();
91 * Save modified objects
95 if (!$this->is_modified)
100 $this->_write('global', $this->vars, 2592000);
102 $this->is_modified = false;
110 // cache has auto GC, no need to have any code here :)
112 set_config('cache_last_gc', time(), true);
116 * Get saved cache object
118 function get($var_name)
120 if ($var_name[0] == '_')
122 if (!$this->_exists($var_name))
127 return $this->_read($var_name);
131 return ($this->_exists($var_name)) ? $this->vars[$var_name] : false;
136 * Put data into cache
138 function put($var_name, $var, $ttl = 2592000)
140 if ($var_name[0] == '_')
142 $this->_write($var_name, $var, $ttl);
146 $this->vars[$var_name] = $var;
147 $this->is_modified = true;
156 // Purge all phpbb cache files
157 $dir = @opendir($this->cache_dir);
164 while (($entry = readdir($dir)) !== false)
166 if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
171 $this->remove_file($this->cache_dir . $entry);
176 unset($this->sql_rowset);
177 unset($this->sql_row_pointer);
179 $this->vars = array();
180 $this->sql_rowset = array();
181 $this->sql_row_pointer = array();
183 $this->is_modified = false;
190 function destroy($var_name, $table = '')
192 if ($var_name == 'sql' && !empty($table))
194 if (!is_array($table))
196 $table = array($table);
199 foreach ($table as $table_name)
201 // gives us the md5s that we want
202 $temp = $this->_read('sql_' . $table_name);
209 // delete each query ref
210 foreach ($temp as $md5_id => $void)
212 $this->_delete('sql_' . $md5_id);
215 // delete the table ref
216 $this->_delete('sql_' . $table_name);
222 if (!$this->_exists($var_name))
227 if ($var_name[0] == '_')
229 $this->_delete($var_name);
231 else if (isset($this->vars[$var_name]))
233 $this->is_modified = true;
234 unset($this->vars[$var_name]);
236 // We save here to let the following cache hits succeed
242 * Check if a given cache entry exist
244 function _exists($var_name)
246 if ($var_name[0] == '_')
248 return $this->_isset($var_name);
252 if (!sizeof($this->vars))
257 return isset($this->vars[$var_name]);
262 * Load cached sql query
264 function sql_load($query)
266 // Remove extra spaces and tabs
267 $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
268 $query_id = sizeof($this->sql_rowset);
270 if (($result = $this->_read('sql_' . md5($query))) === false)
275 $this->sql_rowset[$query_id] = $result;
276 $this->sql_row_pointer[$query_id] = 0;
284 function sql_save($query, &$query_result, $ttl)
288 // Remove extra spaces and tabs
289 $query = preg_replace('/[\n\r\s\t]+/', ' ', $query);
292 // determine which tables this query belongs to
293 // Some queries use backticks, namely the get_database_size() query
294 // don't check for conformity, the SQL would error and not reach here.
295 if (!preg_match('/FROM \\(?(`?\\w+`?(?: \\w+)?(?:, ?`?\\w+`?(?: \\w+)?)*)\\)?/', $query, $regs))
297 // Bail out if the match fails.
300 $tables = array_map('trim', explode(',', $regs[1]));
302 foreach ($tables as $table_name)
305 $table_name = ($table_name[0] == '`') ? substr($table_name, 1, -1) : $table_name;
307 if (($pos = strpos($table_name, ' ')) !== false)
309 $table_name = substr($table_name, 0, $pos);
312 $temp = $this->_read('sql_' . $table_name);
321 // This must never expire
322 $this->_write('sql_' . $table_name, $temp, 0);
325 // store them in the right place
326 $query_id = sizeof($this->sql_rowset);
327 $this->sql_rowset[$query_id] = array();
328 $this->sql_row_pointer[$query_id] = 0;
330 while ($row = $db->sql_fetchrow($query_result))
332 $this->sql_rowset[$query_id][] = $row;
334 $db->sql_freeresult($query_result);
336 $this->_write('sql_' . $hash, $this->sql_rowset[$query_id], $ttl);
338 $query_result = $query_id;
342 * Ceck if a given sql query exist in cache
344 function sql_exists($query_id)
346 return isset($this->sql_rowset[$query_id]);
350 * Fetch row from cache (database)
352 function sql_fetchrow($query_id)
354 if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
356 return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++];
363 * Fetch a field from the current row of a cached database result (database)
365 function sql_fetchfield($query_id, $field)
367 if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id]))
369 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;
376 * Seek a specific row in an a cached database result (database)
378 function sql_rowseek($rownum, $query_id)
380 if ($rownum >= sizeof($this->sql_rowset[$query_id]))
385 $this->sql_row_pointer[$query_id] = $rownum;
390 * Free memory used for a cached database result (database)
392 function sql_freeresult($query_id)
394 if (!isset($this->sql_rowset[$query_id]))
399 unset($this->sql_rowset[$query_id]);
400 unset($this->sql_row_pointer[$query_id]);
406 * Removes/unlinks file
408 function remove_file($filename, $check = false)
410 if (!function_exists('phpbb_is_writable'))
412 global $phpbb_root_path, $phpEx;
413 include($phpbb_root_path . 'includes/functions.' . $phpEx);
416 if ($check && !phpbb_is_writable($this->cache_dir))
418 // E_USER_ERROR - not using language entry - intended.
419 trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR);
422 return @unlink($filename);
426 * Check if a cache var exists
429 * @param string $var Cache key
430 * @return bool True if it exists, otherwise false
432 function _isset($var)
434 // Most caches don't need to check