broker.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class OC_Cache_Broker {
  9. protected $fast_cache;
  10. protected $slow_cache;
  11. public function __construct($fast_cache, $slow_cache) {
  12. $this->fast_cache = $fast_cache;
  13. $this->slow_cache = $slow_cache;
  14. }
  15. public function get($key) {
  16. if ($r = $this->fast_cache->get($key)) {
  17. return $r;
  18. }
  19. return $this->slow_cache->get($key);
  20. }
  21. public function set($key, $value, $ttl=0) {
  22. if (!$this->fast_cache->set($key, $value, $ttl)) {
  23. if ($this->fast_cache->hasKey($key)) {
  24. $this->fast_cache->remove($key);
  25. }
  26. return $this->slow_cache->set($key, $value, $ttl);
  27. }
  28. return true;
  29. }
  30. public function hasKey($key) {
  31. if ($this->fast_cache->hasKey($key)) {
  32. return true;
  33. }
  34. return $this->slow_cache->hasKey($key);
  35. }
  36. public function remove($key) {
  37. if ($this->fast_cache->remove($key)) {
  38. return true;
  39. }
  40. return $this->slow_cache->remove($key);
  41. }
  42. public function clear($prefix='') {
  43. $this->fast_cache->clear($prefix);
  44. $this->slow_cache->clear($prefix);
  45. }
  46. }