6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * ACM Memcache Based Caching
18 var $is_modified = false;
20 var $sql_rowset = array();
21 var $sql_row_pointer = array();
29 $this->cache_dir
= $phpbb_root_path . 'cache/';
37 // grab the global cache
38 $temp = eaccelerator_get('global');
59 unset($this->sql_rowset
);
60 unset($this->sql_row_pointer
);
64 * Save modified objects
68 if (!$this->is_modified
)
73 eaccelerator_put('global', $this->vars
, 31536000);
75 $this->is_modified
= false;
85 set_config('cache_last_gc', time(), true);
89 * Get saved cache object
91 function get($var_name)
93 if ($var_name[0] == '_')
95 $temp = eaccelerator_get($var_name);
108 if (!sizeof($this->vars
))
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);
127 $this->vars
[$var_name] = $var;
128 $this->is_modified
= true;
137 // Purge all phpbb cache files
138 $dir = @opendir
($this->cache_dir
);
145 while (($entry = readdir($dir)) !== false)
147 if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0)
152 @unlink
($this->cache_dir
. $entry);
156 foreach (eaccelerator_list_keys() as $var)
158 eaccelerator_rm(substr($var['name'], 1));
162 unset($this->sql_rowset
);
163 unset($this->sql_row_pointer
);
165 $this->is_modified
= false;
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);
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);
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
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));
236 $this->sql_rowset
[$query_id] = $temp;
238 $this->sql_row_pointer
[$query_id] = 0;
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);
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]++
];
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;
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]))
331 $this->sql_row_pointer
[$query_id] = $rownum;
336 * Free memory used for a cached database result (database)
338 function sql_freeresult($query_id)
340 if (!isset($this->sql_rowset
[$query_id]))
345 unset($this->sql_rowset
[$query_id]);
346 unset($this->sql_row_pointer
[$query_id]);