Cache.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. *
  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\Memcache;
  26. /**
  27. * @template-implements \ArrayAccess<string,mixed>
  28. */
  29. abstract class Cache implements \ArrayAccess, \OCP\ICache {
  30. /**
  31. * @var string $prefix
  32. */
  33. protected $prefix;
  34. /**
  35. * @param string $prefix
  36. */
  37. public function __construct($prefix = '') {
  38. $this->prefix = $prefix;
  39. }
  40. /**
  41. * @return string Prefix used for caching purposes
  42. */
  43. public function getPrefix() {
  44. return $this->prefix;
  45. }
  46. /**
  47. * @param string $key
  48. * @return mixed
  49. */
  50. abstract public function get($key);
  51. /**
  52. * @param string $key
  53. * @param mixed $value
  54. * @param int $ttl
  55. * @return mixed
  56. */
  57. abstract public function set($key, $value, $ttl = 0);
  58. /**
  59. * @param string $key
  60. * @return mixed
  61. */
  62. abstract public function hasKey($key);
  63. /**
  64. * @param string $key
  65. * @return mixed
  66. */
  67. abstract public function remove($key);
  68. /**
  69. * @param string $prefix
  70. * @return mixed
  71. */
  72. abstract public function clear($prefix = '');
  73. //implement the ArrayAccess interface
  74. public function offsetExists($offset): bool {
  75. return $this->hasKey($offset);
  76. }
  77. public function offsetSet($offset, $value): void {
  78. $this->set($offset, $value);
  79. }
  80. /**
  81. * @return mixed
  82. */
  83. #[\ReturnTypeWillChange]
  84. public function offsetGet($offset) {
  85. return $this->get($offset);
  86. }
  87. public function offsetUnset($offset): void {
  88. $this->remove($offset);
  89. }
  90. }