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 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC;
  23. class RedisFactory {
  24. /** @var \Redis */
  25. private $instance;
  26. /** @var SystemConfig */
  27. private $config;
  28. /**
  29. * RedisFactory constructor.
  30. *
  31. * @param SystemConfig $config
  32. */
  33. public function __construct(SystemConfig $config) {
  34. $this->config = $config;
  35. }
  36. private function create() {
  37. $this->instance = new \Redis();
  38. // TODO allow configuring a RedisArray, see https://github.com/nicolasff/phpredis/blob/master/arrays.markdown#redis-arrays
  39. $config = $this->config->getValue('redis', array());
  40. if (isset($config['host'])) {
  41. $host = $config['host'];
  42. } else {
  43. $host = '127.0.0.1';
  44. }
  45. if (isset($config['port'])) {
  46. $port = $config['port'];
  47. } else {
  48. $port = 6379;
  49. }
  50. if (isset($config['timeout'])) {
  51. $timeout = $config['timeout'];
  52. } else {
  53. $timeout = 0.0; // unlimited
  54. }
  55. $this->instance->connect($host, $port, $timeout);
  56. if (isset($config['password']) && $config['password'] !== '') {
  57. $this->instance->auth($config['password']);
  58. }
  59. if (isset($config['dbindex'])) {
  60. $this->instance->select($config['dbindex']);
  61. }
  62. }
  63. public function getInstance() {
  64. if (!$this->isAvailable()) {
  65. throw new \Exception('Redis support is not available');
  66. }
  67. if (!$this->instance instanceof \Redis) {
  68. $this->create();
  69. }
  70. return $this->instance;
  71. }
  72. public function isAvailable() {
  73. return extension_loaded('redis')
  74. && version_compare(phpversion('redis'), '2.2.5', '>=');
  75. }
  76. }