1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
<?php
/***************************************************************************
* acm_db.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.
*
***************************************************************************/
class acm
{
var $db;
var $is_modified = FALSE;
var $vars = '';
var $sql_enabled = FALSE;
function acm(&$db)
{
$this->db =& $db;
}
function load($var_names = '')
{
$this->vars = array();
$sql = 'SELECT var_name, var_ts, var_data
FROM ' . CACHE_TABLE;
if (!empty($var_names))
{
$sql .= " WHERE var_name IN ('" . implode("', '", $var_names) . "')";
}
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$this->vars[$row['var_name']] = array(
'data' => unserialize($row['var_data']),
'ts' => intval($row['var_ts'])
);
}
}
function unload()
{
$this->save();
unset($this->vars);
}
function save()
{
if (!$this->is_modified)
{
return;
}
$delete = $insert = array();
foreach ($this->vars as $var_name => $var_ary)
{
if (!empty($var_ary['is_modified']))
{
if (!empty($var_ary['delete']))
{
$delete[] = $var_name;
}
else
{
$delete[] = $var_name;
$insert[] = "'$var_name', " . time() . ", '" . $this->db->sql_escape(serialize($var_ary['data'])) . "'";
}
$this->db->sql_query($sql);
}
}
if (count($delete))
{
$sql = 'DELETE FROM ' . CACHE_TABLE . "
WHERE var_name IN ('" . implode("', '", $delete) . "')";
$this->db->sql_query($sql);
}
if (count($insert))
{
switch (SQL_LAYER)
{
case 'mysql':
$sql = 'INSERT INTO ' . CACHE_TABLE . ' (var_name, var_ts, var_data)
VALUES (' . implode('), (', $insert) . ')';
$this->db->sql_query($sql);
break;
default:
foreach ($insert as $values)
{
$sql = 'INSERT INTO ' . CACHE_TABLE . " (var_name, var_ts, var_data)
VALUES ($values)";
$this->db->sql_query($sql);
}
}
}
$this->is_modified = FALSE;
}
function tidy($max_age = 0)
{
$sql = 'DELETE FROM ' . CACHE_TABLE . '
WHERE var_ts < ' . (time() - $max_age);
$this->db->sql_query($sql);
}
function get($var_name, $max_age = 0)
{
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)
{
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))
{
$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]);
}
}
?>
|