src/V4/Dev/DataCollector/Cache/TraceableCacheManager.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\V4\Dev\DataCollector\Cache;
  3. use App\Service\Cache\CacheManager;
  4. class TraceableCacheManager extends CacheManager
  5. {
  6.     private const LIMIT_OOM_TRACES 50;
  7.     private const LIMIT_OOM_MESSAGE 'OOM_PREVENTION_TRIGGERED';
  8.     public $traces = [];
  9.     public function get(string $entityNamestring $action, ?string $id null)
  10.     {
  11.         $calledAt microtime(true);
  12.         $value parent::get($entityName$action$id);
  13.         $trace = [
  14.             'entityName' => $entityName,
  15.             'action' => $action,
  16.             'id' => $id,
  17.             'trace' => [self::LIMIT_OOM_MESSAGE],
  18.             'value' => self::LIMIT_OOM_MESSAGE,
  19.             'time' => (microtime(true) - $calledAt) * 1000,
  20.         ];
  21.         if (count($this->traces['get'] ?? []) <= self::LIMIT_OOM_TRACES) {
  22.             $trace array_merge($trace, [
  23.                 'trace' => debug_backtrace(2),
  24.                 'value' => $value,
  25.             ]);
  26.         }
  27.         $this->traces['get'][] = $trace;
  28.         return $value;
  29.     }
  30.     public function set(string $entityNamestring $action$value, ?string $id null, ?int $expiresInSec null): void
  31.     {
  32.         $calledAt microtime(true);
  33.         parent::set($entityName$action$value$id$expiresInSec);
  34.         $trace = [
  35.             'entityName' => $entityName,
  36.             'action' => $action,
  37.             'id' => $id,
  38.             'expiresInSec' => $expiresInSec,
  39.             'trace' => [self::LIMIT_OOM_MESSAGE],
  40.             'value' => self::LIMIT_OOM_MESSAGE,
  41.             'time' => (microtime(true) - $calledAt) * 1000,
  42.         ];
  43.         if (count($this->traces['set'] ?? []) <= self::LIMIT_OOM_TRACES) {
  44.             $trace array_merge($trace, [
  45.                 'trace' => debug_backtrace(2),
  46.                 'value' => $value,
  47.             ]);
  48.         }
  49.         $this->traces['set'][] = $trace;
  50.     }
  51.     public function invalidate(string $entityNamestring $action, ?string $customerId null, ?string $id null): void
  52.     {
  53.         $calledAt microtime(true);
  54.         parent::invalidate($entityName$action$customerId$id);
  55.         $trace = [
  56.             'entityName' => $entityName,
  57.             'action' => $action,
  58.             'customerId' => $customerId,
  59.             'id' => $id,
  60.             'trace' => [self::LIMIT_OOM_MESSAGE],
  61.             'time' => (microtime(true) - $calledAt) * 1000,
  62.         ];
  63.         if (count($this->traces['invalidate'] ?? []) <= self::LIMIT_OOM_TRACES) {
  64.             $trace array_merge($trace, [
  65.                 'trace' => debug_backtrace(2),
  66.             ]);
  67.         }
  68.         $this->traces['invalidate'][] = $trace;
  69.     }
  70.     public function invalidateTag(array $tags): bool
  71.     {
  72.         $calledAt microtime(true);
  73.         $didInvalidate parent::invalidateTag($tags);
  74.         $trace = [
  75.             'tags' => $tags,
  76.             'trace' => [self::LIMIT_OOM_MESSAGE],
  77.             'time' => (microtime(true) - $calledAt) * 1000,
  78.         ];
  79.         if (count($this->traces['invalidateTag'] ?? []) <= self::LIMIT_OOM_TRACES) {
  80.             $trace array_merge($trace, [
  81.                 'trace' => debug_backtrace(2),
  82.             ]);
  83.         }
  84.         $this->traces['invalidateTag'][] = $trace;
  85.         return $didInvalidate;
  86.     }
  87.     public function invalidateAllKeys(): void
  88.     {
  89.         $calledAt microtime(true);
  90.         parent::invalidateAllKeys();
  91.         $this->traces['invalidateAllKeys'][] = [
  92.             'trace' => debug_backtrace(2),
  93.             'time' => (microtime(true) - $calledAt) * 1000,
  94.         ];
  95.     }
  96. }