rt/47905/stable30'>backport/47905/stable30 Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Memcache/LoggerWrapperCache.php
blob: 55d5104dedba3299ddf32f6a53bae58b73ef1393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

declare(strict_types = 1);
/**
 * @copyright 2022 Carl Schwan <carl@carlschwan.eu>
 *
 * @author Carl Schwan <carl@carlschwan.eu>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OC\Memcache;

use OCP\IMemcacheTTL;

/**
 * Cache wrapper that logs the cache operation in a log file
 */
class LoggerWrapperCache extends Cache implements IMemcacheTTL {
	/** @var Redis */
	protected $wrappedCache;

	/** @var string $logFile */
	private $logFile;

	/** @var string $prefix */
	protected $prefix;

	public function __construct(Redis $wrappedCache, string $logFile) {
		parent::__construct($wrappedCache->getPrefix());
		$this->wrappedCache = $wrappedCache;
		$this->logFile = $logFile;
	}

	/**
	 * @return string Prefix used for caching purposes
	 */
	public function getPrefix() {
		return $this->prefix;
	}

	protected function getNameSpace() {
		return $this->prefix;
	}

	/** @inheritDoc */
	public function get($key) {
		file_put_contents(
			$this->logFile,
			$this->getNameSpace() . '::get::' . $key . "\n",
			FILE_APPEND
		);
		return $this->wrappedCache->get($key);
	}

	/** @inheritDoc */
	public function set($key, $value, $ttl = 0) {
		file_put_contents(
			$this->logFile,
			$this->getNameSpace() . '::set::' . $key . '::' . $ttl . '::' . json_encode($value) . "\n",
			FILE_APPEND
		);

		return $this->wrappedCache->set($key, $value, $$ttl);
	}

	/** @inheritDoc */
	public function hasKey($key) {
		file_put_contents(
			$this->logFile,
			$this->getNameSpace() . '::hasKey::' . $key . "\n",
			FILE_APPEND
		);

		return $this->wrappedCache->hasKey($key);
	}

	/** @inheritDoc */
	public function remove($key) {
		file_put_contents(
			$this->logFile,
			$this->getNameSpace() . '::remove::' . $key . "\n",
			FILE_APPEND
		);

		return $this->wrappedCache->remove($key);
	}

	/** @inheritDoc */
	public function clear($prefix = '') {
		file_put_contents(
			$this->logFile,
			$this->getNameSpace() . '::clear::' . $prefix . "\n",
			FILE_APPEND
		);

		return $this->wrappedCache->clear($prefix);
	}

	/** @inheritDoc */
	public function add($key, $value, $ttl = 0) {
		file_put_contents(
			$this->logFile,
			$this->getNameSpace() . '::add::' . $key . '::' . $value . "\n",
			FILE_APPEND
		);

		return $this->wrappedCache->add($key, $value, $ttl);
	}

	/** @inheritDoc */
	public function inc($key, $step = 1) {
		file_put_contents(
			$this->logFile,
			$this->getNameSpace() . '::inc::' . $key . "\n",
			FILE_APPEND
		);

		return $this->wrappedCache->inc($key, $step);
	}

	/** @inheritDoc */
	public function dec($key, $step = 1) {
		file_put_contents(
			$this->logFile,
			$this->getNameSpace() . '::dec::' . $key . "\n",
			FILE_APPEND
		);

		return $this->wrappedCache->dec($key, $step);
	}

	/** @inheritDoc */
	public function cas($key, $old, $new) {
		file_put_contents(
			$this->logFile,
			$this->getNameSpace() . '::cas::' . $key . "\n",
			FILE_APPEND
		);

		return $this->wrappedCache->cas($key, $old, $new);
	}

	/** @inheritDoc */
	public function cad($key, $old) {
		file_put_contents(
			$this->logFile,
			$this->getNameSpace() . '::cad::' . $key . "\n",
			FILE_APPEND
		);

		return $this->wrappedCache->cad($key, $old);
	}

	/** @inheritDoc */
	public function setTTL(string $key, int $ttl) {
		$this->wrappedCache->setTTL($key, $ttl);
	}

	public function getTTL(string $key): int|false {
		return $this->wrappedCache->getTTL($key);
	}

	public function compareSetTTL(string $key, mixed $value, int $ttl): bool {
		return $this->wrappedCache->compareSetTTL($key, $value, $ttl);
	}

	public static function isAvailable(): bool {
		return true;
	}
}