Provide array cache implementation
doctrine/cache 2.0 no longer provides implementations of caches. It provides only interfaces. We need only PHP array cache so I chose to implement this simple cache instead of introducing new cache library. [MAILPOET-3892]
This commit is contained in:
committed by
Veljko V
parent
35271bcaf9
commit
503283fa63
97
lib/Doctrine/ArrayCache.php
Normal file
97
lib/Doctrine/ArrayCache.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Doctrine;
|
||||
|
||||
use MailPoetVendor\Doctrine\Common\Cache\CacheProvider;
|
||||
|
||||
/**
|
||||
* Array cache
|
||||
* Based on https://github.com/doctrine/cache/blob/1.11.x/lib/Doctrine/Common/Cache/ArrayCache.php
|
||||
* The cache implementation was removed from the doctrine/cache v2.0 so we need to provide own implementation.
|
||||
*/
|
||||
class ArrayCache extends CacheProvider
|
||||
{
|
||||
/** @var mixed[] */
|
||||
private $data = [];
|
||||
|
||||
/** @var int */
|
||||
private $hitsCount = 0;
|
||||
|
||||
/** @var int */
|
||||
private $missesCount = 0;
|
||||
|
||||
/** @var int */
|
||||
private $upTime;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->upTime = time();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doFetch($id) {
|
||||
if (! $this->doContains($id)) {
|
||||
$this->missesCount += 1;
|
||||
return false;
|
||||
}
|
||||
$this->hitsCount += 1;
|
||||
return $this->data[$id][0];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doContains($id) {
|
||||
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) {
|
||||
$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() {
|
||||
$this->data = [];
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doGetStats() {
|
||||
return [
|
||||
CacheProvider::STATS_HITS => $this->hitsCount,
|
||||
CacheProvider::STATS_MISSES => $this->missesCount,
|
||||
CacheProvider::STATS_UPTIME => $this->upTime,
|
||||
CacheProvider::STATS_MEMORY_USAGE => null,
|
||||
CacheProvider::STATS_MEMORY_AVAILABLE => null,
|
||||
];
|
||||
}
|
||||
}
|
@@ -3,7 +3,6 @@
|
||||
namespace MailPoet\Doctrine;
|
||||
|
||||
use MailPoet\Doctrine\Annotations\AnnotationReaderProvider;
|
||||
use MailPoetVendor\Doctrine\Common\Cache\ArrayCache;
|
||||
use MailPoetVendor\Doctrine\Common\Proxy\AbstractProxyFactory;
|
||||
use MailPoetVendor\Doctrine\ORM\Configuration;
|
||||
use MailPoetVendor\Doctrine\ORM\Mapping\Driver\AnnotationDriver;
|
||||
|
@@ -3,11 +3,10 @@
|
||||
namespace MailPoet\Test\Config;
|
||||
|
||||
use MailPoet\Doctrine\Annotations\AnnotationReaderProvider;
|
||||
use MailPoet\Doctrine\ArrayCache;
|
||||
use MailPoet\Doctrine\ConfigurationFactory;
|
||||
use MailPoet\Doctrine\MetadataCache;
|
||||
use MailPoet\Doctrine\PSRMetadataCache;
|
||||
use MailPoet\Doctrine\TablePrefixMetadataFactory;
|
||||
use MailPoetVendor\Doctrine\Common\Cache\ArrayCache;
|
||||
use MailPoetVendor\Doctrine\Common\Proxy\AbstractProxyFactory;
|
||||
use MailPoetVendor\Doctrine\ORM\Configuration;
|
||||
use MailPoetVendor\Doctrine\ORM\Mapping\Driver\AnnotationDriver;
|
||||
|
@@ -3,6 +3,7 @@
|
||||
namespace MailPoet\Test\Doctrine\EventListeners;
|
||||
|
||||
use MailPoet\Doctrine\Annotations\AnnotationReaderProvider;
|
||||
use MailPoet\Doctrine\ArrayCache;
|
||||
use MailPoet\Doctrine\ConfigurationFactory;
|
||||
use MailPoet\Doctrine\EntityManagerFactory;
|
||||
use MailPoet\Doctrine\EventListeners\EmojiEncodingListener;
|
||||
@@ -13,7 +14,6 @@ use MailPoet\Doctrine\Validator\ValidationException;
|
||||
use MailPoet\Doctrine\Validator\ValidatorFactory;
|
||||
use MailPoet\WP\Emoji;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoetVendor\Doctrine\Common\Cache\ArrayCache;
|
||||
|
||||
require_once __DIR__ . '/ValidatedEntity.php';
|
||||
|
||||
|
@@ -4,6 +4,7 @@ namespace MailPoet\Test\Doctrine\EventListeners;
|
||||
|
||||
use Exception;
|
||||
use MailPoet\Doctrine\Annotations\AnnotationReaderProvider;
|
||||
use MailPoet\Doctrine\ArrayCache;
|
||||
use MailPoet\Doctrine\ConfigurationFactory;
|
||||
use MailPoet\Doctrine\EntityManagerFactory;
|
||||
use MailPoet\Doctrine\EventListeners\EmojiEncodingListener;
|
||||
@@ -14,7 +15,6 @@ use MailPoet\Doctrine\Validator\ValidatorFactory;
|
||||
use MailPoet\Test\Doctrine\Types\JsonEntity;
|
||||
use MailPoet\WP\Emoji;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use MailPoetVendor\Doctrine\Common\Cache\ArrayCache;
|
||||
use RuntimeException;
|
||||
|
||||
require_once __DIR__ . '/JsonEntity.php';
|
||||
|
Reference in New Issue
Block a user