Cache.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. abstract class Cache implements \ArrayAccess, \OCP\ICache {
  27. /**
  28. * @var string $prefix
  29. */
  30. protected $prefix;
  31. /**
  32. * @param string $prefix
  33. */
  34. public function __construct($prefix = '') {
  35. $this->prefix = $prefix;
  36. }
  37. /**
  38. * @return string Prefix used for caching purposes
  39. */
  40. public function getPrefix() {
  41. return $this->prefix;
  42. }
  43. /**
  44. * @param string $key
  45. * @return mixed
  46. */
  47. abstract public function get($key);
  48. /**
  49. * @param string $key
  50. * @param mixed $value
  51. * @param int $ttl
  52. * @return mixed
  53. */
  54. abstract public function set($key, $value, $ttl = 0);
  55. /**
  56. * @param string $key
  57. * @return mixed
  58. */
  59. abstract public function hasKey($key);
  60. /**
  61. * @param string $key
  62. * @return mixed
  63. */
  64. abstract public function remove($key);
  65. /**
  66. * @param string $prefix
  67. * @return mixed
  68. */
  69. abstract public function clear($prefix = '');
  70. //implement the ArrayAccess interface
  71. public function offsetExists($offset): bool {
  72. return $this->hasKey($offset);
  73. }
  74. public function offsetSet($offset, $value): void {
  75. $this->set($offset, $value);
  76. }
  77. /**
  78. * @return mixed
  79. */
  80. #[\ReturnTypeWillChange]
  81. public function offsetGet($offset) {
  82. return $this->get($offset);
  83. }
  84. public function offsetUnset($offset): void {
  85. $this->remove($offset);
  86. }
  87. }