Browse Source

Look over the cache class

pjc 5 years ago
parent
commit
f782031a7b
1 changed files with 370 additions and 337 deletions
  1. 370
    337
      classes/cache.class.php

+ 370
- 337
classes/cache.class.php View File

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

Loading…
Cancel
Save