factory.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Memcache;
  9. use \OCP\ICacheFactory;
  10. class Factory implements ICacheFactory {
  11. /**
  12. * @var string $globalPrefix
  13. */
  14. private $globalPrefix;
  15. /**
  16. * @param string $globalPrefix
  17. */
  18. public function __construct($globalPrefix) {
  19. $this->globalPrefix = $globalPrefix;
  20. }
  21. /**
  22. * get a cache instance, will return null if no backend is available
  23. *
  24. * @param string $prefix
  25. * @return \OC\Memcache\Cache
  26. */
  27. function create($prefix = '') {
  28. $prefix = $this->globalPrefix . '/' . $prefix;
  29. if (XCache::isAvailable()) {
  30. return new XCache($prefix);
  31. } elseif (APCu::isAvailable()) {
  32. return new APCu($prefix);
  33. } elseif (APC::isAvailable()) {
  34. return new APC($prefix);
  35. } elseif (Memcached::isAvailable()) {
  36. return new Memcached($prefix);
  37. } else {
  38. return null;
  39. }
  40. }
  41. /**
  42. * check if there is a memcache backend available
  43. *
  44. * @return bool
  45. */
  46. public function isAvailable() {
  47. return XCache::isAvailable() || APCu::isAvailable() || APC::isAvailable() || Memcached::isAvailable();
  48. }
  49. /**
  50. * get a in-server cache instance, will return null if no backend is available
  51. *
  52. * @param string $prefix
  53. * @return null|Cache
  54. */
  55. public function createLowLatency($prefix = '') {
  56. $prefix = $this->globalPrefix . '/' . $prefix;
  57. if (XCache::isAvailable()) {
  58. return new XCache($prefix);
  59. } elseif (APCu::isAvailable()) {
  60. return new APCu($prefix);
  61. } elseif (APC::isAvailable()) {
  62. return new APC($prefix);
  63. } else {
  64. return null;
  65. }
  66. }
  67. /**
  68. * check if there is a in-server backend available
  69. *
  70. * @return bool
  71. */
  72. public function isAvailableLowLatency() {
  73. return XCache::isAvailable() || APCu::isAvailable() || APC::isAvailable();
  74. }
  75. }