1<?php 2 3/** 4 * Device Detector - The Universal Device Detection library for parsing User Agents 5 * 6 * @link https://matomo.org 7 * 8 * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later 9 */ 10 11declare(strict_types=1); 12 13namespace DeviceDetector\Cache; 14 15/** 16 * Class StaticCache 17 * 18 * Simple Cache that caches in a static property 19 * (Speeds up multiple detections in one request) 20 */ 21class StaticCache implements CacheInterface 22{ 23 /** 24 * Holds the static cache data 25 * @var array 26 */ 27 protected static $staticCache = []; 28 29 /** 30 * @inheritdoc 31 */ 32 public function fetch(string $id) 33 { 34 return $this->contains($id) ? self::$staticCache[$id] : false; 35 } 36 37 /** 38 * @inheritdoc 39 */ 40 public function contains(string $id): bool 41 { 42 return isset(self::$staticCache[$id]) || \array_key_exists($id, self::$staticCache); 43 } 44 45 /** 46 * @inheritdoc 47 */ 48 public function save(string $id, $data, int $lifeTime = 0): bool 49 { 50 self::$staticCache[$id] = $data; 51 52 return true; 53 } 54 55 /** 56 * @inheritdoc 57 */ 58 public function delete(string $id): bool 59 { 60 unset(self::$staticCache[$id]); 61 62 return true; 63 } 64 65 /** 66 * @inheritdoc 67 */ 68 public function flushAll(): bool 69 { 70 self::$staticCache = []; 71 72 return true; 73 } 74} 75