broker.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. * @author Thomas Tanghus <thomas@tanghus.net>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Cache;
  26. class Broker {
  27. /**
  28. * @var \OC\Cache
  29. */
  30. protected $fast_cache;
  31. /**
  32. * @var \OC\Cache
  33. */
  34. protected $slow_cache;
  35. public function __construct($fast_cache, $slow_cache) {
  36. $this->fast_cache = $fast_cache;
  37. $this->slow_cache = $slow_cache;
  38. }
  39. public function get($key) {
  40. if ($r = $this->fast_cache->get($key)) {
  41. return $r;
  42. }
  43. return $this->slow_cache->get($key);
  44. }
  45. public function set($key, $value, $ttl=0) {
  46. if (!$this->fast_cache->set($key, $value, $ttl)) {
  47. if ($this->fast_cache->hasKey($key)) {
  48. $this->fast_cache->remove($key);
  49. }
  50. return $this->slow_cache->set($key, $value, $ttl);
  51. }
  52. return true;
  53. }
  54. public function hasKey($key) {
  55. if ($this->fast_cache->hasKey($key)) {
  56. return true;
  57. }
  58. return $this->slow_cache->hasKey($key);
  59. }
  60. public function remove($key) {
  61. if ($this->fast_cache->remove($key)) {
  62. return true;
  63. }
  64. return $this->slow_cache->remove($key);
  65. }
  66. public function clear($prefix='') {
  67. $this->fast_cache->clear($prefix);
  68. $this->slow_cache->clear($prefix);
  69. }
  70. }