LoggerWrapperCache.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * @copyright 2022 Carl Schwan <carl@carlschwan.eu>
  5. *
  6. * @author Carl Schwan <carl@carlschwan.eu>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Memcache;
  25. use OCP\IMemcacheTTL;
  26. /**
  27. * Cache wrapper that logs the cache operation in a log file
  28. */
  29. class LoggerWrapperCache extends Cache implements IMemcacheTTL {
  30. /** @var Redis */
  31. protected $wrappedCache;
  32. /** @var string $logFile */
  33. private $logFile;
  34. /** @var string $prefix */
  35. protected $prefix;
  36. public function __construct(Redis $wrappedCache, string $logFile) {
  37. parent::__construct($wrappedCache->getPrefix());
  38. $this->wrappedCache = $wrappedCache;
  39. $this->logFile = $logFile;
  40. }
  41. /**
  42. * @return string Prefix used for caching purposes
  43. */
  44. public function getPrefix() {
  45. return $this->prefix;
  46. }
  47. protected function getNameSpace() {
  48. return $this->prefix;
  49. }
  50. /** @inheritDoc */
  51. public function get($key) {
  52. file_put_contents(
  53. $this->logFile,
  54. $this->getNameSpace() . '::get::' . $key . "\n",
  55. FILE_APPEND
  56. );
  57. return $this->wrappedCache->get($key);
  58. }
  59. /** @inheritDoc */
  60. public function set($key, $value, $ttl = 0) {
  61. file_put_contents(
  62. $this->logFile,
  63. $this->getNameSpace() . '::set::' . $key . '::' . $ttl . '::' . json_encode($value) . "\n",
  64. FILE_APPEND
  65. );
  66. return $this->wrappedCache->set($key, $value, $ttl);
  67. }
  68. /** @inheritDoc */
  69. public function hasKey($key) {
  70. file_put_contents(
  71. $this->logFile,
  72. $this->getNameSpace() . '::hasKey::' . $key . "\n",
  73. FILE_APPEND
  74. );
  75. return $this->wrappedCache->hasKey($key);
  76. }
  77. /** @inheritDoc */
  78. public function remove($key) {
  79. file_put_contents(
  80. $this->logFile,
  81. $this->getNameSpace() . '::remove::' . $key . "\n",
  82. FILE_APPEND
  83. );
  84. return $this->wrappedCache->remove($key);
  85. }
  86. /** @inheritDoc */
  87. public function clear($prefix = '') {
  88. file_put_contents(
  89. $this->logFile,
  90. $this->getNameSpace() . '::clear::' . $prefix . "\n",
  91. FILE_APPEND
  92. );
  93. return $this->wrappedCache->clear($prefix);
  94. }
  95. /** @inheritDoc */
  96. public function add($key, $value, $ttl = 0) {
  97. file_put_contents(
  98. $this->logFile,
  99. $this->getNameSpace() . '::add::' . $key . '::' . $value . "\n",
  100. FILE_APPEND
  101. );
  102. return $this->wrappedCache->add($key, $value, $ttl);
  103. }
  104. /** @inheritDoc */
  105. public function inc($key, $step = 1) {
  106. file_put_contents(
  107. $this->logFile,
  108. $this->getNameSpace() . '::inc::' . $key . "\n",
  109. FILE_APPEND
  110. );
  111. return $this->wrappedCache->inc($key, $step);
  112. }
  113. /** @inheritDoc */
  114. public function dec($key, $step = 1) {
  115. file_put_contents(
  116. $this->logFile,
  117. $this->getNameSpace() . '::dec::' . $key . "\n",
  118. FILE_APPEND
  119. );
  120. return $this->wrappedCache->dec($key, $step);
  121. }
  122. /** @inheritDoc */
  123. public function cas($key, $old, $new) {
  124. file_put_contents(
  125. $this->logFile,
  126. $this->getNameSpace() . '::cas::' . $key . "\n",
  127. FILE_APPEND
  128. );
  129. return $this->wrappedCache->cas($key, $old, $new);
  130. }
  131. /** @inheritDoc */
  132. public function cad($key, $old) {
  133. file_put_contents(
  134. $this->logFile,
  135. $this->getNameSpace() . '::cad::' . $key . "\n",
  136. FILE_APPEND
  137. );
  138. return $this->wrappedCache->cad($key, $old);
  139. }
  140. /** @inheritDoc */
  141. public function setTTL(string $key, int $ttl) {
  142. $this->wrappedCache->setTTL($key, $ttl);
  143. }
  144. public function getTTL(string $key): int|false {
  145. return $this->wrappedCache->getTTL($key);
  146. }
  147. public function compareSetTTL(string $key, mixed $value, int $ttl): bool {
  148. return $this->wrappedCache->compareSetTTL($key, $value, $ttl);
  149. }
  150. public static function isAvailable(): bool {
  151. return true;
  152. }
  153. }