This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
drupalcampbristol/vendor/doctrine/cache/lib/Doctrine/Common/Cache/ArrayCache.php

114 lines
2 KiB
PHP
Raw Normal View History

<?php
namespace Doctrine\Common\Cache;
2018-11-23 12:29:20 +00:00
use function time;
/**
* 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 14:53:35 +00:00
private $data = [];
2018-11-23 12:29:20 +00:00
/** @var int */
2017-04-13 14:53:35 +00:00
private $hitsCount = 0;
2018-11-23 12:29:20 +00:00
/** @var int */
2017-04-13 14:53:35 +00:00
private $missesCount = 0;
2018-11-23 12:29:20 +00:00
/** @var int */
2017-04-13 14:53:35 +00:00
private $upTime;
/**
* {@inheritdoc}
*/
public function __construct()
{
$this->upTime = time();
}
/**
* {@inheritdoc}
*/
protected function doFetch($id)
{
2017-04-13 14:53:35 +00:00
if (! $this->doContains($id)) {
$this->missesCount += 1;
return false;
}
$this->hitsCount += 1;
return $this->data[$id][0];
}
/**
* {@inheritdoc}
*/
protected function doContains($id)
{
2017-04-13 14:53:35 +00:00
if (! isset($this->data[$id])) {
return false;
}
$expiration = $this->data[$id][1];
if ($expiration && $expiration < time()) {
$this->doDelete($id);
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
protected function doSave($id, $data, $lifeTime = 0)
{
2017-04-13 14:53:35 +00:00
$this->data[$id] = [$data, $lifeTime ? time() + $lifeTime : false];
return true;
}
/**
* {@inheritdoc}
*/
protected function doDelete($id)
{
unset($this->data[$id]);
return true;
}
/**
* {@inheritdoc}
*/
protected function doFlush()
{
2017-04-13 14:53:35 +00:00
$this->data = [];
return true;
}
/**
* {@inheritdoc}
*/
protected function doGetStats()
{
2017-04-13 14:53:35 +00: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,
];
}
}