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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
<?php
declare(strict_types = 1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Memcache;
use OC\AppFramework\Http\Request;
use OCP\AppFramework\Http\Response;
use OCP\DataCollector\AbstractDataCollector;
use OCP\IMemcacheTTL;
/**
* Cache wrapper that logs profiling information
* @template-implements \ArrayAccess<string,mixed>
*/
class ProfilerWrapperCache extends AbstractDataCollector implements IMemcacheTTL, \ArrayAccess {
/** @var Redis $wrappedCache*/
protected $wrappedCache;
/** @var string $prefix */
protected $prefix;
/** @var string $type */
private $type;
public function __construct(Redis $wrappedCache, string $type) {
$this->prefix = $wrappedCache->getPrefix();
$this->wrappedCache = $wrappedCache;
$this->type = $type;
$this->data['queries'] = [];
$this->data['cacheHit'] = 0;
$this->data['cacheMiss'] = 0;
}
public function getPrefix(): string {
return $this->prefix;
}
/** @inheritDoc */
public function get($key) {
$start = microtime(true);
$ret = $this->wrappedCache->get($key);
if ($ret === null) {
$this->data['cacheMiss']++;
} else {
$this->data['cacheHit']++;
}
$this->data['queries'][] = [
'start' => $start,
'end' => microtime(true),
'op' => $this->getPrefix() . '::get::' . $key,
'hit' => $ret !== null,
];
return $ret;
}
/** @inheritDoc */
public function set($key, $value, $ttl = 0) {
$start = microtime(true);
$ret = $this->wrappedCache->set($key, $value, $ttl);
$this->data['queries'][] = [
'start' => $start,
'end' => microtime(true),
'op' => $this->getPrefix() . '::set::' . $key,
];
return $ret;
}
/** @inheritDoc */
public function hasKey($key) {
$start = microtime(true);
$ret = $this->wrappedCache->hasKey($key);
$this->data['queries'][] = [
'start' => $start,
'end' => microtime(true),
'op' => $this->getPrefix() . '::hasKey::' . $key,
];
return $ret;
}
/** @inheritDoc */
public function remove($key) {
$start = microtime(true);
$ret = $this->wrappedCache->remove($key);
$this->data['queries'][] = [
'start' => $start,
'end' => microtime(true),
'op' => $this->getPrefix() . '::remove::' . $key,
];
return $ret;
}
/** @inheritDoc */
public function clear($prefix = '') {
$start = microtime(true);
$ret = $this->wrappedCache->clear($prefix);
$this->data['queries'][] = [
'start' => $start,
'end' => microtime(true),
'op' => $this->getPrefix() . '::clear::' . $prefix,
];
return $ret;
}
/** @inheritDoc */
public function add($key, $value, $ttl = 0) {
$start = microtime(true);
$ret = $this->wrappedCache->add($key, $value, $ttl);
$this->data['queries'][] = [
'start' => $start,
'end' => microtime(true),
'op' => $this->getPrefix() . '::add::' . $key,
];
return $ret;
}
/** @inheritDoc */
public function inc($key, $step = 1) {
$start = microtime(true);
$ret = $this->wrappedCache->inc($key, $step);
$this->data['queries'][] = [
'start' => $start,
'end' => microtime(true),
'op' => $this->getPrefix() . '::inc::' . $key,
];
return $ret;
}
/** @inheritDoc */
public function dec($key, $step = 1) {
$start = microtime(true);
$ret = $this->wrappedCache->dec($key, $step);
$this->data['queries'][] = [
'start' => $start,
'end' => microtime(true),
'op' => $this->getPrefix() . '::dev::' . $key,
];
return $ret;
}
/** @inheritDoc */
public function cas($key, $old, $new) {
$start = microtime(true);
$ret = $this->wrappedCache->cas($key, $old, $new);
$this->data['queries'][] = [
'start' => $start,
'end' => microtime(true),
'op' => $this->getPrefix() . '::cas::' . $key,
];
return $ret;
}
/** @inheritDoc */
public function cad($key, $old) {
$start = microtime(true);
$ret = $this->wrappedCache->cad($key, $old);
$this->data['queries'][] = [
'start' => $start,
'end' => microtime(true),
'op' => $this->getPrefix() . '::cad::' . $key,
];
return $ret;
}
/** @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 function offsetExists($offset): bool {
return $this->hasKey($offset);
}
public function offsetSet($offset, $value): void {
$this->set($offset, $value);
}
/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset) {
return $this->get($offset);
}
public function offsetUnset($offset): void {
$this->remove($offset);
}
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void {
// Nothing to do here $data is already ready
}
public function getName(): string {
return 'cache/' . $this->type . '/' . $this->prefix;
}
public static function isAvailable(): bool {
return true;
}
}
|