]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/MemcachePlugin.php
Merge commit 'origin/master' into testing
[quix0rs-gnu-social.git] / plugins / MemcachePlugin.php
index 998766313f78dba150b86fb2ac3d6494b01547c8..2bc4b892bd6a287c9750c73060f7522f68db5bf5 100644 (file)
@@ -57,6 +57,10 @@ class MemcachePlugin extends Plugin
     public $compressThreshold = 20480;
     public $compressMinSaving = 0.2;
 
+    public $persistent = null;
+
+    public $defaultExpiry = 86400; // 24h
+
     /**
      * Initialize the plugin
      *
@@ -67,6 +71,9 @@ class MemcachePlugin extends Plugin
 
     function onInitializePlugin()
     {
+        if (is_null($this->persistent)) {
+            $this->persistent = (php_sapi_name() == 'cli') ? false : true;
+        }
         $this->_ensureConn();
         return true;
     }
@@ -105,6 +112,9 @@ class MemcachePlugin extends Plugin
     function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
     {
         $this->_ensureConn();
+        if ($expiry === null) {
+            $expiry = $this->defaultExpiry;
+        }
         $success = $this->_conn->set($key, $value, $flag, $expiry);
         Event::handle('EndCacheSet', array($key, $value, $flag,
                                            $expiry));
@@ -128,6 +138,23 @@ class MemcachePlugin extends Plugin
         return false;
     }
 
+    function onStartCacheReconnect(&$success)
+    {
+        if (empty($this->_conn)) {
+            // nothing to do
+            return true;
+        }
+        if ($this->persistent) {
+            common_log(LOG_ERR, "Cannot close persistent memcached connection");
+            $success = false;
+        } else {
+            common_log(LOG_INFO, "Closing memcached connection");
+            $success = $this->_conn->close();
+            $this->_conn = null;
+        }
+        return false;
+    }
+
     /**
      * Ensure that a connection exists
      *
@@ -143,21 +170,19 @@ class MemcachePlugin extends Plugin
             $this->_conn = new Memcache();
 
             if (is_array($this->servers)) {
-                foreach ($this->servers as $server) {
-                    list($host, $port) = explode(';', $server);
-                    if (empty($port)) {
-                        $port = 11211;
-                    }
-
-                    $this->_conn->addServer($host, $port);
-                }
+                $servers = $this->servers;
             } else {
-                $this->_conn->addServer($this->servers);
-                list($host, $port) = explode(';', $this->servers);
-                if (empty($port)) {
+                $servers = array($this->servers);
+            }
+            foreach ($servers as $server) {
+                if (strpos($server, ';') !== false) {
+                    list($host, $port) = explode(';', $server);
+                } else {
+                    $host = $server;
                     $port = 11211;
                 }
-                $this->_conn->addServer($host, $port);
+
+                $this->_conn->addServer($host, $port, $this->persistent);
             }
 
             // Compress items stored in the cache if they're over threshold in size
@@ -171,5 +196,16 @@ class MemcachePlugin extends Plugin
                                                $this->compressMinSaving);
         }
     }
+
+    function onPluginVersion(&$versions)
+    {
+        $versions[] = array('name' => 'Memcache',
+                            'version' => STATUSNET_VERSION,
+                            'author' => 'Evan Prodromou, Craig Andrews',
+                            'homepage' => 'http://status.net/wiki/Plugin:Memcache',
+                            'rawdescription' =>
+                            _m('Use <a href="http://memcached.org/">Memcached</a> to cache query results.'));
+        return true;
+    }
 }