Cache.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Memcache;
  8. /**
  9. * @template-implements \ArrayAccess<string,mixed>
  10. */
  11. abstract class Cache implements \ArrayAccess, \OCP\ICache {
  12. /**
  13. * @var string $prefix
  14. */
  15. protected $prefix;
  16. /**
  17. * @param string $prefix
  18. */
  19. public function __construct($prefix = '') {
  20. $this->prefix = $prefix;
  21. }
  22. /**
  23. * @return string Prefix used for caching purposes
  24. */
  25. public function getPrefix() {
  26. return $this->prefix;
  27. }
  28. /**
  29. * @param string $key
  30. * @return mixed
  31. */
  32. abstract public function get($key);
  33. /**
  34. * @param string $key
  35. * @param mixed $value
  36. * @param int $ttl
  37. * @return mixed
  38. */
  39. abstract public function set($key, $value, $ttl = 0);
  40. /**
  41. * @param string $key
  42. * @return mixed
  43. */
  44. abstract public function hasKey($key);
  45. /**
  46. * @param string $key
  47. * @return mixed
  48. */
  49. abstract public function remove($key);
  50. /**
  51. * @param string $prefix
  52. * @return mixed
  53. */
  54. abstract public function clear($prefix = '');
  55. //implement the ArrayAccess interface
  56. public function offsetExists($offset): bool {
  57. return $this->hasKey($offset);
  58. }
  59. public function offsetSet($offset, $value): void {
  60. $this->set($offset, $value);
  61. }
  62. /**
  63. * @return mixed
  64. */
  65. #[\ReturnTypeWillChange]
  66. public function offsetGet($offset) {
  67. return $this->get($offset);
  68. }
  69. public function offsetUnset($offset): void {
  70. $this->remove($offset);
  71. }
  72. }