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.

118 lines
3.2 KiB

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