123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- declare(strict_types = 1);
- namespace OC\Memcache;
- use OC\AppFramework\Http\Request;
- use OCP\AppFramework\Http\Response;
- use OCP\DataCollector\AbstractDataCollector;
- use OCP\IMemcacheTTL;
- class ProfilerWrapperCache extends AbstractDataCollector implements IMemcacheTTL, \ArrayAccess {
-
- protected $wrappedCache;
-
- protected $prefix;
-
- 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- 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;
- }
-
- public function setTTL($key, $ttl) {
- $this->wrappedCache->setTTL($key, $ttl);
- }
- public function offsetExists($offset): bool {
- return $this->hasKey($offset);
- }
- public function offsetSet($offset, $value): void {
- $this->set($offset, $value);
- }
-
-
- 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 {
-
- }
- public function getName(): string {
- return 'cache/' . $this->type . '/' . $this->prefix;
- }
- public static function isAvailable(): bool {
- return true;
- }
- }
|