1
0

WithLocalCache.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace OC\Memcache;
  3. use OCP\Cache\CappedMemoryCache;
  4. use OCP\ICache;
  5. /**
  6. * Wrap a cache instance with an extra later of local, in-memory caching
  7. */
  8. class WithLocalCache implements ICache {
  9. private ICache $inner;
  10. private CappedMemoryCache $cached;
  11. public function __construct(ICache $inner, int $localCapacity = 512) {
  12. $this->inner = $inner;
  13. $this->cached = new CappedMemoryCache($localCapacity);
  14. }
  15. public function get($key) {
  16. if (isset($this->cached[$key])) {
  17. return $this->cached[$key];
  18. } else {
  19. $value = $this->inner->get($key);
  20. if (!is_null($value)) {
  21. $this->cached[$key] = $value;
  22. }
  23. return $value;
  24. }
  25. }
  26. public function set($key, $value, $ttl = 0) {
  27. $this->cached[$key] = $value;
  28. return $this->inner->set($key, $value, $ttl);
  29. }
  30. public function hasKey($key) {
  31. return isset($this->cached[$key]) || $this->inner->hasKey($key);
  32. }
  33. public function remove($key) {
  34. unset($this->cached[$key]);
  35. return $this->inner->remove($key);
  36. }
  37. public function clear($prefix = '') {
  38. $this->cached->clear();
  39. return $this->inner->clear($prefix);
  40. }
  41. public static function isAvailable(): bool {
  42. return false;
  43. }
  44. }