1<?php 2 3namespace Doctrine\Common\Cache; 4 5use function time; 6 7/** 8 * Array cache driver. 9 * 10 * @link www.doctrine-project.org 11 */ 12class ArrayCache extends CacheProvider 13{ 14 /** @var array[] $data each element being a tuple of [$data, $expiration], where the expiration is int|bool */ 15 private $data = []; 16 17 /** @var int */ 18 private $hitsCount = 0; 19 20 /** @var int */ 21 private $missesCount = 0; 22 23 /** @var int */ 24 private $upTime; 25 26 /** 27 * {@inheritdoc} 28 */ 29 public function __construct() 30 { 31 $this->upTime = time(); 32 } 33 34 /** 35 * {@inheritdoc} 36 */ 37 protected function doFetch($id) 38 { 39 if (! $this->doContains($id)) { 40 $this->missesCount += 1; 41 42 return false; 43 } 44 45 $this->hitsCount += 1; 46 47 return $this->data[$id][0]; 48 } 49 50 /** 51 * {@inheritdoc} 52 */ 53 protected function doContains($id) 54 { 55 if (! isset($this->data[$id])) { 56 return false; 57 } 58 59 $expiration = $this->data[$id][1]; 60 61 if ($expiration && $expiration < time()) { 62 $this->doDelete($id); 63 64 return false; 65 } 66 67 return true; 68 } 69 70 /** 71 * {@inheritdoc} 72 */ 73 protected function doSave($id, $data, $lifeTime = 0) 74 { 75 $this->data[$id] = [$data, $lifeTime ? time() + $lifeTime : false]; 76 77 return true; 78 } 79 80 /** 81 * {@inheritdoc} 82 */ 83 protected function doDelete($id) 84 { 85 unset($this->data[$id]); 86 87 return true; 88 } 89 90 /** 91 * {@inheritdoc} 92 */ 93 protected function doFlush() 94 { 95 $this->data = []; 96 97 return true; 98 } 99 100 /** 101 * {@inheritdoc} 102 */ 103 protected function doGetStats() 104 { 105 return [ 106 Cache::STATS_HITS => $this->hitsCount, 107 Cache::STATS_MISSES => $this->missesCount, 108 Cache::STATS_UPTIME => $this->upTime, 109 Cache::STATS_MEMORY_USAGE => null, 110 Cache::STATS_MEMORY_AVAILABLE => null, 111 ]; 112 } 113} 114