factory.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Markus Goetz <markus@woboq.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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\Memcache;
  27. use \OCP\ICacheFactory;
  28. class Factory implements ICacheFactory {
  29. const NULL_CACHE = '\\OC\\Memcache\\Null';
  30. /**
  31. * @var string $globalPrefix
  32. */
  33. private $globalPrefix;
  34. /**
  35. * @var string $localCacheClass
  36. */
  37. private $localCacheClass;
  38. /**
  39. * @var string $distributedCacheClass
  40. */
  41. private $distributedCacheClass;
  42. /**
  43. * @param string $globalPrefix
  44. * @param string|null $localCacheClass
  45. * @param string|null $distributedCacheClass
  46. */
  47. public function __construct($globalPrefix,
  48. $localCacheClass = null, $distributedCacheClass = null)
  49. {
  50. $this->globalPrefix = $globalPrefix;
  51. if (!($localCacheClass && $localCacheClass::isAvailable())) {
  52. $localCacheClass = self::NULL_CACHE;
  53. }
  54. if (!($distributedCacheClass && $distributedCacheClass::isAvailable())) {
  55. $distributedCacheClass = $localCacheClass;
  56. }
  57. $this->localCacheClass = $localCacheClass;
  58. $this->distributedCacheClass = $distributedCacheClass;
  59. }
  60. /**
  61. * create a distributed cache instance
  62. *
  63. * @param string $prefix
  64. * @return \OC\Memcache\Cache
  65. */
  66. public function createDistributed($prefix = '') {
  67. return new $this->distributedCacheClass($this->globalPrefix . '/' . $prefix);
  68. }
  69. /**
  70. * create a local cache instance
  71. *
  72. * @param string $prefix
  73. * @return \OC\Memcache\Cache
  74. */
  75. public function createLocal($prefix = '') {
  76. return new $this->localCacheClass($this->globalPrefix . '/' . $prefix);
  77. }
  78. /**
  79. * @see \OC\Memcache\Factory::createDistributed()
  80. * @param string $prefix
  81. * @return \OC\Memcache\Cache
  82. */
  83. public function create($prefix = '') {
  84. return $this->createDistributed($prefix);
  85. }
  86. /**
  87. * check memcache availability
  88. *
  89. * @return bool
  90. */
  91. public function isAvailable() {
  92. return ($this->distributedCacheClass !== self::NULL_CACHE);
  93. }
  94. /**
  95. * @see \OC\Memcache\Factory::createLocal()
  96. * @param string $prefix
  97. * @return \OC\Memcache\Cache|null
  98. */
  99. public function createLowLatency($prefix = '') {
  100. return $this->createLocal($prefix);
  101. }
  102. /**
  103. * check local memcache availability
  104. *
  105. * @return bool
  106. */
  107. public function isAvailableLowLatency() {
  108. return ($this->localCacheClass !== self::NULL_CACHE);
  109. }
  110. }