RedisFactory.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $this->instance = new \RedisCluster(null, $config['seeds'], $timeout, $readTimeout);
  55. if (isset($config['failover_mode'])) {
  56. $this->instance->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, $config['failover_mode']);
  57. }
  58. } else {
  59. $this->instance = new \Redis();
  60. $config = $this->config->getValue('redis', []);
  61. if (isset($config['host'])) {
  62. $host = $config['host'];
  63. } else {
  64. $host = '127.0.0.1';
  65. }
  66. if (isset($config['port'])) {
  67. $port = $config['port'];
  68. } else {
  69. $port = 6379;
  70. }
  71. if (isset($config['timeout'])) {
  72. $timeout = $config['timeout'];
  73. } else {
  74. $timeout = 0.0; // unlimited
  75. }
  76. $this->instance->connect($host, $port, $timeout);
  77. if (isset($config['password']) && $config['password'] !== '') {
  78. $this->instance->auth($config['password']);
  79. }
  80. if (isset($config['dbindex'])) {
  81. $this->instance->select($config['dbindex']);
  82. }
  83. }
  84. }
  85. public function getInstance() {
  86. if (!$this->isAvailable()) {
  87. throw new \Exception('Redis support is not available');
  88. }
  89. if (!$this->instance instanceof \Redis) {
  90. $this->create();
  91. }
  92. return $this->instance;
  93. }
  94. public function isAvailable() {
  95. return extension_loaded('redis')
  96. && version_compare(phpversion('redis'), '2.2.5', '>=');
  97. }
  98. }