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

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