add support for Redis as caching server
authorHerb <herb@freeform.ca>
Thu, 15 Oct 2015 16:45:38 +0000 (12:45 -0400)
committerHerb <herb@freeform.ca>
Thu, 15 Oct 2015 16:45:38 +0000 (12:45 -0400)
CRM/Utils/Cache.php
CRM/Utils/Cache/Redis.php [new file with mode: 0644]
templates/CRM/common/civicrm.settings.php.template

index 52fdd8f6a70edae711299d813ad1a3eb00a1815c..f54c5e104dbc9f07561d72156078868e4f937886 100644 (file)
@@ -100,7 +100,7 @@ class CRM_Utils_Cache {
       case 'NoCache':
         $defaults = array();
         break;
-
+      case 'Redis':
       case 'Memcache':
       case 'Memcached':
         $defaults = array(
diff --git a/CRM/Utils/Cache/Redis.php b/CRM/Utils/Cache/Redis.php
new file mode 100644 (file)
index 0000000..9f6dbe8
--- /dev/null
@@ -0,0 +1,151 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+*/
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2015
+ * $Id$
+ *
+ */
+class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface {
+  const DEFAULT_HOST    = 'localhost';
+  const DEFAULT_PORT    = 11211;
+  const DEFAULT_TIMEOUT = 3600;
+  const DEFAULT_PREFIX  = '';
+
+  /**
+   * The host name of the redisd server
+   *
+   * @var string
+  */
+  protected $_host = self::DEFAULT_HOST;
+
+  /**
+   * The port on which to connect on
+   *
+   * @var int
+   */
+  protected $_port = self::DEFAULT_PORT;
+
+  /**
+   * The default timeout to use
+   *
+   * @var int
+   */
+  protected $_timeout = self::DEFAULT_TIMEOUT;
+
+  /**
+   * The prefix prepended to cache keys.
+   *
+   * If we are using the same redis instance for multiple CiviCRM
+   * installs, we must have a unique prefix for each install to prevent
+   * the keys from clobbering each other.
+   *
+   * @var string
+   */
+  protected $_prefix = self::DEFAULT_PREFIX;
+
+  /**
+   * The actual redis object
+   *
+   * @var resource
+   */
+  protected $_cache;
+
+  /**
+   * Constructor
+   *
+   * @param array   $config  an array of configuration params
+   *
+   * @return \CRM_Utils_Cache_Redis
+   */
+  public function __construct($config) {
+    if (isset($config['host'])) {
+      $this->_host = $config['host'];
+    }
+    if (isset($config['port'])) {
+      $this->_port = $config['port'];
+    }
+    if (isset($config['timeout'])) {
+      $this->_timeout = $config['timeout'];
+    }
+    if (isset($config['prefix'])) {
+      $this->_prefix = $config['prefix'];
+    }
+
+    $this->_cache = new Redis();
+    if (!$this->_cache->connect($this->_host, $this->_port)) {
+      // dont use fatal here since we can go in an infinite loop
+      echo 'Could not connect to redisd server';
+      CRM_Utils_System::civiExit();
+    }
+    $this->_cache->auth(CIVICRM_DB_CACHE_PASSWORD);
+  }
+
+  /**
+   * @param $key
+   * @param $value
+   *
+   * @return bool
+   * @throws Exception
+   */
+  public function set($key, &$value) {
+    if (!$this->_cache->set($this->_prefix . $key, serialize($value), $this->_timeout)) {
+      CRM_Core_Error::debug('Result Code: ', $this->_cache->getResultMessage());
+      CRM_Core_Error::fatal("Redis set failed, wondering why?, $key", $value);
+      return FALSE;
+    }
+    return TRUE;
+  }
+
+  /**
+   * @param $key
+   *
+   * @return mixed
+   */
+  public function get($key) {
+    $result = $this->_cache->get($this->_prefix . $key);
+    return unserialize($result);
+  }
+
+  /**
+   * @param $key
+   *
+   * @return mixed
+   */
+  public function delete($key) {
+    return $this->_cache->delete($this->_prefix . $key);
+  }
+
+  /**
+   * @return mixed
+   */
+  public function flush() {
+    return $this->_cache->flush();
+  }
+}
index 4224165487a8940757bc22bf04a196af8dd4c459..7ecff73025d82151bbd1d455f3830b077d1bd28d 100644 (file)
@@ -257,7 +257,7 @@ if (!defined('CIVICRM_DOMAIN_ID')) {
 }
 
 /**
- * Settings to enable external caching using a Memcache server.  This is an
+ * Settings to enable external caching using a cache server.  This is an
  * advanced feature, and you should read and understand the documentation
  * before you turn it on. We cannot store these settings in the DB since the
  * config could potentially also be cached and we need to avoid an infinite
@@ -267,8 +267,8 @@ if (!defined('CIVICRM_DOMAIN_ID')) {
  */
 
 /**
- * If you have a memcache server configured and want CiviCRM to make use of it,
- * set the following constant.  You should only set this once you have your memcache
+ * If you have a cache server configured and want CiviCRM to make use of it,
+ * set the following constant.  You should only set this once you have your cache
  * server up and working, because CiviCRM will not start up if your server is
  * unavailable on the host and port that you specify. By default CiviCRM will use
  * an in-memory array cache
@@ -276,6 +276,7 @@ if (!defined('CIVICRM_DOMAIN_ID')) {
  * To use the php extension memcache  use a value of 'Memcache'
  * To use the php extension memcached use a value of 'Memcached'
  * To use the php extension apc       use a value of 'APCcache'
+ * To use the php extension redis     use a value of 'Redis'
  * To not use any caching (not recommended), use a value of 'NoCache'
  *
  */
@@ -292,12 +293,20 @@ if (!defined('CIVICRM_DB_CACHE_HOST')) {
 }
 
 /**
- * Change this if you are not using the standard port for memcache or apccache (11211)
+ * Change this if you are not using the standard port for your cache server
  */
 if (!defined('CIVICRM_DB_CACHE_PORT')) {
   define('CIVICRM_DB_CACHE_PORT', 11211 );
 }
 
+/**
+ * Change this if your cache server requires a password (currently only works 
+ * with Redis)
+ */
+if (!defined('CIVICRM_DB_CACHE_PASSWORD')) {
+  define('CIVICRM_DB_CACHE_PASSWORD', '' );
+}
+
 /**
  * Items in cache will expire after the number of seconds specified here.
  * Default value is 3600 (i.e., after an hour)
@@ -307,15 +316,15 @@ if (!defined('CIVICRM_DB_CACHE_TIMEOUT')) {
 }
 
 /**
- * If you are sharing the same memcache instance with more than one CiviCRM
+ * If you are sharing the same cache instance with more than one CiviCRM
  * database, you will need to set a different value for the following argument
  * so that each copy of CiviCRM will not interfere with other copies.  If you only
  * have one copy of CiviCRM, you may leave this set to ''.  A good value for
  * this if you have two servers might be 'server1_' for the first server, and
  * 'server2_' for the second server.
  */
-if (!defined('CIVICRM_MEMCACHE_PREFIX')) {
-  define('CIVICRM_MEMCACHE_PREFIX', '');
+if (!defined('CIVICRM_DB_CACHE_PREFIX')) {
+  define('CIVICRM_DB_CACHE_PREFIX', '');
 }
 
 /**