data cache usage

This commit is contained in:
Yuri Kuznetsov
2020-09-21 11:30:07 +03:00
parent 0020a5ea4a
commit e17b88ca74
2 changed files with 34 additions and 32 deletions

View File

@@ -29,13 +29,19 @@
namespace Espo\Core\Utils;
use Espo\Core\Exceptions\Error;
use Espo\Core\{
Exceptions\Error,
Utils\Config,
Utils\Metadata,
Utils\File\Manager as FileManager,
Utils\DataCache,
};
class Route
{
protected $data = null;
protected $cacheFile = 'data/cache/application/routes.php';
protected $cacheKey = 'routes';
protected $paths = [
'corePath' => 'application/Espo/Resources/routes.json',
@@ -43,15 +49,17 @@ class Route
'customPath' => 'custom/Espo/Custom/Resources/routes.json',
];
private $fileManager;
private $config;
private $metadata;
private $fileManager;
private $dataCache;
public function __construct(Config $config, Metadata $metadata, File\Manager $fileManager)
public function __construct(Config $config, Metadata $metadata, FileManager $fileManager, DataCache $dataCache)
{
$this->config = $config;
$this->metadata = $metadata;
$this->fileManager = $fileManager;
$this->dataCache = $dataCache;
}
/**
@@ -70,8 +78,8 @@ class Route
{
$useCache = $this->config->get('useCache');
if (file_exists($this->cacheFile) && $useCache) {
$this->data = $this->fileManager->getPhpContents($this->cacheFile);
if ($this->dataCache->has($this->cacheKey) && $useCache) {
$this->data = $this->dataCache->get($this->cacheKey);
return;
}
@@ -79,17 +87,10 @@ class Route
$this->data = $this->unify();
if ($useCache) {
$result = $this->fileManager->putPhpContents($this->cacheFile, $this->data);
if ($result == false) {
throw new Error('Route - Cannot save unified routes');
}
$this->dataCache->store($this->cacheKey, $this->data);
}
}
/**
* Unify routes.
*/
protected function unify() : array
{
$data = $this->addDataFromFile([], $this->paths['customPath']);

View File

@@ -31,6 +31,14 @@ namespace tests\unit\Espo\Core\Utils;
use tests\unit\ReflectionHelper;
use Espo\Core\{
Utils\Route,
Utils\Config,
Utils\File\Manager as FileManager,
Utils\Metadata,
Utils\DataCache,
};
class RouteTest extends \PHPUnit\Framework\TestCase
{
protected $object;
@@ -41,24 +49,17 @@ class RouteTest extends \PHPUnit\Framework\TestCase
protected function setUp() : void
{
$this->objects['container'] = $this->getMockBuilder('\\Espo\\Core\\Container')->disableOriginalConstructor()->getMock();
$this->config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
$this->fileManager = new FileManager();
$this->objects['config'] = $this->getMockBuilder('\\Espo\\Core\\Utils\\Config')->disableOriginalConstructor()->getMock();
$this->objects['fileManager'] = new \Espo\Core\Utils\File\Manager();
$this->objects['metadata'] = $this->getMockBuilder('\\Espo\\Core\\Utils\\Metadata')->disableOriginalConstructor()->getMock();
$this->metadata = $this->getMockBuilder(Metadata::class)->disableOriginalConstructor()->getMock();
$map = array(
array('config', $this->objects['config']),
array('metadata', $this->objects['metadata']),
array('fileManager', $this->objects['fileManager']),
$this->dataCache = $this->getMockBuilder(DataCache::class)->disableOriginalConstructor()->getMock();
$this->object = new Route(
$this->config, $this->metadata, $this->fileManager, $this->dataCache
);
$this->objects['container']
->expects($this->any())
->method('get')
->will($this->returnValueMap($map));
$this->object = new \Espo\Core\Utils\Route($this->objects['config'], $this->objects['metadata'], $this->objects['fileManager']);
$this->reflection = new ReflectionHelper($this->object);
}
@@ -76,7 +77,7 @@ class RouteTest extends \PHPUnit\Framework\TestCase
'customPath' => $this->filesPath . '/testCase1/custom/Espo/Custom/Resources/routes.json',
));
$this->objects['metadata']
$this->metadata
->expects($this->once())
->method('getModuleList')
->will($this->returnValue(array(
@@ -165,7 +166,7 @@ class RouteTest extends \PHPUnit\Framework\TestCase
'customPath' => $this->filesPath . '/testCase2/custom/Espo/Custom/Resources/routes.json',
));
$this->objects['metadata']
$this->metadata
->expects($this->once())
->method('getModuleList')
->will($this->returnValue(array(
@@ -252,7 +253,7 @@ class RouteTest extends \PHPUnit\Framework\TestCase
'customPath' => $this->filesPath . '/testCase3/custom/Espo/Custom/Resources/routes.json',
));
$this->objects['metadata']
$this->metadata
->expects($this->once())
->method('getModuleList')
->will($this->returnValue(array(
@@ -342,7 +343,7 @@ class RouteTest extends \PHPUnit\Framework\TestCase
$this->reflection->setProperty('paths', $paths);
$this->objects['metadata']
$this->metadata
->expects($this->once())
->method('getModuleList')
->will($this->returnValue(array(