| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Transparent WordPress object-cache adapter used during a diagnostic scope. |
| 9 |
* |
| 10 |
* Unknown Object Cache Pro methods and public properties pass through without |
| 11 |
* tracing. Standard key operations are bracketed with the tracer's hard |
| 12 |
* record budget. The original cache remains available for capability and |
| 13 |
* metrics inspection without the adapter making magic methods look real. |
| 14 |
* |
| 15 |
* allow-no-test-found: exercised through the real AJAX table render entry point in tests/AjaxRowProgressAttributionTest.php |
| 16 |
*/ |
| 17 |
final class ABJ_404_Solution_InstrumentedObjectCache { |
| 18 |
/** @var object */ |
| 19 |
private $target; |
| 20 |
/** @var ABJ_404_Solution_CacheOperationTraceSink */ |
| 21 |
private $tracer; |
| 22 |
|
| 23 |
public function __construct( |
| 24 |
object $target, |
| 25 |
ABJ_404_Solution_CacheOperationTraceSink $tracer |
| 26 |
) { |
| 27 |
$this->target = $target; |
| 28 |
$this->tracer = $tracer; |
| 29 |
} |
| 30 |
|
| 31 |
/** The decorated cache, for non-operation capability inspection. */ |
| 32 |
public function originalCache(): object { |
| 33 |
return $this->target; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param int|string $key |
| 38 |
* @param bool|null $found |
| 39 |
* @return mixed |
| 40 |
*/ |
| 41 |
public function get($key, string $group = 'default', bool $force = false, &$found = null) { |
| 42 |
return $this->tracer->traceCache('get', $key, $group, function () use ($key, $group, $force, &$found) { |
| 43 |
return $this->callTarget('get', array($key, $group, $force, &$found)); |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param array<int|string, int|string> $keys |
| 49 |
* @return mixed |
| 50 |
*/ |
| 51 |
public function get_multiple(array $keys, string $group = 'default', bool $force = false) { |
| 52 |
return $this->tracer->traceCache('get_multiple', $keys, $group, function () use ($keys, $group, $force) { |
| 53 |
return $this->callTarget('get_multiple', array($keys, $group, $force)); |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @param array<int|string, mixed> $data |
| 59 |
* @return mixed |
| 60 |
*/ |
| 61 |
public function add_multiple(array $data, string $group = 'default', int $expire = 0) { |
| 62 |
return $this->tracer->traceCache('add_multiple', $data, $group, function () use ($data, $group, $expire) { |
| 63 |
return $this->callTarget('add_multiple', array($data, $group, $expire)); |
| 64 |
}); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param array<int|string, mixed> $data |
| 69 |
* @return mixed |
| 70 |
*/ |
| 71 |
public function set_multiple(array $data, string $group = 'default', int $expire = 0) { |
| 72 |
return $this->tracer->traceCache('set_multiple', $data, $group, function () use ($data, $group, $expire) { |
| 73 |
return $this->callTarget('set_multiple', array($data, $group, $expire)); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param array<int|string, int|string> $keys |
| 79 |
* @return mixed |
| 80 |
*/ |
| 81 |
public function delete_multiple(array $keys, string $group = 'default') { |
| 82 |
return $this->tracer->traceCache('delete_multiple', $keys, $group, function () use ($keys, $group) { |
| 83 |
return $this->callTarget('delete_multiple', array($keys, $group)); |
| 84 |
}); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @param int|string $key |
| 89 |
* @param mixed $value |
| 90 |
* @return mixed |
| 91 |
*/ |
| 92 |
public function set($key, $value, string $group = 'default', int $expire = 0) { |
| 93 |
return $this->tracer->traceCache('set', $key, $group, function () use ($key, $value, $group, $expire) { |
| 94 |
return $this->callTarget('set', array($key, $value, $group, $expire)); |
| 95 |
}); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @param int|string $key |
| 100 |
* @param mixed $value |
| 101 |
* @return mixed |
| 102 |
*/ |
| 103 |
public function add($key, $value, string $group = 'default', int $expire = 0) { |
| 104 |
return $this->tracer->traceCache('add', $key, $group, function () use ($key, $value, $group, $expire) { |
| 105 |
return $this->callTarget('add', array($key, $value, $group, $expire)); |
| 106 |
}); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param int|string $key |
| 111 |
* @param mixed $value |
| 112 |
* @return mixed |
| 113 |
*/ |
| 114 |
public function replace($key, $value, string $group = 'default', int $expire = 0) { |
| 115 |
return $this->tracer->traceCache('replace', $key, $group, function () use ($key, $value, $group, $expire) { |
| 116 |
return $this->callTarget('replace', array($key, $value, $group, $expire)); |
| 117 |
}); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @param int|string $key |
| 122 |
* @return mixed |
| 123 |
*/ |
| 124 |
public function delete($key, string $group = 'default', bool $deprecated = false) { |
| 125 |
return $this->tracer->traceCache('delete', $key, $group, function () use ($key, $group, $deprecated) { |
| 126 |
return $this->callTarget('delete', array($key, $group, $deprecated)); |
| 127 |
}); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @param int|string $key |
| 132 |
* @return mixed |
| 133 |
*/ |
| 134 |
public function incr($key, int $offset = 1, string $group = 'default') { |
| 135 |
return $this->tracer->traceCache('incr', $key, $group, function () use ($key, $offset, $group) { |
| 136 |
return $this->callTarget('incr', array($key, $offset, $group)); |
| 137 |
}); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @param int|string $key |
| 142 |
* @return mixed |
| 143 |
*/ |
| 144 |
public function decr($key, int $offset = 1, string $group = 'default') { |
| 145 |
return $this->tracer->traceCache('decr', $key, $group, function () use ($key, $offset, $group) { |
| 146 |
return $this->callTarget('decr', array($key, $offset, $group)); |
| 147 |
}); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @param array<int, mixed> $arguments |
| 152 |
* @return mixed |
| 153 |
*/ |
| 154 |
public function __call(string $name, array $arguments) { |
| 155 |
return $this->callTarget($name, $arguments); |
| 156 |
} |
| 157 |
|
| 158 |
/** @return mixed */ |
| 159 |
public function __get(string $name) { |
| 160 |
return $this->target->$name; |
| 161 |
} |
| 162 |
|
| 163 |
/** @param mixed $value */ |
| 164 |
public function __set(string $name, $value): void { |
| 165 |
$this->target->$name = $value; |
| 166 |
} |
| 167 |
|
| 168 |
public function __isset(string $name): bool { |
| 169 |
return isset($this->target->$name); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @param array<int, mixed> $arguments |
| 174 |
* @return mixed |
| 175 |
*/ |
| 176 |
private function callTarget(string $method, array $arguments) { |
| 177 |
$callback = array($this->target, $method); |
| 178 |
if (!is_callable($callback)) { |
| 179 |
abj404_logPhpFallback( |
| 180 |
'diagnostic-object-cache', |
| 181 |
'cache method unavailable: ' . get_class($this->target) . '::' . $method |
| 182 |
); |
| 183 |
return false; |
| 184 |
} |
| 185 |
return call_user_func_array($callback, $arguments); |
| 186 |
} |
| 187 |
} |
| 188 |
|