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
 memcache.
12
 memcache.
13
 
13
 
14
 Also, Memcache::get and Memcache::set have been wrapped by
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
 as get, but cache_value only takes the key, the value, and the duration
16
 as get, but cache_value only takes the key, the value, and the duration
17
 (no zlib).
17
 (no zlib).
18
 
18
 
24
 
24
 
25
 |*************************************************************************/
25
 |*************************************************************************/
26
 
26
 
27
-if (!extension_loaded('memcache')) {
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
-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
   const GROUP_VERSION = 5;
39
   const GROUP_VERSION = 5;
36
 
40
 
37
   public $CacheHits = [];
41
   public $CacheHits = [];
57
   public $InternalCache = true;
61
   public $InternalCache = true;
58
 
62
 
59
   function __construct($Servers) {
63
   function __construct($Servers) {
64
+    if (is_subclass_of($this, 'Memcached')) parent::__construct();
60
     $this->Servers = $Servers;
65
     $this->Servers = $Servers;
61
     foreach ($Servers as $Server) {
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
     if (empty($Key)) {
88
     if (empty($Key)) {
80
       trigger_error("Cache insert failed for empty key");
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
       trigger_error("Cache insert failed for key $Key");
94
       trigger_error("Cache insert failed for key $Key");
84
     }
95
     }
85
     if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
96
     if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
98
 
109
 
99
   public function replace_value($Key, $Value, $Duration = 2592000) {
110
   public function replace_value($Key, $Value, $Duration = 2592000) {
100
     $StartTime = microtime(true);
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
     if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
115
     if ($this->InternalCache && array_key_exists($Key, $this->CacheHits)) {
103
       $this->CacheHits[$Key] = $Value;
116
       $this->CacheHits[$Key] = $Value;
104
     }
117
     }

+ 1
- 1
classes/script_start.php View File

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

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

57
         continue;
57
         continue;
58
       }
58
       }
59
       $Data = G::$Cache->get_value($Key . $GroupID, true);
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
         unset($NotFound[$GroupID]);
61
         unset($NotFound[$GroupID]);
62
         $Found[$GroupID] = $Data['d'];
62
         $Found[$GroupID] = $Data['d'];
63
       }
63
       }
126
       }
126
       }
127
 
127
 
128
       foreach ($NotFound as $GroupID => $GroupInfo) {
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
       $Found = $NotFound + $Found;
132
       $Found = $NotFound + $Found;

+ 2
- 2
feeds.php View File

16
 require(SERVER_ROOT.'/classes/misc.class.php'); // Require the misc class
16
 require(SERVER_ROOT.'/classes/misc.class.php'); // Require the misc class
17
 require(SERVER_ROOT.'/classes/cache.class.php'); // Require the caching class
17
 require(SERVER_ROOT.'/classes/cache.class.php'); // Require the caching class
18
 require(SERVER_ROOT.'/classes/feed.class.php'); // Require the feeds class
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
 function check_perms() {
22
 function check_perms() {
23
   return false;
23
   return false;

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

54
   if ($LastGroupID != $GroupID) {
54
   if ($LastGroupID != $GroupID) {
55
     $CachedData = $Cache->get_value("torrent_group_$GroupID");
55
     $CachedData = $Cache->get_value("torrent_group_$GroupID");
56
     if ($CachedData !== false) {
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
         $CachedStats = &$CachedData['d']['Torrents'];
58
         $CachedStats = &$CachedData['d']['Torrents'];
59
       }
59
       }
60
     } else {
60
     } else {

Loading…
Cancel
Save