Browse Source

Add compatibility with php-memcached

spaghetti 7 years ago
parent
commit
cb1f6ad1d2
5 changed files with 28 additions and 15 deletions
  1. 22
    9
      classes/cache.class.php
  2. 1
    1
      classes/script_start.php
  3. 2
    2
      classes/torrents.class.php
  4. 2
    2
      feeds.php
  5. 1
    1
      sections/peerupdate/index.php

+ 22
- 9
classes/cache.class.php View File

@@ -12,7 +12,7 @@ however, this class has page caching functions superior to those of
12 12
 memcache.
13 13
 
14 14
 Also, Memcache::get and Memcache::set have been wrapped by
15
-CACHE::get_value and CACHE::cache_value. get_value uses the same argument
15
+Cache::get_value and Cache::cache_value. get_value uses the same argument
16 16
 as get, but cache_value only takes the key, the value, and the duration
17 17
 (no zlib).
18 18
 
@@ -24,14 +24,18 @@ memcached -d -m 8192 -l 10.10.0.1 -t8 -C
24 24
 
25 25
 |*************************************************************************/
26 26
 
27
-if (!extension_loaded('memcache')) {
27
+if (!extension_loaded('memcache') && !extension_loaded('memcached')) {
28 28
   die('Memcache Extension not loaded.');
29 29
 }
30 30
 
31
-class CACHE extends Memcache {
32
-  /**
33
-   * Torrent Group cache version
34
-   */
31
+if (class_exists('Memcached')) {
32
+  class MemcacheCompat extends Memcached {}
33
+} else {
34
+  class MemcacheCompat extends Memcache {}
35
+}
36
+
37
+class Cache extends MemcacheCompat {
38
+  // Torrent Group cache version
35 39
   const GROUP_VERSION = 5;
36 40
 
37 41
   public $CacheHits = [];
@@ -57,9 +61,14 @@ class CACHE extends Memcache {
57 61
   public $InternalCache = true;
58 62
 
59 63
   function __construct($Servers) {
64
+    if (is_subclass_of($this, 'Memcached')) parent::__construct();
60 65
     $this->Servers = $Servers;
61 66
     foreach ($Servers as $Server) {
62
-      $this->addServer($Server['host'], $Server['port'], true, $Server['buckets']);
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
+      }
63 72
     }
64 73
   }
65 74
 
@@ -79,7 +88,9 @@ class CACHE extends Memcache {
79 88
     if (empty($Key)) {
80 89
       trigger_error("Cache insert failed for empty key");
81 90
     }
82
-    if (!$this->set($Key, $Value, 0, $Duration)) {
91
+    $SetParams = [$Key, $Value, 0, $Duration];
92
+    if (is_subclass_of($this, 'Memcached')) unset($SetParams[2]);
93
+    if (!$this->set(...$SetParams)) {
83 94
       trigger_error("Cache insert failed for key $Key");
84 95
     }
85 96
     if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
@@ -98,7 +109,9 @@ class CACHE extends Memcache {
98 109
 
99 110
   public function replace_value($Key, $Value, $Duration = 2592000) {
100 111
     $StartTime = microtime(true);
101
-    $this->replace($Key, $Value, false, $Duration);
112
+    $ReplaceParams = [$Key, $Value, false, $Duration];
113
+    if (is_subclass_of($this, 'Memcached')) unset($ReplaceParams[2]);
114
+    $this->replace(...$ReplaceParams);
102 115
     if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
103 116
       $this->CacheHits[$Key] = $Value;
104 117
     }

+ 1
- 1
classes/script_start.php View File

@@ -66,7 +66,7 @@ $Debug->handle_errors();
66 66
 $Debug->set_flag('Debug constructed');
67 67
 
68 68
 $DB = new DB_MYSQL;
69
-$Cache = new CACHE(MEMCACHED_SERVERS);
69
+$Cache = new Cache(MEMCACHED_SERVERS);
70 70
 
71 71
 // Autoload classes.
72 72
 require(SERVER_ROOT.'/classes/classloader.php');

+ 2
- 2
classes/torrents.class.php View File

@@ -57,7 +57,7 @@ class Torrents {
57 57
         continue;
58 58
       }
59 59
       $Data = G::$Cache->get_value($Key . $GroupID, true);
60
-      if (!empty($Data) && is_array($Data) && $Data['ver'] == CACHE::GROUP_VERSION) {
60
+      if (!empty($Data) && is_array($Data) && $Data['ver'] == Cache::GROUP_VERSION) {
61 61
         unset($NotFound[$GroupID]);
62 62
         $Found[$GroupID] = $Data['d'];
63 63
       }
@@ -126,7 +126,7 @@ class Torrents {
126 126
       }
127 127
 
128 128
       foreach ($NotFound as $GroupID => $GroupInfo) {
129
-        G::$Cache->cache_value($Key . $GroupID, array('ver' => CACHE::GROUP_VERSION, 'd' => $GroupInfo), 0);
129
+        G::$Cache->cache_value($Key . $GroupID, array('ver' => Cache::GROUP_VERSION, 'd' => $GroupInfo), 0);
130 130
       }
131 131
 
132 132
       $Found = $NotFound + $Found;

+ 2
- 2
feeds.php View File

@@ -16,8 +16,8 @@ require 'classes/config.php'; // The config contains all site-wide configuration
16 16
 require(SERVER_ROOT.'/classes/misc.class.php'); // Require the misc class
17 17
 require(SERVER_ROOT.'/classes/cache.class.php'); // Require the caching class
18 18
 require(SERVER_ROOT.'/classes/feed.class.php'); // Require the feeds class
19
-$Cache = NEW CACHE(MEMCACHED_SERVERS); // Load the caching class
20
-$Feed = NEW FEED; // Load the time class
19
+$Cache = new Cache(MEMCACHED_SERVERS); // Load the caching class
20
+$Feed = new FEED; // Load the time class
21 21
 
22 22
 function check_perms() {
23 23
   return false;

+ 1
- 1
sections/peerupdate/index.php View File

@@ -54,7 +54,7 @@ while ($TorrentID) {
54 54
   if ($LastGroupID != $GroupID) {
55 55
     $CachedData = $Cache->get_value("torrent_group_$GroupID");
56 56
     if ($CachedData !== false) {
57
-      if (isset($CachedData['ver']) && $CachedData['ver'] == CACHE::GROUP_VERSION) {
57
+      if (isset($CachedData['ver']) && $CachedData['ver'] == Cache::GROUP_VERSION) {
58 58
         $CachedStats = &$CachedData['d']['Torrents'];
59 59
       }
60 60
     } else {

Loading…
Cancel
Save