From 59e560215c83e278f1c8d829f52124a83ddc0b95 Mon Sep 17 00:00:00 2001 From: Herb Date: Thu, 15 Oct 2015 12:45:38 -0400 Subject: [PATCH] add support for Redis as caching server --- CRM/Utils/Cache.php | 2 +- CRM/Utils/Cache/Redis.php | 151 ++++++++++++++++++ .../CRM/common/civicrm.settings.php.template | 23 ++- 3 files changed, 168 insertions(+), 8 deletions(-) create mode 100644 CRM/Utils/Cache/Redis.php diff --git a/CRM/Utils/Cache.php b/CRM/Utils/Cache.php index 52fdd8f6a7..f54c5e104d 100644 --- a/CRM/Utils/Cache.php +++ b/CRM/Utils/Cache.php @@ -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 index 0000000000..9f6dbe8c57 --- /dev/null +++ b/CRM/Utils/Cache/Redis.php @@ -0,0 +1,151 @@ +_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(); + } +} diff --git a/templates/CRM/common/civicrm.settings.php.template b/templates/CRM/common/civicrm.settings.php.template index 4224165487..7ecff73025 100644 --- a/templates/CRM/common/civicrm.settings.php.template +++ b/templates/CRM/common/civicrm.settings.php.template @@ -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', ''); } /** -- 2.25.1