|
@@ -1,4 +1,4 @@
|
1
|
|
-<?
|
|
1
|
+<?php
|
2
|
2
|
/*************************************************************************|
|
3
|
3
|
|--------------- Caching class -------------------------------------------|
|
4
|
4
|
|*************************************************************************|
|
|
@@ -25,26 +25,31 @@ memcached -d -m 8192 -l 10.10.0.1 -t8 -C
|
25
|
25
|
|*************************************************************************/
|
26
|
26
|
|
27
|
27
|
if (!extension_loaded('memcache') && !extension_loaded('memcached')) {
|
28
|
|
- die('Memcache Extension not loaded.');
|
|
28
|
+ die('Memcache Extension not loaded.');
|
29
|
29
|
}
|
30
|
30
|
|
31
|
31
|
if (class_exists('Memcached')) {
|
32
|
|
- class MemcacheCompat extends Memcached {}
|
|
32
|
+ class MemcacheCompat extends Memcached
|
|
33
|
+ {
|
|
34
|
+ }
|
33
|
35
|
} else {
|
34
|
|
- class MemcacheCompat extends Memcache {}
|
|
36
|
+ class MemcacheCompat extends Memcache
|
|
37
|
+ {
|
|
38
|
+ }
|
35
|
39
|
}
|
36
|
40
|
|
37
|
|
-class Cache extends MemcacheCompat {
|
38
|
|
- // Torrent Group cache version
|
39
|
|
- const GROUP_VERSION = 5;
|
40
|
|
-
|
41
|
|
- public $CacheHits = [];
|
42
|
|
- public $MemcacheDBArray = [];
|
43
|
|
- public $MemcacheDBKey = '';
|
44
|
|
- protected $InTransaction = false;
|
45
|
|
- public $Time = 0;
|
46
|
|
- private $Servers = [];
|
47
|
|
- private $PersistentKeys = [
|
|
41
|
+class Cache extends MemcacheCompat
|
|
42
|
+{
|
|
43
|
+ // Torrent Group cache version
|
|
44
|
+ const GROUP_VERSION = 5;
|
|
45
|
+
|
|
46
|
+ public $CacheHits = [];
|
|
47
|
+ public $MemcacheDBArray = [];
|
|
48
|
+ public $MemcacheDBKey = '';
|
|
49
|
+ protected $InTransaction = false;
|
|
50
|
+ public $Time = 0;
|
|
51
|
+ private $Servers = [];
|
|
52
|
+ private $PersistentKeys = [
|
48
|
53
|
'ajax_requests_*',
|
49
|
54
|
'query_lock_*',
|
50
|
55
|
'stats_*',
|
|
@@ -54,362 +59,392 @@ class Cache extends MemcacheCompat {
|
54
|
59
|
// Cache-based features
|
55
|
60
|
'global_notification',
|
56
|
61
|
'notifications_one_reads_*',
|
57
|
|
- ];
|
58
|
|
- private $ClearedKeys = [];
|
59
|
|
-
|
60
|
|
- public $CanClear = false;
|
61
|
|
- public $InternalCache = true;
|
62
|
|
-
|
63
|
|
- function __construct($Servers) {
|
64
|
|
- if (is_subclass_of($this, 'Memcached')) parent::__construct();
|
65
|
|
- $this->Servers = $Servers;
|
66
|
|
- foreach ($Servers as $Server) {
|
67
|
|
- if (is_subclass_of($this, 'Memcache')) {
|
68
|
|
- $this->addServer($Server['host'], $Server['port'], true, $Server['buckets']);
|
69
|
|
- } else {
|
70
|
|
- $this->addServer(str_replace('unix://', '', $Server['host']), $Server['port'], $Server['buckets']);
|
71
|
|
- }
|
72
|
|
- }
|
73
|
|
- }
|
74
|
|
-
|
75
|
|
- //---------- Caching functions ----------//
|
76
|
|
-
|
77
|
|
- // Allows us to set an expiration on otherwise perminantly cache'd values
|
78
|
|
- // Useful for disabled users, locked threads, basically reducing ram usage
|
79
|
|
- public function expire_value($Key, $Duration = 2592000) {
|
80
|
|
- $StartTime = microtime(true);
|
81
|
|
- $this->set($Key, $this->get($Key), $Duration);
|
82
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
83
|
|
- }
|
84
|
|
-
|
85
|
|
- // Wrapper for Memcache::set, with the zlib option removed and default duration of 30 days
|
86
|
|
- public function cache_value($Key, $Value, $Duration = 2592000) {
|
87
|
|
- $StartTime = microtime(true);
|
88
|
|
- if (empty($Key)) {
|
89
|
|
- trigger_error("Cache insert failed for empty key");
|
90
|
|
- }
|
91
|
|
- $SetParams = [$Key, $Value, 0, $Duration];
|
92
|
|
- if (is_subclass_of($this, 'Memcached')) unset($SetParams[2]);
|
93
|
|
- if (!$this->set(...$SetParams)) {
|
94
|
|
- trigger_error("Cache insert failed for key $Key");
|
|
62
|
+ ];
|
|
63
|
+ private $ClearedKeys = [];
|
|
64
|
+
|
|
65
|
+ public $CanClear = false;
|
|
66
|
+ public $InternalCache = true;
|
|
67
|
+
|
|
68
|
+ public function __construct($Servers)
|
|
69
|
+ {
|
|
70
|
+ if (is_subclass_of($this, 'Memcached')) {
|
|
71
|
+ parent::__construct();
|
|
72
|
+ }
|
|
73
|
+ $this->Servers = $Servers;
|
|
74
|
+ foreach ($Servers as $Server) {
|
|
75
|
+ if (is_subclass_of($this, 'Memcache')) {
|
|
76
|
+ $this->addServer($Server['host'], $Server['port'], true, $Server['buckets']);
|
|
77
|
+ } else {
|
|
78
|
+ $this->addServer(str_replace('unix://', '', $Server['host']), $Server['port'], $Server['buckets']);
|
|
79
|
+ }
|
|
80
|
+ }
|
95
|
81
|
}
|
96
|
|
- if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
|
97
|
|
- $this->CacheHits[$Key] = $Value;
|
|
82
|
+
|
|
83
|
+ //---------- Caching functions ----------//
|
|
84
|
+
|
|
85
|
+ // Allows us to set an expiration on otherwise perminantly cache'd values
|
|
86
|
+ // Useful for disabled users, locked threads, basically reducing ram usage
|
|
87
|
+ public function expire_value($Key, $Duration = 2592000)
|
|
88
|
+ {
|
|
89
|
+ $StartTime = microtime(true);
|
|
90
|
+ $this->set($Key, $this->get($Key), $Duration);
|
|
91
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
98
|
92
|
}
|
99
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
100
|
|
- }
|
101
|
|
-
|
102
|
|
- // Wrapper for Memcache::add, with the zlib option removed and default duration of 30 days
|
103
|
|
- public function add_value($Key, $Value, $Duration = 2592000) {
|
104
|
|
- $StartTime = microtime(true);
|
105
|
|
- $AddParams = [$Key, $Value, false, $Duration];
|
106
|
|
- if (is_subclass_of($this, 'Memcached')) unset($AddParams[2]);
|
107
|
|
- $Added = $this->add(...$AddParams);
|
108
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
109
|
|
- return $Added;
|
110
|
|
- }
|
111
|
|
-
|
112
|
|
- public function replace_value($Key, $Value, $Duration = 2592000) {
|
113
|
|
- $StartTime = microtime(true);
|
114
|
|
- $ReplaceParams = [$Key, $Value, false, $Duration];
|
115
|
|
- if (is_subclass_of($this, 'Memcached')) unset($ReplaceParams[2]);
|
116
|
|
- $this->replace(...$ReplaceParams);
|
117
|
|
- if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
|
118
|
|
- $this->CacheHits[$Key] = $Value;
|
|
93
|
+
|
|
94
|
+ // Wrapper for Memcache::set, with the zlib option removed and default duration of 30 days
|
|
95
|
+ public function cache_value($Key, $Value, $Duration = 2592000)
|
|
96
|
+ {
|
|
97
|
+ $StartTime = microtime(true);
|
|
98
|
+ if (empty($Key)) {
|
|
99
|
+ trigger_error("Cache insert failed for empty key");
|
|
100
|
+ }
|
|
101
|
+ $SetParams = [$Key, $Value, 0, $Duration];
|
|
102
|
+ if (is_subclass_of($this, 'Memcached')) {
|
|
103
|
+ unset($SetParams[2]);
|
|
104
|
+ }
|
|
105
|
+ if (!$this->set(...$SetParams)) {
|
|
106
|
+ trigger_error("Cache insert failed for key $Key");
|
|
107
|
+ }
|
|
108
|
+ if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
|
|
109
|
+ $this->CacheHits[$Key] = $Value;
|
|
110
|
+ }
|
|
111
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
119
|
112
|
}
|
120
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
121
|
|
- }
|
122
|
113
|
|
123
|
|
- public function get_value($Key, $NoCache = false) {
|
124
|
|
- if (!$this->InternalCache) {
|
125
|
|
- $NoCache = true;
|
|
114
|
+ // Wrapper for Memcache::add, with the zlib option removed and default duration of 30 days
|
|
115
|
+ public function add_value($Key, $Value, $Duration = 2592000)
|
|
116
|
+ {
|
|
117
|
+ $StartTime = microtime(true);
|
|
118
|
+ $AddParams = [$Key, $Value, false, $Duration];
|
|
119
|
+ if (is_subclass_of($this, 'Memcached')) {
|
|
120
|
+ unset($AddParams[2]);
|
|
121
|
+ }
|
|
122
|
+ $Added = $this->add(...$AddParams);
|
|
123
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
|
124
|
+ return $Added;
|
126
|
125
|
}
|
127
|
|
- $StartTime = microtime(true);
|
128
|
|
- if (empty($Key)) {
|
129
|
|
- trigger_error('Cache retrieval failed for empty key');
|
|
126
|
+
|
|
127
|
+ public function replace_value($Key, $Value, $Duration = 2592000)
|
|
128
|
+ {
|
|
129
|
+ $StartTime = microtime(true);
|
|
130
|
+ $ReplaceParams = [$Key, $Value, false, $Duration];
|
|
131
|
+ if (is_subclass_of($this, 'Memcached')) {
|
|
132
|
+ unset($ReplaceParams[2]);
|
|
133
|
+ }
|
|
134
|
+ $this->replace(...$ReplaceParams);
|
|
135
|
+ if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
|
|
136
|
+ $this->CacheHits[$Key] = $Value;
|
|
137
|
+ }
|
|
138
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
130
|
139
|
}
|
131
|
140
|
|
132
|
|
- if (!empty($_GET['clearcache']) && $this->CanClear && !isset($this->ClearedKeys[$Key]) && !Misc::in_array_partial($Key, $this->PersistentKeys)) {
|
133
|
|
- if ($_GET['clearcache'] === '1') {
|
134
|
|
- // Because check_perms() isn't true until LoggedUser is pulled from the cache, we have to remove the entries loaded before the LoggedUser data
|
135
|
|
- // Because of this, not user cache data will require a secondary pageload following the clearcache to update
|
136
|
|
- if (count($this->CacheHits) > 0) {
|
137
|
|
- foreach (array_keys($this->CacheHits) as $HitKey) {
|
138
|
|
- if (!isset($this->ClearedKeys[$HitKey]) && !Misc::in_array_partial($HitKey, $this->PersistentKeys)) {
|
139
|
|
- $this->delete($HitKey);
|
140
|
|
- unset($this->CacheHits[$HitKey]);
|
141
|
|
- $this->ClearedKeys[$HitKey] = true;
|
|
141
|
+ public function get_value($Key, $NoCache = false)
|
|
142
|
+ {
|
|
143
|
+ if (!$this->InternalCache) {
|
|
144
|
+ $NoCache = true;
|
|
145
|
+ }
|
|
146
|
+ $StartTime = microtime(true);
|
|
147
|
+ if (empty($Key)) {
|
|
148
|
+ trigger_error('Cache retrieval failed for empty key');
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ if (!empty($_GET['clearcache']) && $this->CanClear && !isset($this->ClearedKeys[$Key]) && !Misc::in_array_partial($Key, $this->PersistentKeys)) {
|
|
152
|
+ if ($_GET['clearcache'] === '1') {
|
|
153
|
+ // Because check_perms() isn't true until LoggedUser is pulled from the cache, we have to remove the entries loaded before the LoggedUser data
|
|
154
|
+ // Because of this, not user cache data will require a secondary pageload following the clearcache to update
|
|
155
|
+ if (count($this->CacheHits) > 0) {
|
|
156
|
+ foreach (array_keys($this->CacheHits) as $HitKey) {
|
|
157
|
+ if (!isset($this->ClearedKeys[$HitKey]) && !Misc::in_array_partial($HitKey, $this->PersistentKeys)) {
|
|
158
|
+ $this->delete($HitKey);
|
|
159
|
+ unset($this->CacheHits[$HitKey]);
|
|
160
|
+ $this->ClearedKeys[$HitKey] = true;
|
|
161
|
+ }
|
|
162
|
+ }
|
|
163
|
+ }
|
|
164
|
+ $this->delete($Key);
|
|
165
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
|
166
|
+ return false;
|
|
167
|
+ } elseif ($_GET['clearcache'] == $Key) {
|
|
168
|
+ $this->delete($Key);
|
|
169
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
|
170
|
+ return false;
|
|
171
|
+ } elseif (substr($_GET['clearcache'], -1) === '*') {
|
|
172
|
+ $Prefix = substr($_GET['clearcache'], 0, -1);
|
|
173
|
+ if ($Prefix === '' || $Prefix === substr($Key, 0, strlen($Prefix))) {
|
|
174
|
+ $this->delete($Key);
|
|
175
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
|
176
|
+ return false;
|
|
177
|
+ }
|
142
|
178
|
}
|
143
|
|
- }
|
|
179
|
+ $this->ClearedKeys[$Key] = true;
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+ // For cases like the forums, if a key is already loaded, grab the existing pointer
|
|
183
|
+ if (isset($this->CacheHits[$Key]) && !$NoCache) {
|
|
184
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
|
185
|
+ return $this->CacheHits[$Key];
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ $Return = $this->get($Key);
|
|
189
|
+ if ($Return !== false) {
|
|
190
|
+ $this->CacheHits[$Key] = $NoCache ? null : $Return;
|
144
|
191
|
}
|
145
|
|
- $this->delete($Key);
|
146
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
147
|
|
- return false;
|
148
|
|
- } elseif ($_GET['clearcache'] == $Key) {
|
149
|
|
- $this->delete($Key);
|
150
|
192
|
$this->Time += (microtime(true) - $StartTime) * 1000;
|
151
|
|
- return false;
|
152
|
|
- } elseif (substr($_GET['clearcache'], -1) === '*') {
|
153
|
|
- $Prefix = substr($_GET['clearcache'], 0, -1);
|
154
|
|
- if ($Prefix === '' || $Prefix === substr($Key, 0, strlen($Prefix))) {
|
155
|
|
- $this->delete($Key);
|
156
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
157
|
|
- return false;
|
158
|
|
- }
|
159
|
|
- }
|
160
|
|
- $this->ClearedKeys[$Key] = true;
|
|
193
|
+ return $Return;
|
161
|
194
|
}
|
162
|
195
|
|
163
|
|
- // For cases like the forums, if a key is already loaded, grab the existing pointer
|
164
|
|
- if (isset($this->CacheHits[$Key]) && !$NoCache) {
|
165
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
166
|
|
- return $this->CacheHits[$Key];
|
|
196
|
+ // Wrapper for Memcache::delete. For a reason, see above.
|
|
197
|
+ public function delete_value($Key)
|
|
198
|
+ {
|
|
199
|
+ $StartTime = microtime(true);
|
|
200
|
+ if (empty($Key)) {
|
|
201
|
+ trigger_error('Cache deletion failed for empty key');
|
|
202
|
+ }
|
|
203
|
+ if (!$this->delete($Key)) {
|
|
204
|
+ //trigger_error("Cache delete failed for key $Key");
|
|
205
|
+ }
|
|
206
|
+ unset($this->CacheHits[$Key]);
|
|
207
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
167
|
208
|
}
|
168
|
209
|
|
169
|
|
- $Return = $this->get($Key);
|
170
|
|
- if ($Return !== false) {
|
171
|
|
- $this->CacheHits[$Key] = $NoCache ? null : $Return;
|
172
|
|
- }
|
173
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
174
|
|
- return $Return;
|
175
|
|
- }
|
176
|
|
-
|
177
|
|
- // Wrapper for Memcache::delete. For a reason, see above.
|
178
|
|
- public function delete_value($Key) {
|
179
|
|
- $StartTime = microtime(true);
|
180
|
|
- if (empty($Key)) {
|
181
|
|
- trigger_error('Cache deletion failed for empty key');
|
182
|
|
- }
|
183
|
|
- if (!$this->delete($Key)) {
|
184
|
|
- //trigger_error("Cache delete failed for key $Key");
|
185
|
|
- }
|
186
|
|
- unset($this->CacheHits[$Key]);
|
187
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
188
|
|
- }
|
189
|
|
-
|
190
|
|
- public function increment_value($Key, $Value = 1) {
|
191
|
|
- $StartTime = microtime(true);
|
192
|
|
- $NewVal = $this->increment($Key, $Value);
|
193
|
|
- if (isset($this->CacheHits[$Key])) {
|
194
|
|
- $this->CacheHits[$Key] = $NewVal;
|
195
|
|
- }
|
196
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
197
|
|
- }
|
198
|
|
-
|
199
|
|
- public function decrement_value($Key, $Value = 1) {
|
200
|
|
- $StartTime = microtime(true);
|
201
|
|
- $NewVal = $this->decrement($Key, $Value);
|
202
|
|
- if (isset($this->CacheHits[$Key])) {
|
203
|
|
- $this->CacheHits[$Key] = $NewVal;
|
204
|
|
- }
|
205
|
|
- $this->Time += (microtime(true) - $StartTime) * 1000;
|
206
|
|
- }
|
207
|
|
-
|
208
|
|
- //---------- memcachedb functions ----------//
|
209
|
|
-
|
210
|
|
- public function begin_transaction($Key) {
|
211
|
|
- $Value = $this->get($Key);
|
212
|
|
- if (!is_array($Value)) {
|
213
|
|
- $this->InTransaction = false;
|
214
|
|
- $this->MemcacheDBKey = [];
|
215
|
|
- $this->MemcacheDBKey = '';
|
216
|
|
- return false;
|
217
|
|
- }
|
218
|
|
- $this->MemcacheDBArray = $Value;
|
219
|
|
- $this->MemcacheDBKey = $Key;
|
220
|
|
- $this->InTransaction = true;
|
221
|
|
- return true;
|
222
|
|
- }
|
223
|
|
-
|
224
|
|
- public function cancel_transaction() {
|
225
|
|
- $this->InTransaction = false;
|
226
|
|
- $this->MemcacheDBKey = [];
|
227
|
|
- $this->MemcacheDBKey = '';
|
228
|
|
- }
|
229
|
|
-
|
230
|
|
- public function commit_transaction($Time = 2592000) {
|
231
|
|
- if (!$this->InTransaction) {
|
232
|
|
- return false;
|
233
|
|
- }
|
234
|
|
- $this->cache_value($this->MemcacheDBKey, $this->MemcacheDBArray, $Time);
|
235
|
|
- $this->InTransaction = false;
|
236
|
|
- }
|
237
|
|
-
|
238
|
|
- // Updates multiple rows in an array
|
239
|
|
- public function update_transaction($Rows, $Values) {
|
240
|
|
- if (!$this->InTransaction) {
|
241
|
|
- return false;
|
242
|
|
- }
|
243
|
|
- $Array = $this->MemcacheDBArray;
|
244
|
|
- if (is_array($Rows)) {
|
245
|
|
- $i = 0;
|
246
|
|
- $Keys = $Rows[0];
|
247
|
|
- $Property = $Rows[1];
|
248
|
|
- foreach ($Keys as $Row) {
|
249
|
|
- $Array[$Row][$Property] = $Values[$i];
|
250
|
|
- $i++;
|
251
|
|
- }
|
252
|
|
- } else {
|
253
|
|
- $Array[$Rows] = $Values;
|
254
|
|
- }
|
255
|
|
- $this->MemcacheDBArray = $Array;
|
256
|
|
- }
|
257
|
|
-
|
258
|
|
- // Updates multiple values in a single row in an array
|
259
|
|
- // $Values must be an associative array with key:value pairs like in the array we're updating
|
260
|
|
- public function update_row($Row, $Values) {
|
261
|
|
- if (!$this->InTransaction) {
|
262
|
|
- return false;
|
263
|
|
- }
|
264
|
|
- if ($Row === false) {
|
265
|
|
- $UpdateArray = $this->MemcacheDBArray;
|
266
|
|
- } else {
|
267
|
|
- $UpdateArray = $this->MemcacheDBArray[$Row];
|
268
|
|
- }
|
269
|
|
- foreach ($Values as $Key => $Value) {
|
270
|
|
- if (!array_key_exists($Key, $UpdateArray)) {
|
271
|
|
- trigger_error('Bad transaction key ('.$Key.') for cache '.$this->MemcacheDBKey);
|
272
|
|
- }
|
273
|
|
- if ($Value === '+1') {
|
274
|
|
- if (!is_number($UpdateArray[$Key])) {
|
275
|
|
- trigger_error('Tried to increment non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
|
276
|
|
- }
|
277
|
|
- ++$UpdateArray[$Key]; // Increment value
|
278
|
|
- } elseif ($Value === '-1') {
|
279
|
|
- if (!is_number($UpdateArray[$Key])) {
|
280
|
|
- trigger_error('Tried to decrement non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
|
281
|
|
- }
|
282
|
|
- --$UpdateArray[$Key]; // Decrement value
|
283
|
|
- } else {
|
284
|
|
- $UpdateArray[$Key] = $Value; // Otherwise, just alter value
|
285
|
|
- }
|
286
|
|
- }
|
287
|
|
- if ($Row === false) {
|
288
|
|
- $this->MemcacheDBArray = $UpdateArray;
|
289
|
|
- } else {
|
290
|
|
- $this->MemcacheDBArray[$Row] = $UpdateArray;
|
|
210
|
+ public function increment_value($Key, $Value = 1)
|
|
211
|
+ {
|
|
212
|
+ $StartTime = microtime(true);
|
|
213
|
+ $NewVal = $this->increment($Key, $Value);
|
|
214
|
+ if (isset($this->CacheHits[$Key])) {
|
|
215
|
+ $this->CacheHits[$Key] = $NewVal;
|
|
216
|
+ }
|
|
217
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
291
|
218
|
}
|
292
|
|
- }
|
293
|
219
|
|
294
|
|
- // Increments multiple values in a single row in an array
|
295
|
|
- // $Values must be an associative array with key:value pairs like in the array we're updating
|
296
|
|
- public function increment_row($Row, $Values) {
|
297
|
|
- if (!$this->InTransaction) {
|
298
|
|
- return false;
|
299
|
|
- }
|
300
|
|
- if ($Row === false) {
|
301
|
|
- $UpdateArray = $this->MemcacheDBArray;
|
302
|
|
- } else {
|
303
|
|
- $UpdateArray = $this->MemcacheDBArray[$Row];
|
304
|
|
- }
|
305
|
|
- foreach ($Values as $Key => $Value) {
|
306
|
|
- if (!array_key_exists($Key, $UpdateArray)) {
|
307
|
|
- trigger_error("Bad transaction key ($Key) for cache ".$this->MemcacheDBKey);
|
308
|
|
- }
|
309
|
|
- if (!is_number($Value)) {
|
310
|
|
- trigger_error("Tried to increment with non-number ($Key) for cache ".$this->MemcacheDBKey);
|
311
|
|
- }
|
312
|
|
- $UpdateArray[$Key] += $Value; // Increment value
|
313
|
|
- }
|
314
|
|
- if ($Row === false) {
|
315
|
|
- $this->MemcacheDBArray = $UpdateArray;
|
316
|
|
- } else {
|
317
|
|
- $this->MemcacheDBArray[$Row] = $UpdateArray;
|
|
220
|
+ public function decrement_value($Key, $Value = 1)
|
|
221
|
+ {
|
|
222
|
+ $StartTime = microtime(true);
|
|
223
|
+ $NewVal = $this->decrement($Key, $Value);
|
|
224
|
+ if (isset($this->CacheHits[$Key])) {
|
|
225
|
+ $this->CacheHits[$Key] = $NewVal;
|
|
226
|
+ }
|
|
227
|
+ $this->Time += (microtime(true) - $StartTime) * 1000;
|
318
|
228
|
}
|
319
|
|
- }
|
320
|
229
|
|
321
|
|
- // Insert a value at the beginning of the array
|
322
|
|
- public function insert_front($Key, $Value) {
|
323
|
|
- if (!$this->InTransaction) {
|
324
|
|
- return false;
|
|
230
|
+ //---------- memcachedb functions ----------//
|
|
231
|
+
|
|
232
|
+ public function begin_transaction($Key)
|
|
233
|
+ {
|
|
234
|
+ $Value = $this->get($Key);
|
|
235
|
+ if (!is_array($Value)) {
|
|
236
|
+ $this->InTransaction = false;
|
|
237
|
+ $this->MemcacheDBKey = [];
|
|
238
|
+ $this->MemcacheDBKey = '';
|
|
239
|
+ return false;
|
|
240
|
+ }
|
|
241
|
+ $this->MemcacheDBArray = $Value;
|
|
242
|
+ $this->MemcacheDBKey = $Key;
|
|
243
|
+ $this->InTransaction = true;
|
|
244
|
+ return true;
|
325
|
245
|
}
|
326
|
|
- if ($Key === '') {
|
327
|
|
- array_unshift($this->MemcacheDBArray, $Value);
|
328
|
|
- } else {
|
329
|
|
- $this->MemcacheDBArray = array($Key=>$Value) + $this->MemcacheDBArray;
|
|
246
|
+
|
|
247
|
+ public function cancel_transaction()
|
|
248
|
+ {
|
|
249
|
+ $this->InTransaction = false;
|
|
250
|
+ $this->MemcacheDBKey = [];
|
|
251
|
+ $this->MemcacheDBKey = '';
|
330
|
252
|
}
|
331
|
|
- }
|
332
|
253
|
|
333
|
|
- // Insert a value at the end of the array
|
334
|
|
- public function insert_back($Key, $Value) {
|
335
|
|
- if (!$this->InTransaction) {
|
336
|
|
- return false;
|
|
254
|
+ public function commit_transaction($Time = 2592000)
|
|
255
|
+ {
|
|
256
|
+ if (!$this->InTransaction) {
|
|
257
|
+ return false;
|
|
258
|
+ }
|
|
259
|
+ $this->cache_value($this->MemcacheDBKey, $this->MemcacheDBArray, $Time);
|
|
260
|
+ $this->InTransaction = false;
|
337
|
261
|
}
|
338
|
|
- if ($Key === '') {
|
339
|
|
- array_push($this->MemcacheDBArray, $Value);
|
340
|
|
- } else {
|
341
|
|
- $this->MemcacheDBArray = $this->MemcacheDBArray + array($Key=>$Value);
|
|
262
|
+
|
|
263
|
+ // Updates multiple rows in an array
|
|
264
|
+ public function update_transaction($Rows, $Values)
|
|
265
|
+ {
|
|
266
|
+ if (!$this->InTransaction) {
|
|
267
|
+ return false;
|
|
268
|
+ }
|
|
269
|
+ $Array = $this->MemcacheDBArray;
|
|
270
|
+ if (is_array($Rows)) {
|
|
271
|
+ $i = 0;
|
|
272
|
+ $Keys = $Rows[0];
|
|
273
|
+ $Property = $Rows[1];
|
|
274
|
+ foreach ($Keys as $Row) {
|
|
275
|
+ $Array[$Row][$Property] = $Values[$i];
|
|
276
|
+ $i++;
|
|
277
|
+ }
|
|
278
|
+ } else {
|
|
279
|
+ $Array[$Rows] = $Values;
|
|
280
|
+ }
|
|
281
|
+ $this->MemcacheDBArray = $Array;
|
342
|
282
|
}
|
343
|
283
|
|
344
|
|
- }
|
|
284
|
+ // Updates multiple values in a single row in an array
|
|
285
|
+ // $Values must be an associative array with key:value pairs like in the array we're updating
|
|
286
|
+ public function update_row($Row, $Values)
|
|
287
|
+ {
|
|
288
|
+ if (!$this->InTransaction) {
|
|
289
|
+ return false;
|
|
290
|
+ }
|
|
291
|
+ if ($Row === false) {
|
|
292
|
+ $UpdateArray = $this->MemcacheDBArray;
|
|
293
|
+ } else {
|
|
294
|
+ $UpdateArray = $this->MemcacheDBArray[$Row];
|
|
295
|
+ }
|
|
296
|
+ foreach ($Values as $Key => $Value) {
|
|
297
|
+ if (!array_key_exists($Key, $UpdateArray)) {
|
|
298
|
+ trigger_error('Bad transaction key ('.$Key.') for cache '.$this->MemcacheDBKey);
|
|
299
|
+ }
|
|
300
|
+ if ($Value === '+1') {
|
|
301
|
+ if (!is_number($UpdateArray[$Key])) {
|
|
302
|
+ trigger_error('Tried to increment non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
|
|
303
|
+ }
|
|
304
|
+ ++$UpdateArray[$Key]; // Increment value
|
|
305
|
+ } elseif ($Value === '-1') {
|
|
306
|
+ if (!is_number($UpdateArray[$Key])) {
|
|
307
|
+ trigger_error('Tried to decrement non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
|
|
308
|
+ }
|
|
309
|
+ --$UpdateArray[$Key]; // Decrement value
|
|
310
|
+ } else {
|
|
311
|
+ $UpdateArray[$Key] = $Value; // Otherwise, just alter value
|
|
312
|
+ }
|
|
313
|
+ }
|
|
314
|
+ if ($Row === false) {
|
|
315
|
+ $this->MemcacheDBArray = $UpdateArray;
|
|
316
|
+ } else {
|
|
317
|
+ $this->MemcacheDBArray[$Row] = $UpdateArray;
|
|
318
|
+ }
|
|
319
|
+ }
|
345
|
320
|
|
346
|
|
- public function insert($Key, $Value) {
|
347
|
|
- if (!$this->InTransaction) {
|
348
|
|
- return false;
|
|
321
|
+ // Increments multiple values in a single row in an array
|
|
322
|
+ // $Values must be an associative array with key:value pairs like in the array we're updating
|
|
323
|
+ public function increment_row($Row, $Values)
|
|
324
|
+ {
|
|
325
|
+ if (!$this->InTransaction) {
|
|
326
|
+ return false;
|
|
327
|
+ }
|
|
328
|
+ if ($Row === false) {
|
|
329
|
+ $UpdateArray = $this->MemcacheDBArray;
|
|
330
|
+ } else {
|
|
331
|
+ $UpdateArray = $this->MemcacheDBArray[$Row];
|
|
332
|
+ }
|
|
333
|
+ foreach ($Values as $Key => $Value) {
|
|
334
|
+ if (!array_key_exists($Key, $UpdateArray)) {
|
|
335
|
+ trigger_error("Bad transaction key ($Key) for cache ".$this->MemcacheDBKey);
|
|
336
|
+ }
|
|
337
|
+ if (!is_number($Value)) {
|
|
338
|
+ trigger_error("Tried to increment with non-number ($Key) for cache ".$this->MemcacheDBKey);
|
|
339
|
+ }
|
|
340
|
+ $UpdateArray[$Key] += $Value; // Increment value
|
|
341
|
+ }
|
|
342
|
+ if ($Row === false) {
|
|
343
|
+ $this->MemcacheDBArray = $UpdateArray;
|
|
344
|
+ } else {
|
|
345
|
+ $this->MemcacheDBArray[$Row] = $UpdateArray;
|
|
346
|
+ }
|
349
|
347
|
}
|
350
|
|
- if ($Key === '') {
|
351
|
|
- $this->MemcacheDBArray[] = $Value;
|
352
|
|
- } else {
|
353
|
|
- $this->MemcacheDBArray[$Key] = $Value;
|
|
348
|
+
|
|
349
|
+ // Insert a value at the beginning of the array
|
|
350
|
+ public function insert_front($Key, $Value)
|
|
351
|
+ {
|
|
352
|
+ if (!$this->InTransaction) {
|
|
353
|
+ return false;
|
|
354
|
+ }
|
|
355
|
+ if ($Key === '') {
|
|
356
|
+ array_unshift($this->MemcacheDBArray, $Value);
|
|
357
|
+ } else {
|
|
358
|
+ $this->MemcacheDBArray = array($Key=>$Value) + $this->MemcacheDBArray;
|
|
359
|
+ }
|
354
|
360
|
}
|
355
|
|
- }
|
356
|
361
|
|
357
|
|
- public function delete_row($Row) {
|
358
|
|
- if (!$this->InTransaction) {
|
359
|
|
- return false;
|
|
362
|
+ // Insert a value at the end of the array
|
|
363
|
+ public function insert_back($Key, $Value)
|
|
364
|
+ {
|
|
365
|
+ if (!$this->InTransaction) {
|
|
366
|
+ return false;
|
|
367
|
+ }
|
|
368
|
+ if ($Key === '') {
|
|
369
|
+ array_push($this->MemcacheDBArray, $Value);
|
|
370
|
+ } else {
|
|
371
|
+ $this->MemcacheDBArray = $this->MemcacheDBArray + array($Key=>$Value);
|
|
372
|
+ }
|
360
|
373
|
}
|
361
|
|
- if (!isset($this->MemcacheDBArray[$Row])) {
|
362
|
|
- trigger_error("Tried to delete non-existent row ($Row) for cache ".$this->MemcacheDBKey);
|
|
374
|
+
|
|
375
|
+ public function insert($Key, $Value)
|
|
376
|
+ {
|
|
377
|
+ if (!$this->InTransaction) {
|
|
378
|
+ return false;
|
|
379
|
+ }
|
|
380
|
+ if ($Key === '') {
|
|
381
|
+ $this->MemcacheDBArray[] = $Value;
|
|
382
|
+ } else {
|
|
383
|
+ $this->MemcacheDBArray[$Key] = $Value;
|
|
384
|
+ }
|
363
|
385
|
}
|
364
|
|
- unset($this->MemcacheDBArray[$Row]);
|
365
|
|
- }
|
366
|
|
-
|
367
|
|
- public function update($Key, $Rows, $Values, $Time = 2592000) {
|
368
|
|
- if (!$this->InTransaction) {
|
369
|
|
- $this->begin_transaction($Key);
|
370
|
|
- $this->update_transaction($Rows, $Values);
|
371
|
|
- $this->commit_transaction($Time);
|
372
|
|
- } else {
|
373
|
|
- $this->update_transaction($Rows, $Values);
|
|
386
|
+
|
|
387
|
+ public function delete_row($Row)
|
|
388
|
+ {
|
|
389
|
+ if (!$this->InTransaction) {
|
|
390
|
+ return false;
|
|
391
|
+ }
|
|
392
|
+ if (!isset($this->MemcacheDBArray[$Row])) {
|
|
393
|
+ trigger_error("Tried to delete non-existent row ($Row) for cache ".$this->MemcacheDBKey);
|
|
394
|
+ }
|
|
395
|
+ unset($this->MemcacheDBArray[$Row]);
|
374
|
396
|
}
|
375
|
|
- }
|
376
|
|
-
|
377
|
|
- /**
|
378
|
|
- * Tries to set a lock. Expiry time is one hour to avoid indefinite locks
|
379
|
|
- *
|
380
|
|
- * @param string $LockName name on the lock
|
381
|
|
- * @return true if lock was acquired
|
382
|
|
- */
|
383
|
|
- public function get_query_lock($LockName) {
|
384
|
|
- return $this->add_value('query_lock_'.$LockName, 1, 3600);
|
385
|
|
- }
|
386
|
|
-
|
387
|
|
- /**
|
388
|
|
- * Remove lock
|
389
|
|
- *
|
390
|
|
- * @param string $LockName name on the lock
|
391
|
|
- */
|
392
|
|
- public function clear_query_lock($LockName) {
|
393
|
|
- $this->delete_value('query_lock_'.$LockName);
|
394
|
|
- }
|
395
|
|
-
|
396
|
|
- /**
|
397
|
|
- * Get cache server status
|
398
|
|
- *
|
399
|
|
- * @return array (host => int status, ...)
|
400
|
|
- */
|
401
|
|
- public function server_status() {
|
402
|
|
- $Status = [];
|
403
|
|
- if (is_subclass_of($this, 'Memcached')) {
|
404
|
|
- $MemcachedStats = $this->getStats();
|
|
397
|
+
|
|
398
|
+ public function update($Key, $Rows, $Values, $Time = 2592000)
|
|
399
|
+ {
|
|
400
|
+ if (!$this->InTransaction) {
|
|
401
|
+ $this->begin_transaction($Key);
|
|
402
|
+ $this->update_transaction($Rows, $Values);
|
|
403
|
+ $this->commit_transaction($Time);
|
|
404
|
+ } else {
|
|
405
|
+ $this->update_transaction($Rows, $Values);
|
|
406
|
+ }
|
405
|
407
|
}
|
406
|
|
- foreach ($this->Servers as $Server) {
|
407
|
|
- if (is_subclass_of($this, 'Memcached')) {
|
408
|
|
- $Status["$Server[host]:$Server[port]"] = gettype($MemcachedStats["$Server[host]:$Server[port]"]) == 'array' ? 1 : 0;
|
409
|
|
- } else {
|
410
|
|
- $Status["$Server[host]:$Server[port]"] = $this->getServerStatus($Server['host'], $Server['port']);
|
411
|
|
- }
|
|
408
|
+
|
|
409
|
+ /**
|
|
410
|
+ * Tries to set a lock. Expiry time is one hour to avoid indefinite locks
|
|
411
|
+ *
|
|
412
|
+ * @param string $LockName name on the lock
|
|
413
|
+ * @return true if lock was acquired
|
|
414
|
+ */
|
|
415
|
+ public function get_query_lock($LockName)
|
|
416
|
+ {
|
|
417
|
+ return $this->add_value('query_lock_'.$LockName, 1, 3600);
|
|
418
|
+ }
|
|
419
|
+
|
|
420
|
+ /**
|
|
421
|
+ * Remove lock
|
|
422
|
+ *
|
|
423
|
+ * @param string $LockName name on the lock
|
|
424
|
+ */
|
|
425
|
+ public function clear_query_lock($LockName)
|
|
426
|
+ {
|
|
427
|
+ $this->delete_value('query_lock_'.$LockName);
|
|
428
|
+ }
|
|
429
|
+
|
|
430
|
+ /**
|
|
431
|
+ * Get cache server status
|
|
432
|
+ *
|
|
433
|
+ * @return array (host => int status, ...)
|
|
434
|
+ */
|
|
435
|
+ public function server_status()
|
|
436
|
+ {
|
|
437
|
+ $Status = [];
|
|
438
|
+ if (is_subclass_of($this, 'Memcached')) {
|
|
439
|
+ $MemcachedStats = $this->getStats();
|
|
440
|
+ }
|
|
441
|
+ foreach ($this->Servers as $Server) {
|
|
442
|
+ if (is_subclass_of($this, 'Memcached')) {
|
|
443
|
+ $Status["$Server[host]:$Server[port]"] = gettype($MemcachedStats["$Server[host]:$Server[port]"]) == 'array' ? 1 : 0;
|
|
444
|
+ } else {
|
|
445
|
+ $Status["$Server[host]:$Server[port]"] = $this->getServerStatus($Server['host'], $Server['port']);
|
|
446
|
+ }
|
|
447
|
+ }
|
|
448
|
+ return $Status;
|
412
|
449
|
}
|
413
|
|
- return $Status;
|
414
|
|
- }
|
415
|
450
|
}
|