mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-31 22:10:45 +02:00
Implementation of an experimental cache manager.
git-svn-id: file:///svn/phpbb/trunk@3312 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
244
phpBB/includes/acm/cache_file.php
Normal file
244
phpBB/includes/acm/cache_file.php
Normal file
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
/***************************************************************************
|
||||
* cache_file.php
|
||||
* -------------------
|
||||
* begin : Saturday, Feb 13, 2001
|
||||
* copyright : (C) 2001 The phpBB Group
|
||||
* email : support@phpbb.com
|
||||
*
|
||||
* $Id$
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
//
|
||||
// This class is part of the Advanced Cache Manager
|
||||
//
|
||||
|
||||
class acm
|
||||
{
|
||||
var $vars = '';
|
||||
var $vars_ts = array();
|
||||
var $modified = FALSE;
|
||||
|
||||
var $sql_rowset = array();
|
||||
var $sql_rowset_index = array();
|
||||
|
||||
function acm()
|
||||
{
|
||||
//$this->load_cache();
|
||||
$this->cache_dir = $phpbb_root_path . 'cache/';
|
||||
}
|
||||
|
||||
function tidy($expire_time = 0)
|
||||
{
|
||||
global $phpEx;
|
||||
|
||||
$dir = opendir($this->cache_dir);
|
||||
while ($entry = readdir($dir))
|
||||
{
|
||||
if ($entry{0} == '.')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!$expire_time || time() - $expire_time >= filemtime($this->cache_dir . $entry))
|
||||
{
|
||||
unlink($this->cache_dir . $entry);
|
||||
}
|
||||
}
|
||||
if (file_exists($this->cache_dir . 'global.' . $phpEx))
|
||||
{
|
||||
foreach ($this->vars_ts as $varname => $timestamp)
|
||||
{
|
||||
if (time() - $expire_time >= $timestamp)
|
||||
{
|
||||
$this->destroy($varname);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->vars = $this->vars_ts = array();
|
||||
$this->modified = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
function save($varname, $var)
|
||||
{
|
||||
$this->vars[$varname] = $var;
|
||||
$this->vars_ts[$varname] = time();
|
||||
$this->modified = TRUE;
|
||||
}
|
||||
|
||||
function destroy($varname)
|
||||
{
|
||||
if (isset($this->vars[$varname]))
|
||||
{
|
||||
$this->modified = TRUE;
|
||||
unset($this->vars[$varname]);
|
||||
unset($this->vars_ts[$varname]);
|
||||
}
|
||||
}
|
||||
|
||||
function load($varname, $expire_time = 0)
|
||||
{
|
||||
if (!is_array($this->vars))
|
||||
{
|
||||
$this->load_cache();
|
||||
}
|
||||
if (isset($this->vars[$varname]))
|
||||
{
|
||||
if ($expire_time && time() - $this->vars_ts[$varname] > $expire_time)
|
||||
{
|
||||
$this->destroy($varname);
|
||||
return null;
|
||||
}
|
||||
return $this->vars[$varname];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function exists($varname, $expire_time = 0)
|
||||
{
|
||||
if (!is_array($this->vars))
|
||||
{
|
||||
$this->load_cache();
|
||||
}
|
||||
|
||||
if ($expire_time > 0)
|
||||
{
|
||||
if (!isset($this->vars[$varname]))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
if ($this->vars_ts[$varname] <= time() - $expire_time)
|
||||
{
|
||||
$this->destroy($varname);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
return isset($this->vars[$varname]);
|
||||
}
|
||||
|
||||
function load_cache()
|
||||
{
|
||||
global $phpEx;
|
||||
|
||||
$this->vars = array();
|
||||
@include($this->cache_dir . 'global.' . $phpEx);
|
||||
}
|
||||
|
||||
function save_cache()
|
||||
{
|
||||
if (!$this->modified)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
global $phpEx;
|
||||
$file = '<?php $this->vars=' . $this->format_array($this->vars) . ";\n\$this->vars_ts=" . $this->format_array($this->vars_ts) . ' ?>';
|
||||
|
||||
$fp = fopen($this->cache_dir . 'global.' . $phpEx, 'wb');
|
||||
fwrite($fp, $file);
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
function format_array($array)
|
||||
{
|
||||
$lines = array();
|
||||
foreach ($array as $k => $v)
|
||||
{
|
||||
if (is_array($v))
|
||||
{
|
||||
$lines[] = "'$k'=>" . $this->format_array($v);
|
||||
}
|
||||
elseif (is_int($v))
|
||||
{
|
||||
$lines[] = "'$k'=>$v";
|
||||
}
|
||||
elseif (is_bool($v))
|
||||
{
|
||||
$lines[] = "'$k'=>" . (($v) ? 'TRUE' : 'FALSE');
|
||||
}
|
||||
else
|
||||
{
|
||||
$lines[] = "'$k'=>'" . str_replace('\\\\', '\\\\\\\\', str_replace("'", "\'", $v)) . "'";
|
||||
}
|
||||
}
|
||||
return 'array(' . implode(',', $lines) . ')';
|
||||
}
|
||||
|
||||
function sql_load($query)
|
||||
{
|
||||
global $db, $phpEx;
|
||||
if (!file_exists($this->cache_dir . md5($query) . '.' . $phpEx))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
include($this->cache_dir . md5($query) . '.' . $phpEx);
|
||||
|
||||
$query_id = 'Cache id #' . count($this->sql_rowset);
|
||||
$this->sql_rowset[$query_id] = $rowset;
|
||||
$this->sql_rowset_index[$query_id] = 0;
|
||||
$db->query_result = $query_id;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function sql_save($query, $result)
|
||||
{
|
||||
global $db, $phpEx;
|
||||
$fp = fopen($this->cache_dir . md5($query) . '.' . $phpEx, 'wb');
|
||||
|
||||
$lines = array();
|
||||
$query_id = 'Cache id #' . count($this->sql_rowset);
|
||||
$this->sql_rowset[$query_id] = array();
|
||||
$this->sql_rowset_index[$query_id] = 0;
|
||||
$db->query_result = $query_id;
|
||||
|
||||
while ($row = $db->sql_fetchrow($result))
|
||||
{
|
||||
$this->sql_rowset[$query_id][] = $row;
|
||||
|
||||
$line = 'array(';
|
||||
foreach ($row as $key => $val)
|
||||
{
|
||||
$line .= "'$key'=>'" . str_replace('\\\\', '\\\\\\\\', str_replace("'", "\'", $val)) . "',";
|
||||
}
|
||||
$lines[] = substr($line, 0, -1) . ')';
|
||||
}
|
||||
|
||||
fwrite($fp, "<?php\n\n/*\n$query\n*/\n\n\$rowset = array(" . implode(',', $lines) . ') ?>');
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
function sql_exists($query_id)
|
||||
{
|
||||
return isset($this->sql_rowset[$query_id]);
|
||||
}
|
||||
|
||||
function sql_fetchrow($query_id)
|
||||
{
|
||||
if (!isset($this->sql_rowset[$query_id][$this->sql_rowset_index[$query_id]]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$row = $this->sql_rowset[$query_id][$this->sql_rowset_index[$query_id]];
|
||||
++$this->sql_rowset_index[$query_id];
|
||||
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
?>
|
@@ -29,6 +29,18 @@ function sql_quote($msg)
|
||||
return str_replace("\'", "''", $msg);
|
||||
}
|
||||
|
||||
function set_config($config_name, $config_value)
|
||||
{
|
||||
global $db, $cache, $config;
|
||||
$config[$config_name] = $config_value;
|
||||
$cache->save('config', $config);
|
||||
|
||||
$sql = 'UPDATE ' . CONFIG_TABLE . "
|
||||
SET config_value = '" . sql_escape($config_value) . "'
|
||||
WHERE config_name = '$config_name'";
|
||||
$db->sql_query($sql);
|
||||
}
|
||||
|
||||
function get_userdata($user)
|
||||
{
|
||||
global $db;
|
||||
@@ -140,7 +152,7 @@ function generate_forum_nav(&$forum_data)
|
||||
// Obtain list of moderators of each forum
|
||||
function get_moderators(&$forum_moderators, $forum_id = false)
|
||||
{
|
||||
global $SID, $db, $acl_options, $phpEx;
|
||||
global $cache, $SID, $db, $acl_options, $phpEx;
|
||||
|
||||
if (!empty($forum_id) && is_array($forum_id))
|
||||
{
|
||||
@@ -189,7 +201,9 @@ function make_jumpbox($action, $forum_id = false)
|
||||
$sql = 'SELECT forum_id, forum_name, forum_postable, left_id, right_id
|
||||
FROM ' . FORUMS_TABLE . '
|
||||
ORDER BY left_id ASC';
|
||||
$result = $db->sql_query($sql);
|
||||
|
||||
// Cache the forums list for 60 seconds
|
||||
$result = $db->sql_query($sql, 60);
|
||||
|
||||
$right = $cat_right = 0;
|
||||
$padding = $forum_list = $holding = '';
|
||||
@@ -646,16 +660,27 @@ function on_page($num_items, $per_page, $start)
|
||||
// to return both sets of arrays
|
||||
function obtain_word_list(&$orig_word, &$replacement_word)
|
||||
{
|
||||
global $db;
|
||||
|
||||
$sql = "SELECT word, replacement
|
||||
FROM " . WORDS_TABLE;
|
||||
$result = $db->sql_query($sql);
|
||||
|
||||
while ($row = $db->sql_fetchrow($result))
|
||||
global $db, $cache;
|
||||
if ($cache->exists('word_censors'))
|
||||
{
|
||||
$orig_word[] = '#\b(' . str_replace('\*', '\w*?', preg_quote($row['word'], '#')) . ')\b#i';
|
||||
$replacement_word[] = $row['replacement'];
|
||||
$words = $cache->load('word_censors');
|
||||
$orig_word = $words['orig'];
|
||||
$replacement_word = $words['replacement'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$sql = "SELECT word, replacement
|
||||
FROM " . WORDS_TABLE;
|
||||
$result = $db->sql_query($sql);
|
||||
|
||||
while ($row = $db->sql_fetchrow($result))
|
||||
{
|
||||
$orig_word[] = '#\b(' . str_replace('\*', '\w*?', preg_quote($row['word'], '#')) . ')\b#i';
|
||||
$replacement_word[] = $row['replacement'];
|
||||
}
|
||||
|
||||
$words = array('orig' => $orig_word, 'replacement' => $replacement_word);
|
||||
$cache->save('word_censors', $words);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -833,7 +858,7 @@ function validate_optional_fields(&$icq, &$aim, &$msnm, &$yim, &$website, &$loca
|
||||
// Error and message handler, call with trigger_error if reqd
|
||||
function msg_handler($errno, $msg_text, $errfile, $errline)
|
||||
{
|
||||
global $db, $auth, $template, $config, $user, $nav_links;
|
||||
global $cache, $db, $auth, $template, $config, $user, $nav_links;
|
||||
global $phpEx, $phpbb_root_path, $starttime;
|
||||
|
||||
switch ($errno)
|
||||
|
@@ -139,18 +139,8 @@ $total_online_users = $logged_visible_online + $logged_hidden_online + $guests_o
|
||||
|
||||
if ($total_online_users > $config['record_online_users'])
|
||||
{
|
||||
$config['record_online_users'] = $total_online_users;
|
||||
$config['record_online_date'] = time();
|
||||
|
||||
$sql = "UPDATE " . CONFIG_TABLE . "
|
||||
SET config_value = '$total_online_users'
|
||||
WHERE config_name = 'record_online_users'";
|
||||
$db->sql_query($sql);
|
||||
|
||||
$sql = "UPDATE " . CONFIG_TABLE . "
|
||||
SET config_value = '" . $config['record_online_date'] . "'
|
||||
WHERE config_name = 'record_online_date'";
|
||||
$db->sql_query($sql);
|
||||
set_config('record_online_users', $total_online_users);
|
||||
set_config('record_online_date', time());
|
||||
}
|
||||
|
||||
if ($total_online_users == 0)
|
||||
|
@@ -51,6 +51,8 @@ $template->assign_vars(array(
|
||||
'DEBUG_OUTPUT' => (defined('DEBUG')) ? $debug_output : ''
|
||||
));
|
||||
|
||||
|
||||
$cache->save_cache();
|
||||
$template->display('body');
|
||||
|
||||
exit;
|
||||
|
@@ -326,10 +326,7 @@ class session
|
||||
{
|
||||
// Less than 5 sessions, update gc timer ... else we want gc
|
||||
// called again to delete other sessions
|
||||
$sql = "UPDATE " . CONFIG_TABLE . "
|
||||
SET config_value = '$current_time'
|
||||
WHERE config_name = 'session_last_gc'";
|
||||
$db->sql_query($sql);
|
||||
set_config('session_last_gc', $current_time);
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -442,7 +439,9 @@ class user extends session
|
||||
AND t.template_id = s.template_id
|
||||
AND c.theme_id = s.style_id
|
||||
AND i.imageset_id = s.imageset_id";
|
||||
$result = $db->sql_query($sql);
|
||||
|
||||
// Cache this query for 60 seconds
|
||||
$result = $db->sql_query($sql, 60);
|
||||
|
||||
if (!($this->theme = $db->sql_fetchrow($result)))
|
||||
{
|
||||
|
Reference in New Issue
Block a user