diff options
author | Ludovic Arnaud <ludovic_arnaud@users.sourceforge.net> | 2003-11-26 23:34:33 +0000 |
---|---|---|
committer | Ludovic Arnaud <ludovic_arnaud@users.sourceforge.net> | 2003-11-26 23:34:33 +0000 |
commit | ac0b5d79ad4c6b7d1e9bf52763918f1e0017b9a0 (patch) | |
tree | ae435d9e1fe6180d9bed855c8f72b01ab3215d1a /phpBB/includes/acm | |
parent | 1dd9ad2f79c5d3e26705ea21396456d346c42c30 (diff) | |
download | forums-ac0b5d79ad4c6b7d1e9bf52763918f1e0017b9a0.tar forums-ac0b5d79ad4c6b7d1e9bf52763918f1e0017b9a0.tar.gz forums-ac0b5d79ad4c6b7d1e9bf52763918f1e0017b9a0.tar.bz2 forums-ac0b5d79ad4c6b7d1e9bf52763918f1e0017b9a0.tar.xz forums-ac0b5d79ad4c6b7d1e9bf52763918f1e0017b9a0.zip |
Changed: the way caches expire. (see dev forum)
Added: "private" caching. (see dev forum too ;))
git-svn-id: file:///svn/phpbb/trunk@4684 89ea8834-ac86-4346-8a33-228a782c2dd0
Diffstat (limited to 'phpBB/includes/acm')
-rw-r--r-- | phpBB/includes/acm/acm_db.php | 175 | ||||
-rw-r--r-- | phpBB/includes/acm/acm_file.php | 121 |
2 files changed, 191 insertions, 105 deletions
diff --git a/phpBB/includes/acm/acm_db.php b/phpBB/includes/acm/acm_db.php index 6301307723..ed57e7605a 100644 --- a/phpBB/includes/acm/acm_db.php +++ b/phpBB/includes/acm/acm_db.php @@ -13,30 +13,51 @@ class acm { + // Contains all loaded variables var $vars = ''; - var $is_modified = FALSE; + + // Contains the names of the variables that are ready to be used + // (iow, variables that have been unserialized) + var $var_ready = array(); + + // Contains variables that have been updated or destroyed this session + var $var_expires = array(); + + // Contains variables that have already been requested + // If a variable has been requested but not loaded, it simply means it + // wasn't found in the db + var $var_requested = array(); function load($var_names = '') { global $db; $this->vars = array(); - $sql = 'SELECT var_name, var_ts, var_data - FROM ' . CACHE_TABLE; - - if (!empty($var_names)) + if (is_array($var_names)) { - $sql .= "\nWHERE var_name IN ('" . implode("', '", $var_names) . "')"; + $var_requested = $var_names; + $sql_condition = "var_name IN ('" . implode("', '", $var_names) . "')"; + } + else + { +// $sql_condition = "var_name NOT LIKE '\_%'"; + $sql_condition = "LEFT(var_name, 1) <> '_'"; } + $sql = 'SELECT var_name, var_data + FROM ' . CACHE_TABLE . ' + WHERE var_expires > ' . time() . " + AND $sql_condition"; $result = $db->sql_query($sql); while ($row = $db->sql_fetchrow($result)) { - $this->vars[$row['var_name']] = array( - 'data' => unserialize($row['var_data']), - 'ts' => intval($row['var_ts']) - ); + $this->vars[$row['var_name']] = $row['var_data']; + + if (!$var_names) + { + $var_requested[] = $row['var_name']; + } } } @@ -48,31 +69,22 @@ class acm function save() { - if (!$this->is_modified) - { - return; - } - global $db; $delete = $insert = array(); - foreach ($this->vars as $var_name => $var_ary) + foreach ($this->var_expires as $var_name => $expires) { - if (!empty($var_ary['is_modified'])) + if ($expires == 'now') { - if (!empty($var_ary['delete'])) - { - $delete[] = $var_name; - } - else - { - $delete[] = $var_name; - $insert[] = "'$var_name', " . time() . ", '" . $db->sql_escape(serialize($var_ary['data'])) . "'"; - } - - $this->vars[$var_name]['is_modified'] = FALSE; + $delete[] = $var_name; + } + else + { + $delete[] = $var_name; + $insert[] = "'$var_name', $expires, '" . $db->sql_escape(serialize($this->vars[$var_name])) . "'"; } } + $this->var_expires = array(); if (count($delete)) { @@ -85,7 +97,8 @@ class acm switch (SQL_LAYER) { case 'mysql': - $sql = 'INSERT INTO ' . CACHE_TABLE . ' (var_name, var_ts, var_data) + case 'mysql4': + $sql = 'INSERT INTO ' . CACHE_TABLE . ' (var_name, var_expires, var_data) VALUES (' . implode('), (', $insert) . ')'; $db->sql_query($sql); break; @@ -93,70 +106,114 @@ class acm default: foreach ($insert as $values) { - $sql = 'INSERT INTO ' . CACHE_TABLE . " (var_name, var_ts, var_data) + $sql = 'INSERT INTO ' . CACHE_TABLE . " (var_name, var_expires, var_data) VALUES ($values)"; $db->sql_query($sql); } } } - - $this->is_modified = FALSE; } - function tidy($max_age = 0) + function tidy() { global $db; $sql = 'DELETE FROM ' . CACHE_TABLE . ' - WHERE var_ts < ' . (time() - $max_age); + WHERE var_expires < ' . time(); $db->sql_query($sql); } - function get($var_name, $max_age = 0) + function get($var_name) { - return ($this->exists($var_name, $max_age)) ? $this->vars[$var_name]['data'] : NULL; + if (!is_array($this->vars)) + { + $this->load(); + } + + if ($var_name{0} == '_') + { + if (!in_array($this->var_requested, $var_name)) + { + $this->var_requested[] = $var_name; + + global $db; + $sql = 'SELECT var_data + FROM ' . CACHE_TABLE . " + WHERE var_name = '$var_name' + AND var_expires > " . time(); + $result = $db->sql_query($sql); + if ($row = $db->sql_fetchrow($result)) + { + $this->vars[$var_name] = $row['var_data']; + } + } + } + + if ($this->exists($var_name)) + { + if (empty($this->var_ready[$var_name])) + { + $this->vars[$var_name] = unserialize($this->vars[$var_name]); + $this->var_ready[$var_name] = TRUE; + } + + return $this->vars[$var_name]; + } + else + { + return NULL; + } } - function put($var_name, $var_data) + function put($var_name, $var_data, $ttl = 31536000) { - $this->vars[$var_name] = array( - 'data' => $var_data, - 'ts' => time(), - 'is_modified' => TRUE - ); + $this->vars[$var_name] = $var_data; + + if ($var_name{0} == '_') + { + global $db; + + switch (SQL_LAYER) + { + case 'mysql': + case 'mysql4': + $INSERT = 'REPLACE'; + break; + + default: + $sql = 'DELETE FROM ' . CACHE_TABLE . " + WHERE var_name = '$var_name'"; + $db->sql_query($sql); + + $INSERT = 'INSERT'; + } - $this->is_modified = TRUE; + $sql = "$INSERT INTO " . CACHE_TABLE . " (var_name, var_expires, var_data) + VALUES ('$var_name', " . (time() + $ttl) . ", '" . $db->sql_escape(serialize($var_data)) . "')"; + $db->sql_query($sql); + } + else + { + $this->var_expires[$var_name] = time() + $ttl; + } } function destroy($var_name, $void = NULL) { if (isset($this->vars[$var_name])) { - $this->is_modified = TRUE; - - $this->vars[$var_name] = array( - 'is_modified' => TRUE, - 'delete' => TRUE - ); + $this->var_expires[$var_name] = 'now'; + unset($this->vars[$var_name]); } } - function exists($var_name, $max_age = 0) + function exists($var_name) { if (!is_array($this->vars)) { $this->load(); } - if ($max_age > 0 && isset($this->vars[$var_name])) - { - if ($this->vars[$var_name]['ts'] + $max_age < time()) - { - $this->destroy($var_name); - return FALSE; - } - } - return isset($this->vars[$var_name]); } } diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index 02f42a6f95..6a3d6335a9 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -14,7 +14,7 @@ class acm { var $vars = ''; - var $vars_ts = array(); + var $var_expires = array(); var $is_modified = FALSE; var $sql_rowset = array(); @@ -35,7 +35,7 @@ class acm { $this->save(); unset($this->vars); - unset($this->vars_ts); + unset($this->var_expires); unset($this->sql_rowset); } @@ -47,7 +47,7 @@ class acm } global $phpEx; - $file = '<?php $this->vars=' . $this->format_array($this->vars) . ";\n\$this->vars_ts=" . $this->format_array($this->vars_ts) . ' ?>'; + $file = '<?php $this->vars=' . $this->format_array($this->vars) . ";\n\$this->var_expires=" . $this->format_array($this->var_expires) . ' ?>'; if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb')) { @@ -60,19 +60,21 @@ class acm $this->is_modified = FALSE; } - function tidy($max_age = 0) + function tidy() { global $phpEx; $dir = opendir($this->cache_dir); while ($entry = readdir($dir)) { - if (substr($entry, 0, 4) != 'sql_') + if (!preg_match('/^(sql_|data_(?!global))/', $entry)) { continue; } - if (time() > filemtime($this->cache_dir . $entry) + $max_age) + $expired = TRUE; + include($this->cache_dir . $entry); + if ($expired) { unlink($this->cache_dir . $entry); } @@ -81,35 +83,62 @@ class acm if (file_exists($this->cache_dir . 'data_global.' . $phpEx)) { - foreach ($this->vars_ts as $var_name => $timestamp) + if (!is_array($this->vars)) { - if (time() > $timestamp + $max_age) + $this->load(); + } + + foreach ($this->var_expires as $var_name => $expires) + { + if (time() > $expires) { $this->destroy($var_name); } } } + } + + function get($var_name) + { + if ($var_name{0} == '_') + { + global $phpEx; + + include($this->cache_dir . 'data' . $var_name . ".$phpEx"); + return (isset($data)) ? $data : NULL; + } else { - $this->vars = $this->vars_ts = array(); - $this->is_modified = TRUE; + return ($this->exists($var_name)) ? $this->vars[$var_name] : NULL; } } - function get($var_name, $max_age = 0) + function put($var_name, $var, $ttl = 31536000) { - return ($this->exists($var_name, $max_age)) ? $this->vars[$var_name] : NULL; - } + if ($var_name{0} == '_') + { + global $phpEx; - function put($var_name, $var) - { - $this->vars[$var_name] = $var; - $this->vars_ts[$var_name] = time(); - $this->is_modified = TRUE; + if ($fp = @fopen($this->cache_dir . 'data' . $var_name . ".$phpEx", 'wb')) + { + @flock($fp, LOCK_EX); + fwrite($fp, "<?php\n\$expired = (time() > " . (time() + $ttl) . ") ? TRUE : FALSE;\nif (\$expired) { return; }\n\n\$data = unserialize('" . str_replace("'", "\\'", str_replace('\\', '\\\\', serialize($var))) . "');\n?>"); + @flock($fp, LOCK_UN); + fclose($fp); + } + } + else + { + $this->vars[$var_name] = $var; + $this->var_expires[$var_name] = time() + $ttl; + $this->is_modified = TRUE; + } } function destroy($var_name, $table = '') { + global $phpEx; + if ($var_name == 'sql' && !empty($table)) { $regex = '(' . ((is_array($table)) ? implode('|', $table) : $table) . ')'; @@ -133,31 +162,34 @@ class acm } @closedir($dir); } + elseif ($var_name{0} == '_') + { + @unlink($this->cache_dir . 'data' . $var_name . ".$phpEx"); + } elseif (isset($this->vars[$var_name])) { $this->is_modified = TRUE; unset($this->vars[$var_name]); - unset($this->vars_ts[$var_name]); + unset($this->var_expires[$var_name]); } } - function exists($var_name, $max_age = 0) + function exists($var_name) { - if (!is_array($this->vars)) + if ($var_name{0} == '_') { - $this->load(); + global $phpEx; + return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx"); } - - if ($max_age > 0 && isset($this->vars_ts[$var_name])) + else { - if (time() > $this->vars_ts[$var_name] + $max_age) + if (!is_array($this->vars)) { - $this->destroy($var_name); - return FALSE; + $this->load(); } - } - return isset($this->vars[$var_name]); + return (time() > $this->var_expires[$var_name]) ? FALSE : isset($this->vars[$var_name]); + } } function format_array($array) @@ -179,34 +211,35 @@ class acm } else { - $lines[] = "'$k'=>'" . str_replace("'", "\'", str_replace('\\', '\\\\', $v)) . "'"; + $lines[] = "'$k'=>'" . str_replace("'", "\\'", str_replace('\\', '\\\\', $v)) . "'"; } } return 'array(' . implode(',', $lines) . ')'; } - function sql_load($query, $max_age) + function sql_load($query) { global $phpEx; // Remove extra spaces and tabs $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); + $query_id = 'Cache id #' . count($this->sql_rowset); - $filemtime = intval(@filemtime($this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx)); - if (time() > $filemtime + $max_age) + include($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"); + if (!isset($expired)) { return FALSE; } - - $query_id = 'Cache id #' . count($this->sql_rowset); - include($this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx); - -// $this->sql_rowset[$query_id] =& $rowset; + elseif ($expired) + { + unlink($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"); + return FALSE; + } return $query_id; } - function sql_save($query, &$query_result) + function sql_save($query, &$query_result, $ttl) { global $db, $phpEx; @@ -225,15 +258,11 @@ class acm { $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) . ')'; + $lines[] = "unserialize('" . str_replace("'", "\\'", str_replace('\\', '\\\\', serialize($row))) . "')"; } + $db->sql_freeresult($query_result); - fwrite($fp, "<?php\n\n/*\n$query\n*/\n\n\$this->sql_rowset[\$query_id] = array(" . implode(',', $lines) . ') ?>'); + fwrite($fp, "<?php\n\n/*\n$query\n*/\n\n\$expired = (time() > " . (time() + $ttl) . ") ? TRUE : FALSE;\nif (\$expired) { return; }\n\n\$this->sql_rowset[\$query_id] = array(" . implode(',', $lines) . ') ?>'); @flock($fp, LOCK_UN); fclose($fp); |