You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RedisFactory.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Robin McCorkell <robin@mccorkell.me.uk>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC;
  25. class RedisFactory {
  26. /** @var \Redis */
  27. private $instance;
  28. /** @var SystemConfig */
  29. private $config;
  30. /**
  31. * RedisFactory constructor.
  32. *
  33. * @param SystemConfig $config
  34. */
  35. public function __construct(SystemConfig $config) {
  36. $this->config = $config;
  37. }
  38. private function create() {
  39. if ($config = $this->config->getValue('redis.cluster', [])) {
  40. if (!class_exists('RedisCluster')) {
  41. throw new \Exception('Redis Cluster support is not available');
  42. }
  43. // cluster config
  44. if (isset($config['timeout'])) {
  45. $timeout = $config['timeout'];
  46. } else {
  47. $timeout = null;
  48. }
  49. if (isset($config['read_timeout'])) {
  50. $readTimeout = $config['read_timeout'];
  51. } else {
  52. $readTimeout = null;
  53. }
  54. if (isset($config['password']) && $config['password'] !== '') {
  55. $this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout, false, $config['password']);
  56. } else {
  57. $this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout);
  58. }
  59. if (isset($config['failover_mode'])) {
  60. $this->instance->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, $config['failover_mode']);
  61. }
  62. } else {
  63. $this->instance = new \Redis();
  64. $config = $this->config->getValue('redis', []);
  65. if (isset($config['host'])) {
  66. $host = $config['host'];
  67. } else {
  68. $host = '127.0.0.1';
  69. }
  70. if (isset($config['port'])) {
  71. $port = $config['port'];
  72. } else if ($host[0] !== '/') {
  73. $port = 6379;
  74. } else {
  75. $port = null;
  76. }
  77. if (isset($config['timeout'])) {
  78. $timeout = $config['timeout'];
  79. } else {
  80. $timeout = 0.0; // unlimited
  81. }
  82. $this->instance->connect($host, $port, $timeout);
  83. if (isset($config['password']) && $config['password'] !== '') {
  84. $this->instance->auth($config['password']);
  85. }
  86. if (isset($config['dbindex'])) {
  87. $this->instance->select($config['dbindex']);
  88. }
  89. }
  90. }
  91. public function getInstance() {
  92. if (!$this->isAvailable()) {
  93. throw new \Exception('Redis support is not available');
  94. }
  95. if (!$this->instance instanceof \Redis) {
  96. $this->create();
  97. }
  98. return $this->instance;
  99. }
  100. public function isAvailable() {
  101. return extension_loaded('redis')
  102. && version_compare(phpversion('redis'), '2.2.5', '>=');
  103. }
  104. }