<?php
namespace App\V4\Dev\DataCollector\Cache;
use App\Service\Cache\CacheManager;
class TraceableCacheManager extends CacheManager
{
private const LIMIT_OOM_TRACES = 50;
private const LIMIT_OOM_MESSAGE = 'OOM_PREVENTION_TRIGGERED';
public $traces = [];
public function get(string $entityName, string $action, ?string $id = null)
{
$calledAt = microtime(true);
$value = parent::get($entityName, $action, $id);
$trace = [
'entityName' => $entityName,
'action' => $action,
'id' => $id,
'trace' => [self::LIMIT_OOM_MESSAGE],
'value' => self::LIMIT_OOM_MESSAGE,
'time' => (microtime(true) - $calledAt) * 1000,
];
if (count($this->traces['get'] ?? []) <= self::LIMIT_OOM_TRACES) {
$trace = array_merge($trace, [
'trace' => debug_backtrace(2),
'value' => $value,
]);
}
$this->traces['get'][] = $trace;
return $value;
}
public function set(string $entityName, string $action, $value, ?string $id = null, ?int $expiresInSec = null): void
{
$calledAt = microtime(true);
parent::set($entityName, $action, $value, $id, $expiresInSec);
$trace = [
'entityName' => $entityName,
'action' => $action,
'id' => $id,
'expiresInSec' => $expiresInSec,
'trace' => [self::LIMIT_OOM_MESSAGE],
'value' => self::LIMIT_OOM_MESSAGE,
'time' => (microtime(true) - $calledAt) * 1000,
];
if (count($this->traces['set'] ?? []) <= self::LIMIT_OOM_TRACES) {
$trace = array_merge($trace, [
'trace' => debug_backtrace(2),
'value' => $value,
]);
}
$this->traces['set'][] = $trace;
}
public function invalidate(string $entityName, string $action, ?string $customerId = null, ?string $id = null): void
{
$calledAt = microtime(true);
parent::invalidate($entityName, $action, $customerId, $id);
$trace = [
'entityName' => $entityName,
'action' => $action,
'customerId' => $customerId,
'id' => $id,
'trace' => [self::LIMIT_OOM_MESSAGE],
'time' => (microtime(true) - $calledAt) * 1000,
];
if (count($this->traces['invalidate'] ?? []) <= self::LIMIT_OOM_TRACES) {
$trace = array_merge($trace, [
'trace' => debug_backtrace(2),
]);
}
$this->traces['invalidate'][] = $trace;
}
public function invalidateTag(array $tags): bool
{
$calledAt = microtime(true);
$didInvalidate = parent::invalidateTag($tags);
$trace = [
'tags' => $tags,
'trace' => [self::LIMIT_OOM_MESSAGE],
'time' => (microtime(true) - $calledAt) * 1000,
];
if (count($this->traces['invalidateTag'] ?? []) <= self::LIMIT_OOM_TRACES) {
$trace = array_merge($trace, [
'trace' => debug_backtrace(2),
]);
}
$this->traces['invalidateTag'][] = $trace;
return $didInvalidate;
}
public function invalidateAllKeys(): void
{
$calledAt = microtime(true);
parent::invalidateAllKeys();
$this->traces['invalidateAllKeys'][] = [
'trace' => debug_backtrace(2),
'time' => (microtime(true) - $calledAt) * 1000,
];
}
}