mirror of
https://github.com/ToxicCrack/PrintABrick.git
synced 2025-05-17 12:50:08 -07:00
Cache relations
This commit is contained in:
parent
7d130d1666
commit
80bea7f0c9
@ -24,6 +24,9 @@ services:
|
|||||||
public: true
|
public: true
|
||||||
tags: ['controller.service_arguments']
|
tags: ['controller.service_arguments']
|
||||||
|
|
||||||
|
app.relations.cache_provider:
|
||||||
|
class: Doctrine\Common\Cache\ArrayCache
|
||||||
|
|
||||||
app.brickset.cache_provider:
|
app.brickset.cache_provider:
|
||||||
class: Doctrine\Common\Cache\PhpFileCache
|
class: Doctrine\Common\Cache\PhpFileCache
|
||||||
arguments: ["%kernel.cache_dir%/brickset", ".cache.php"]
|
arguments: ["%kernel.cache_dir%/brickset", ".cache.php"]
|
||||||
@ -76,6 +79,8 @@ services:
|
|||||||
$rebrickableDownloadUrl: '%app.rebrickable_downloads_url%'
|
$rebrickableDownloadUrl: '%app.rebrickable_downloads_url%'
|
||||||
|
|
||||||
AppBundle\Util\RelationMapper:
|
AppBundle\Util\RelationMapper:
|
||||||
|
arguments:
|
||||||
|
- '@app.relations.cache_provider'
|
||||||
calls:
|
calls:
|
||||||
- [ loadResource, ['%kernel.root_dir%/Resources/relations/part_model.yml', 'part_model']]
|
- [ loadResource, ['%kernel.root_dir%/Resources/relations/part_model.yml', 'part_model']]
|
||||||
- [ loadResource, ['%kernel.root_dir%/Resources/relations/alias_model.yml', 'alias_model']]
|
- [ loadResource, ['%kernel.root_dir%/Resources/relations/alias_model.yml', 'alias_model']]
|
||||||
|
@ -4,6 +4,7 @@ namespace AppBundle\Util;
|
|||||||
|
|
||||||
use AppBundle\Exception\RelationMapper\InvalidResourceException;
|
use AppBundle\Exception\RelationMapper\InvalidResourceException;
|
||||||
use AppBundle\Exception\RelationMapper\ResourceNotFoundException;
|
use AppBundle\Exception\RelationMapper\ResourceNotFoundException;
|
||||||
|
use Doctrine\Common\Cache\CacheProvider;
|
||||||
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException;
|
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException;
|
||||||
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
|
||||||
use Symfony\Component\Yaml\Exception\ParseException;
|
use Symfony\Component\Yaml\Exception\ParseException;
|
||||||
@ -16,6 +17,16 @@ class RelationMapper
|
|||||||
*/
|
*/
|
||||||
private $relations;
|
private $relations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RelationMapper constructor.
|
||||||
|
* @param CacheProvider $cache
|
||||||
|
*/
|
||||||
|
public function __construct(CacheProvider $cache)
|
||||||
|
{
|
||||||
|
$this->cache = $cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a Resource.
|
* Adds a Resource.
|
||||||
*
|
*
|
||||||
@ -31,8 +42,13 @@ class RelationMapper
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (!$data = unserialize($this->cache->fetch($domain))) {
|
||||||
|
$data = Yaml::parse(file_get_contents($file),yaml::PARSE_KEYS_AS_STRINGS);
|
||||||
|
$this->cache->save($domain, serialize($data), 60);
|
||||||
|
}
|
||||||
|
|
||||||
$this->relations[$domain] = [];
|
$this->relations[$domain] = [];
|
||||||
$this->relations[$domain] = Yaml::parse(file_get_contents($file));
|
$this->relations[$domain] = $data;
|
||||||
} catch (ParseException $e) {
|
} catch (ParseException $e) {
|
||||||
throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $file), 0, $e);
|
throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $file), 0, $e);
|
||||||
}
|
}
|
||||||
|
15
tests/AppBundle/Controller/DefaultControllerTest.php
Normal file
15
tests/AppBundle/Controller/DefaultControllerTest.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: hubnedav
|
||||||
|
* Date: 8.6.17
|
||||||
|
* Time: 16:05
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tests\AppBundle\Controller;
|
||||||
|
|
||||||
|
|
||||||
|
class DefaultControllerTest
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
namespace Tests\AppBundle\Util\RelationMapper;
|
namespace Tests\AppBundle\Util\RelationMapper;
|
||||||
|
|
||||||
use AppBundle\Util\RelationMapper;
|
use AppBundle\Util\RelationMapper;
|
||||||
|
use Doctrine\Common\Cache\ArrayCache;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\Config\Resource\FileResource;
|
use Symfony\Component\Config\Resource\FileResource;
|
||||||
use Symfony\Component\Yaml\Yaml;
|
use Symfony\Component\Yaml\Yaml;
|
||||||
@ -11,7 +12,7 @@ class RelationMapperTest extends TestCase
|
|||||||
{
|
{
|
||||||
public function testLoad()
|
public function testLoad()
|
||||||
{
|
{
|
||||||
$mapper = new RelationMapper();
|
$mapper = new RelationMapper(new ArrayCache());
|
||||||
$mapper->loadResource(__DIR__.'/fixtures/resources.yml', 'resources');
|
$mapper->loadResource(__DIR__.'/fixtures/resources.yml', 'resources');
|
||||||
|
|
||||||
$this->assertEquals('bar', $mapper->find('foo','resources'));
|
$this->assertEquals('bar', $mapper->find('foo','resources'));
|
||||||
@ -23,7 +24,7 @@ class RelationMapperTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testLoadNonExistingResource()
|
public function testLoadNonExistingResource()
|
||||||
{
|
{
|
||||||
$mapper = new RelationMapper();
|
$mapper = new RelationMapper(new ArrayCache());
|
||||||
$resource = __DIR__.'/fixtures/non-existing.yml';
|
$resource = __DIR__.'/fixtures/non-existing.yml';
|
||||||
$mapper->loadResource($resource, 'resources');
|
$mapper->loadResource($resource, 'resources');
|
||||||
}
|
}
|
||||||
@ -33,7 +34,7 @@ class RelationMapperTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testLoadInvalidResource()
|
public function testLoadInvalidResource()
|
||||||
{
|
{
|
||||||
$mapper = new RelationMapper();
|
$mapper = new RelationMapper(new ArrayCache());
|
||||||
$resource = __DIR__.'/fixtures/invalid.yml';
|
$resource = __DIR__.'/fixtures/invalid.yml';
|
||||||
$mapper->loadResource($resource, 'resources');
|
$mapper->loadResource($resource, 'resources');
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user