Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cache] optimize Pimcore cache with doctrine entities #1843

Merged
merged 7 commits into from
Jan 21, 2022
16 changes: 15 additions & 1 deletion src/CoreShop/Bundle/ResourceBundle/CoreExtension/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@

namespace CoreShop\Bundle\ResourceBundle\CoreExtension;

use CoreShop\Bundle\ResourceBundle\Pimcore\CacheMarshallerInterface;
use CoreShop\Component\Resource\Model\ResourceInterface;
use CoreShop\Component\Resource\Repository\RepositoryInterface;
use Pimcore\Model;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Concrete;

/**
* @psalm-suppress InvalidReturnType, InvalidReturnStatement
*/
abstract class Select extends Data implements
Data\ResourcePersistenceAwareInterface,
Data\QueryResourcePersistenceAwareInterface,
Data\CustomRecyclingMarshalInterface
Data\CustomRecyclingMarshalInterface,
Data\CustomVersionMarshalInterface,
CacheMarshallerInterface
{
use Model\DataObject\Traits\SimpleComparisonTrait;

Expand Down Expand Up @@ -230,4 +234,14 @@ public function setAllowEmpty($allowEmpty)
{
$this->allowEmpty = $allowEmpty;
}

public function marshalForCache(Concrete $concrete, mixed $data): mixed
{
return $this->marshalVersion($concrete, $data);
}

public function unmarshalForCache(Concrete $concrete, mixed $data): mixed
{
return $this->unmarshalVersion($concrete, $data);
}
}
2 changes: 2 additions & 0 deletions src/CoreShop/Bundle/ResourceBundle/CoreShopResourceBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Composer\InstalledVersions;
use CoreShop\Bundle\CoreBundle\Application\Version;
use CoreShop\Bundle\ResourceBundle\DependencyInjection\Compiler\DoctrineTargetEntitiesResolverPass;
use CoreShop\Bundle\ResourceBundle\DependencyInjection\Compiler\PimcoreCachePass;
use CoreShop\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterInstallersPass;
use CoreShop\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterPimcoreRepositoriesPass;
use CoreShop\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterPimcoreResourcesPass;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new StackRepositoryPass());
$container->addCompilerPass(new RegisterPimcoreRepositoriesPass());
$container->addCompilerPass(new ValidatorAutoMappingFixPass());
$container->addCompilerPass(new PimcoreCachePass());
}

public static function registerDependentBundles(BundleCollection $collection): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

declare(strict_types=1);

namespace CoreShop\Bundle\ResourceBundle\DependencyInjection\Compiler;

use CoreShop\Bundle\ResourceBundle\Pimcore\CacheResourceMarshaller;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class PimcoreCachePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->getDefinition('pimcore.cache.adapter.pdo')->setArgument(4, []);
$container->getDefinition('pimcore.cache.adapter.pdo')->setArgument(5, new Reference(CacheResourceMarshaller::class));

$container->getDefinition('pimcore.cache.adapter.redis_tag_aware')->setArgument(4, new Reference(CacheResourceMarshaller::class));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

declare(strict_types=1);

namespace CoreShop\Bundle\ResourceBundle\Pimcore;

use Pimcore\Model\DataObject\Concrete;

interface CacheMarshallerInterface
{
public function marshalForCache(Concrete $concrete, mixed $data): mixed;

public function unmarshalForCache(Concrete $concrete, mixed $data): mixed;
}
103 changes: 103 additions & 0 deletions src/CoreShop/Bundle/ResourceBundle/Pimcore/CacheResourceMarshaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

declare(strict_types=1);

namespace CoreShop\Bundle\ResourceBundle\Pimcore;

use CoreShop\Component\Resource\Model\ResourceInterface;
use CoreShop\Component\Resource\Pimcore\Model\PimcoreModelInterface;
use Doctrine\ORM\EntityManagerInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Concrete;
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
use Symfony\Component\Cache\Marshaller\MarshallerInterface;

class CacheResourceMarshaller implements MarshallerInterface
{
protected MarshallerInterface $defaultMarshaller;
protected EntityManagerInterface $entityManager;

public function __construct(EntityManagerInterface $entityManager, MarshallerInterface $defaultMarshaller = null)
{
$this->defaultMarshaller = $defaultMarshaller ?? new DefaultMarshaller();
$this->entityManager = $entityManager;
}

public function marshall(array $values, ?array &$failed): array
{
foreach ($values as $data) {
if ($data instanceof Concrete) {
$class = $data->getClass();

if (!$class) {
continue;
}

/**
* @var Data&CacheMarshallerInterface $fd
* @psalm-var Data&CacheMarshallerInterface $fd
*/
foreach ($class->getFieldDefinitions() as $fd) {
if (!$fd instanceof CacheMarshallerInterface) {
continue;
}

if (!$fd instanceof Data) {
continue;
}

$data->setObjectVar(
$fd->getName(),
$fd->marshalForCache($data, $data->getObjectVar($fd->getName()))
);
dpfaffenbauer marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

return $this->defaultMarshaller->marshall($values, $failed);
}

public function unmarshall(string $value)
{
$data = $this->defaultMarshaller->unmarshall($value);

if ($data instanceof Concrete) {
$class = $data->getClass();

if (!$class) {
return $data;
}

/**
* @var Data&CacheMarshallerInterface $fd
* @psalm-var Data&CacheMarshallerInterface $fd
*/
foreach ($class->getFieldDefinitions() as $fd) {
if (!$fd instanceof CacheMarshallerInterface) {
continue;
}

if (!$fd instanceof Data) {
continue;
}

$data->setObjectVar(
$fd->getName(),
$fd->unmarshalForCache($data, $data->getObjectVar($fd->getName()))
);
}
}

return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,8 @@ services:
arguments:
- '@CoreShop\Component\Resource\Metadata\RegistryInterface'
- '@CoreShop\Component\Pimcore\DataObject\ObjectServiceInterface'

CoreShop\Bundle\ResourceBundle\Pimcore\CacheResourceMarshaller:
arguments:
- '@doctrine.orm.entity_manager'

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ services:
tags:
- { name: doctrine.event_subscriber, priority: 8192 }

CoreShop\Bundle\ResourceBundle\EventListener\DeepCopyListener:
tags:
- { name: kernel.event_subscriber }

CoreShop\Bundle\ResourceBundle\Doctrine\ResourceMappingDriverChain:
decorates: 'doctrine.orm.default_metadata_driver'
arguments:
Expand Down