2015-08-17 17:00:26 -07:00
< ? php
namespace Doctrine\Common\Cache ;
2018-11-23 12:29:20 +00:00
use function time ;
2015-08-17 17:00:26 -07:00
/**
* Array cache driver .
*
* @ link www . doctrine - project . org
*/
class ArrayCache extends CacheProvider
{
2018-11-23 12:29:20 +00:00
/** @var array[] $data each element being a tuple of [$data, $expiration], where the expiration is int|bool */
2017-04-13 15:53:35 +01:00
private $data = [];
2018-11-23 12:29:20 +00:00
/** @var int */
2017-04-13 15:53:35 +01:00
private $hitsCount = 0 ;
2018-11-23 12:29:20 +00:00
/** @var int */
2017-04-13 15:53:35 +01:00
private $missesCount = 0 ;
2018-11-23 12:29:20 +00:00
/** @var int */
2017-04-13 15:53:35 +01:00
private $upTime ;
/**
* { @ inheritdoc }
*/
public function __construct ()
{
$this -> upTime = time ();
}
2015-08-17 17:00:26 -07:00
/**
* { @ inheritdoc }
*/
protected function doFetch ( $id )
{
2017-04-13 15:53:35 +01:00
if ( ! $this -> doContains ( $id )) {
$this -> missesCount += 1 ;
return false ;
}
$this -> hitsCount += 1 ;
return $this -> data [ $id ][ 0 ];
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
protected function doContains ( $id )
{
2017-04-13 15:53:35 +01:00
if ( ! isset ( $this -> data [ $id ])) {
return false ;
}
$expiration = $this -> data [ $id ][ 1 ];
if ( $expiration && $expiration < time ()) {
$this -> doDelete ( $id );
return false ;
}
return true ;
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
protected function doSave ( $id , $data , $lifeTime = 0 )
{
2017-04-13 15:53:35 +01:00
$this -> data [ $id ] = [ $data , $lifeTime ? time () + $lifeTime : false ];
2015-08-17 17:00:26 -07:00
return true ;
}
/**
* { @ inheritdoc }
*/
protected function doDelete ( $id )
{
unset ( $this -> data [ $id ]);
return true ;
}
/**
* { @ inheritdoc }
*/
protected function doFlush ()
{
2017-04-13 15:53:35 +01:00
$this -> data = [];
2015-08-17 17:00:26 -07:00
return true ;
}
/**
* { @ inheritdoc }
*/
protected function doGetStats ()
{
2017-04-13 15:53:35 +01:00
return [
Cache :: STATS_HITS => $this -> hitsCount ,
Cache :: STATS_MISSES => $this -> missesCount ,
Cache :: STATS_UPTIME => $this -> upTime ,
Cache :: STATS_MEMORY_USAGE => null ,
Cache :: STATS_MEMORY_AVAILABLE => null ,
];
2015-08-17 17:00:26 -07:00
}
}