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
This commit is contained in:
Ludovic Arnaud 2003-11-26 23:34:33 +00:00
parent 1dd9ad2f79
commit ac0b5d79ad
2 changed files with 203 additions and 117 deletions

View file

@ -13,30 +13,51 @@
class acm class acm
{ {
// Contains all loaded variables
var $vars = ''; 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 = '') function load($var_names = '')
{ {
global $db; global $db;
$this->vars = array(); $this->vars = array();
$sql = 'SELECT var_name, var_ts, var_data if (is_array($var_names))
FROM ' . CACHE_TABLE;
if (!empty($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); $result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) while ($row = $db->sql_fetchrow($result))
{ {
$this->vars[$row['var_name']] = array( $this->vars[$row['var_name']] = $row['var_data'];
'data' => unserialize($row['var_data']),
'ts' => intval($row['var_ts']) if (!$var_names)
); {
$var_requested[] = $row['var_name'];
}
} }
} }
@ -48,31 +69,22 @@ class acm
function save() function save()
{ {
if (!$this->is_modified)
{
return;
}
global $db; global $db;
$delete = $insert = array(); $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; $delete[] = $var_name;
} }
else else
{ {
$delete[] = $var_name; $delete[] = $var_name;
$insert[] = "'$var_name', " . time() . ", '" . $db->sql_escape(serialize($var_ary['data'])) . "'"; $insert[] = "'$var_name', $expires, '" . $db->sql_escape(serialize($this->vars[$var_name])) . "'";
}
$this->vars[$var_name]['is_modified'] = FALSE;
} }
} }
$this->var_expires = array();
if (count($delete)) if (count($delete))
{ {
@ -85,7 +97,8 @@ class acm
switch (SQL_LAYER) switch (SQL_LAYER)
{ {
case 'mysql': 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) . ')'; VALUES (' . implode('), (', $insert) . ')';
$db->sql_query($sql); $db->sql_query($sql);
break; break;
@ -93,69 +106,113 @@ class acm
default: default:
foreach ($insert as $values) 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)"; VALUES ($values)";
$db->sql_query($sql); $db->sql_query($sql);
} }
} }
} }
$this->is_modified = FALSE;
} }
function tidy($max_age = 0) function tidy()
{ {
global $db; global $db;
$sql = 'DELETE FROM ' . CACHE_TABLE . ' $sql = 'DELETE FROM ' . CACHE_TABLE . '
WHERE var_ts < ' . (time() - $max_age); WHERE var_expires < ' . time();
$db->sql_query($sql); $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;
}
function put($var_name, $var_data)
{
$this->vars[$var_name] = array(
'data' => $var_data,
'ts' => time(),
'is_modified' => TRUE
);
$this->is_modified = TRUE;
}
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
);
}
}
function exists($var_name, $max_age = 0)
{ {
if (!is_array($this->vars)) if (!is_array($this->vars))
{ {
$this->load(); $this->load();
} }
if ($max_age > 0 && isset($this->vars[$var_name])) if ($var_name{0} == '_')
{ {
if ($this->vars[$var_name]['ts'] + $max_age < time()) if (!in_array($this->var_requested, $var_name))
{ {
$this->destroy($var_name); $this->var_requested[] = $var_name;
return FALSE;
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, $ttl = 31536000)
{
$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';
}
$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->var_expires[$var_name] = 'now';
unset($this->vars[$var_name]);
}
}
function exists($var_name)
{
if (!is_array($this->vars))
{
$this->load();
}
return isset($this->vars[$var_name]); return isset($this->vars[$var_name]);
} }

View file

@ -14,7 +14,7 @@
class acm class acm
{ {
var $vars = ''; var $vars = '';
var $vars_ts = array(); var $var_expires = array();
var $is_modified = FALSE; var $is_modified = FALSE;
var $sql_rowset = array(); var $sql_rowset = array();
@ -35,7 +35,7 @@ class acm
{ {
$this->save(); $this->save();
unset($this->vars); unset($this->vars);
unset($this->vars_ts); unset($this->var_expires);
unset($this->sql_rowset); unset($this->sql_rowset);
} }
@ -47,7 +47,7 @@ class acm
} }
global $phpEx; 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')) if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb'))
{ {
@ -60,19 +60,21 @@ class acm
$this->is_modified = FALSE; $this->is_modified = FALSE;
} }
function tidy($max_age = 0) function tidy()
{ {
global $phpEx; global $phpEx;
$dir = opendir($this->cache_dir); $dir = opendir($this->cache_dir);
while ($entry = readdir($dir)) while ($entry = readdir($dir))
{ {
if (substr($entry, 0, 4) != 'sql_') if (!preg_match('/^(sql_|data_(?!global))/', $entry))
{ {
continue; continue;
} }
if (time() > filemtime($this->cache_dir . $entry) + $max_age) $expired = TRUE;
include($this->cache_dir . $entry);
if ($expired)
{ {
unlink($this->cache_dir . $entry); unlink($this->cache_dir . $entry);
} }
@ -81,35 +83,62 @@ class acm
if (file_exists($this->cache_dir . 'data_global.' . $phpEx)) 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); $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 else
{ {
$this->vars = $this->vars_ts = array(); return ($this->exists($var_name)) ? $this->vars[$var_name] : NULL;
$this->is_modified = TRUE;
} }
} }
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) 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->vars[$var_name] = $var;
$this->vars_ts[$var_name] = time(); $this->var_expires[$var_name] = time() + $ttl;
$this->is_modified = TRUE; $this->is_modified = TRUE;
} }
}
function destroy($var_name, $table = '') function destroy($var_name, $table = '')
{ {
global $phpEx;
if ($var_name == 'sql' && !empty($table)) if ($var_name == 'sql' && !empty($table))
{ {
$regex = '(' . ((is_array($table)) ? implode('|', $table) : $table) . ')'; $regex = '(' . ((is_array($table)) ? implode('|', $table) : $table) . ')';
@ -133,33 +162,36 @@ class acm
} }
@closedir($dir); @closedir($dir);
} }
elseif ($var_name{0} == '_')
{
@unlink($this->cache_dir . 'data' . $var_name . ".$phpEx");
}
elseif (isset($this->vars[$var_name])) elseif (isset($this->vars[$var_name]))
{ {
$this->is_modified = TRUE; $this->is_modified = TRUE;
unset($this->vars[$var_name]); 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 ($var_name{0} == '_')
{
global $phpEx;
return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx");
}
else
{ {
if (!is_array($this->vars)) if (!is_array($this->vars))
{ {
$this->load(); $this->load();
} }
if ($max_age > 0 && isset($this->vars_ts[$var_name])) return (time() > $this->var_expires[$var_name]) ? FALSE : isset($this->vars[$var_name]);
{
if (time() > $this->vars_ts[$var_name] + $max_age)
{
$this->destroy($var_name);
return FALSE;
} }
} }
return isset($this->vars[$var_name]);
}
function format_array($array) function format_array($array)
{ {
$lines = array(); $lines = array();
@ -179,34 +211,35 @@ class acm
} }
else else
{ {
$lines[] = "'$k'=>'" . str_replace("'", "\'", str_replace('\\', '\\\\', $v)) . "'"; $lines[] = "'$k'=>'" . str_replace("'", "\\'", str_replace('\\', '\\\\', $v)) . "'";
} }
} }
return 'array(' . implode(',', $lines) . ')'; return 'array(' . implode(',', $lines) . ')';
} }
function sql_load($query, $max_age) function sql_load($query)
{ {
global $phpEx; global $phpEx;
// Remove extra spaces and tabs // Remove extra spaces and tabs
$query = preg_replace('/[\n\r\s\t]+/', ' ', $query); $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)); include($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
if (time() > $filemtime + $max_age) if (!isset($expired))
{ {
return FALSE; return FALSE;
} }
elseif ($expired)
$query_id = 'Cache id #' . count($this->sql_rowset); {
include($this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx); unlink($this->cache_dir . 'sql_' . md5($query) . ".$phpEx");
return FALSE;
// $this->sql_rowset[$query_id] =& $rowset; }
return $query_id; return $query_id;
} }
function sql_save($query, &$query_result) function sql_save($query, &$query_result, $ttl)
{ {
global $db, $phpEx; global $db, $phpEx;
@ -225,15 +258,11 @@ class acm
{ {
$this->sql_rowset[$query_id][] = $row; $this->sql_rowset[$query_id][] = $row;
$line = 'array('; $lines[] = "unserialize('" . str_replace("'", "\\'", str_replace('\\', '\\\\', serialize($row))) . "')";
foreach ($row as $key => $val)
{
$line .= "'$key'=>'" . str_replace("'", "\'", str_replace('\\', '\\\\', $val)) . "',";
}
$lines[] = substr($line, 0, -1) . ')';
} }
$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); @flock($fp, LOCK_UN);
fclose($fp); fclose($fp);