LoggerWrapperCache.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Memcache;
  8. use OCP\IMemcacheTTL;
  9. /**
  10. * Cache wrapper that logs the cache operation in a log file
  11. */
  12. class LoggerWrapperCache extends Cache implements IMemcacheTTL {
  13. /** @var Redis */
  14. protected $wrappedCache;
  15. /** @var string $logFile */
  16. private $logFile;
  17. /** @var string $prefix */
  18. protected $prefix;
  19. public function __construct(Redis $wrappedCache, string $logFile) {
  20. parent::__construct($wrappedCache->getPrefix());
  21. $this->wrappedCache = $wrappedCache;
  22. $this->logFile = $logFile;
  23. }
  24. /**
  25. * @return string Prefix used for caching purposes
  26. */
  27. public function getPrefix() {
  28. return $this->prefix;
  29. }
  30. protected function getNameSpace() {
  31. return $this->prefix;
  32. }
  33. /** @inheritDoc */
  34. public function get($key) {
  35. file_put_contents(
  36. $this->logFile,
  37. $this->getNameSpace() . '::get::' . $key . "\n",
  38. FILE_APPEND
  39. );
  40. return $this->wrappedCache->get($key);
  41. }
  42. /** @inheritDoc */
  43. public function set($key, $value, $ttl = 0) {
  44. file_put_contents(
  45. $this->logFile,
  46. $this->getNameSpace() . '::set::' . $key . '::' . $ttl . '::' . json_encode($value) . "\n",
  47. FILE_APPEND
  48. );
  49. return $this->wrappedCache->set($key, $value, $ttl);
  50. }
  51. /** @inheritDoc */
  52. public function hasKey($key) {
  53. file_put_contents(
  54. $this->logFile,
  55. $this->getNameSpace() . '::hasKey::' . $key . "\n",
  56. FILE_APPEND
  57. );
  58. return $this->wrappedCache->hasKey($key);
  59. }
  60. /** @inheritDoc */
  61. public function remove($key) {
  62. file_put_contents(
  63. $this->logFile,
  64. $this->getNameSpace() . '::remove::' . $key . "\n",
  65. FILE_APPEND
  66. );
  67. return $this->wrappedCache->remove($key);
  68. }
  69. /** @inheritDoc */
  70. public function clear($prefix = '') {
  71. file_put_contents(
  72. $this->logFile,
  73. $this->getNameSpace() . '::clear::' . $prefix . "\n",
  74. FILE_APPEND
  75. );
  76. return $this->wrappedCache->clear($prefix);
  77. }
  78. /** @inheritDoc */
  79. public function add($key, $value, $ttl = 0) {
  80. file_put_contents(
  81. $this->logFile,
  82. $this->getNameSpace() . '::add::' . $key . '::' . $value . "\n",
  83. FILE_APPEND
  84. );
  85. return $this->wrappedCache->add($key, $value, $ttl);
  86. }
  87. /** @inheritDoc */
  88. public function inc($key, $step = 1) {
  89. file_put_contents(
  90. $this->logFile,
  91. $this->getNameSpace() . '::inc::' . $key . "\n",
  92. FILE_APPEND
  93. );
  94. return $this->wrappedCache->inc($key, $step);
  95. }
  96. /** @inheritDoc */
  97. public function dec($key, $step = 1) {
  98. file_put_contents(
  99. $this->logFile,
  100. $this->getNameSpace() . '::dec::' . $key . "\n",
  101. FILE_APPEND
  102. );
  103. return $this->wrappedCache->dec($key, $step);
  104. }
  105. /** @inheritDoc */
  106. public function cas($key, $old, $new) {
  107. file_put_contents(
  108. $this->logFile,
  109. $this->getNameSpace() . '::cas::' . $key . "\n",
  110. FILE_APPEND
  111. );
  112. return $this->wrappedCache->cas($key, $old, $new);
  113. }
  114. /** @inheritDoc */
  115. public function cad($key, $old) {
  116. file_put_contents(
  117. $this->logFile,
  118. $this->getNameSpace() . '::cad::' . $key . "\n",
  119. FILE_APPEND
  120. );
  121. return $this->wrappedCache->cad($key, $old);
  122. }
  123. /** @inheritDoc */
  124. public function ncad(string $key, mixed $old): bool {
  125. file_put_contents(
  126. $this->logFile,
  127. $this->getNameSpace() . '::ncad::' . $key . "\n",
  128. FILE_APPEND
  129. );
  130. return $this->wrappedCache->cad($key, $old);
  131. }
  132. /** @inheritDoc */
  133. public function setTTL(string $key, int $ttl) {
  134. $this->wrappedCache->setTTL($key, $ttl);
  135. }
  136. public function getTTL(string $key): int|false {
  137. return $this->wrappedCache->getTTL($key);
  138. }
  139. public function compareSetTTL(string $key, mixed $value, int $ttl): bool {
  140. return $this->wrappedCache->compareSetTTL($key, $value, $ttl);
  141. }
  142. public static function isAvailable(): bool {
  143. return true;
  144. }
  145. }