diff --git a/Model/Configuration.php b/Model/Configuration.php index f706ef55..3dcc5347 100644 --- a/Model/Configuration.php +++ b/Model/Configuration.php @@ -170,7 +170,7 @@ class Configuration implements ConfigurationInterface * @param ModuleListInterface $moduleList * @param ProductMetadataInterface $productMetadata * @param CloudinaryLogger $cloudinaryLogger - * @param registry $coreRegistry + * @param Registry $coreRegistry */ public function __construct( ScopeConfigInterface $configReader, @@ -182,7 +182,7 @@ public function __construct( ModuleListInterface $moduleList, ProductMetadataInterface $productMetadata, CloudinaryLogger $cloudinaryLogger, - registry $coreRegistry + Registry $coreRegistry ) { $this->configReader = $configReader; $this->configWriter = $configWriter; @@ -204,6 +204,14 @@ public function getStoreManager() return $this->storeManager; } + /** + * @return Registry + */ + public function getCoreRegistry() + { + return $this->coreRegistry; + } + /** * @return Cloud */ diff --git a/Model/SynchronisationChecker.php b/Model/SynchronisationChecker.php index cd672916..7a380816 100644 --- a/Model/SynchronisationChecker.php +++ b/Model/SynchronisationChecker.php @@ -6,9 +6,15 @@ use Cloudinary\Cloudinary\Core\AutoUploadMapping\AutoUploadConfigurationInterface; use Cloudinary\Cloudinary\Core\ConfigurationInterface; use Cloudinary\Cloudinary\Core\Image\SynchronizationCheck; +use Magento\Framework\Registry; class SynchronisationChecker implements SynchronizationCheck { + /** + * @var string + */ + private $imageNameCacheKey; + /** * @var SynchronisationRepositoryInterface */ @@ -29,30 +35,60 @@ class SynchronisationChecker implements SynchronizationCheck */ private $mediaLibraryMapFactory; + /** + * @var Registry + */ + private $coreRegistry; + /** * @method __construct * @param SynchronisationRepositoryInterface $synchronisationRepository * @param ConfigurationInterface $configuration * @param AutoUploadConfigurationInterface $autoUploadConfiguration * @param MediaLibraryMapFactory $mediaLibraryMapFactory + * @param Registry $coreRegistry */ public function __construct( SynchronisationRepositoryInterface $synchronisationRepository, ConfigurationInterface $configuration, AutoUploadConfigurationInterface $autoUploadConfiguration, - MediaLibraryMapFactory $mediaLibraryMapFactory + MediaLibraryMapFactory $mediaLibraryMapFactory, + Registry $coreRegistry ) { $this->synchronisationRepository = $synchronisationRepository; $this->configuration = $configuration; $this->autoUploadConfiguration = $autoUploadConfiguration; $this->mediaLibraryMapFactory = $mediaLibraryMapFactory; + $this->coreRegistry = $coreRegistry; + } + + /** + * @method cacheResult + * @param bool $result + * @return mixed + */ + private function cacheResult($result) + { + $this->coreRegistry->unregister($this->imageNameCacheKey); + $this->coreRegistry->register($this->imageNameCacheKey, $result); + return $result; } /** - * @param $imageName + * @method cacheResult + * @return mixed + */ + private function getFromCache() + { + return $this->coreRegistry->registry($this->imageNameCacheKey); + } + + /** + * @param string $imageName + * @param bool $refresh * @return bool */ - public function isSynchronized($imageName) + public function isSynchronized($imageName, $refresh = false) { if (!$imageName) { return false; @@ -62,17 +98,22 @@ public function isSynchronized($imageName) return true; } + $this->imageNameCacheKey = 'cldsynccheckcachekey_' . (string) $imageName; + if (!$refresh && ($cacheResult = $this->getFromCache()) !== null) { + return $cacheResult; + } + if ($this->configuration->isEnabledLocalMapping()) { //Look for a match on the mapping table: preg_match('/(cld_[A-Za-z0-9]{13}_).+$/i', $imageName, $cldUniqid); if ($cldUniqid && isset($cldUniqid[1])) { $mapped = $this->mediaLibraryMapFactory->create()->getCollection()->addFieldToFilter("cld_uniqid", $cldUniqid[1])->setPageSize(1)->getFirstItem(); if ($mapped && ($origPublicId = $mapped->getCldPublicId())) { - return true; + return $this->cacheResult(true); } } } - return $this->synchronisationRepository->getListByImagePath($imageName)->getTotalCount() > 0; + return $this->cacheResult($this->synchronisationRepository->isSynchronizedImagePath($imageName)); } } diff --git a/Model/SynchronisationRepository.php b/Model/SynchronisationRepository.php index 5deffd47..144bb53d 100644 --- a/Model/SynchronisationRepository.php +++ b/Model/SynchronisationRepository.php @@ -2,19 +2,18 @@ namespace Cloudinary\Cloudinary\Model; -use Cloudinary\Cloudinary\Core\SynchroniseAssetsRepositoryInterface; - use Cloudinary\Cloudinary\Api\SynchronisationRepositoryInterface; -use Cloudinary\Cloudinary\Model\SynchronisationFactory; -use Cloudinary\Cloudinary\Model\ResourceModel\Synchronisation\CollectionFactory; + +use Cloudinary\Cloudinary\Core\SynchroniseAssetsRepositoryInterface; use Cloudinary\Cloudinary\Model\ResourceModel\Synchronisation\Collection as SynchronisationCollection; +use Cloudinary\Cloudinary\Model\ResourceModel\Synchronisation\CollectionFactory; -use Magento\Framework\Api\AbstractSimpleObject; use Magento\Framework\Api\FilterBuilder; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\Api\SearchCriteriaInterface; use Magento\Framework\Api\SearchResultsInterface; use Magento\Framework\Api\SearchResultsInterfaceFactory; +use Magento\Framework\App\ResourceConnection; class SynchronisationRepository implements SynchronisationRepositoryInterface, SynchroniseAssetsRepositoryInterface { @@ -48,6 +47,11 @@ class SynchronisationRepository implements SynchronisationRepositoryInterface, S */ private $synchronisationFactory; + /** + * @var ResourceConnection + */ + private $connection; + /** * @param FilterBuilder $filterBuilder * @param SearchCriteriaBuilder $searchCriteriaBuilder @@ -62,7 +66,8 @@ public function __construct( CollectionFactory $collectionFactory, SearchResultsInterface $searchResult, SearchResultsInterfaceFactory $searchResultsFactory, - SynchronisationFactory $synchronisationFactory + SynchronisationFactory $synchronisationFactory, + ResourceConnection $resourceConnection ) { $this->filterBuilder = $filterBuilder; $this->searchCriteriaBuilder = $searchCriteriaBuilder; @@ -70,6 +75,7 @@ public function __construct( $this->searchResult = $searchResult; $this->searchResultsFactory = $searchResultsFactory; $this->synchronisationFactory = $synchronisationFactory; + $this->connection = $resourceConnection->getConnection(); } /** @@ -98,6 +104,9 @@ public function getList(SearchCriteriaInterface $searchCriteria) } /** + * @deprecated + * For checking if image path is synchronized, use isSynchronizedImagePath() + * * @param string $imagePath * * @return SearchResultsInterface @@ -109,6 +118,22 @@ public function getListByImagePath($imagePath) return $this->getList($this->searchCriteriaBuilder->create()); } + /** + * @param string $imagePath + * + * @return bool + */ + public function isSynchronizedImagePath($imagePath) + { + return $this->connection->fetchAll($this->connection->select() + ->from( + $this->connection->getTableName("cloudinary_synchronisation"), + ['cloudinary_synchronisation_id'] + ) + ->where('image_path = ?', $imagePath) + ->limit(1)) ? true : false; + } + /** * @param string $imagePath */ diff --git a/Model/Transformation.php b/Model/Transformation.php index 205842a7..954f53cd 100644 --- a/Model/Transformation.php +++ b/Model/Transformation.php @@ -2,10 +2,9 @@ namespace Cloudinary\Cloudinary\Model; -use Cloudinary\Cloudinary\Model\ResourceModel\Transformation as TransformationResourceModel; -use Cloudinary\Cloudinary\Model\Configuration; -use Cloudinary\Cloudinary\Core\Image\Transformation\Freeform; use Cloudinary\Cloudinary\Core\Image\Transformation as ImageTransformation; +use Cloudinary\Cloudinary\Core\Image\Transformation\Freeform; +use Cloudinary\Cloudinary\Model\ResourceModel\Transformation as TransformationResourceModel; use Magento\Framework\Data\Collection\AbstractDb; use Magento\Framework\Model\AbstractModel; use Magento\Framework\Model\Context; diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 06f2b730..e335e041 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -33,6 +33,14 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con $this->createProductSpinsetMapTable($setup); } + if (version_compare($context->getVersion(), '1.14.9', '<')) { + $setup->getConnection()->addIndex( + $setup->getTable('cloudinary_synchronisation'), + $setup->getIdxName('cloudinary_synchronisation', ['image_path']), + ['image_path'] + ); + } + $setup->endSetup(); } diff --git a/composer.json b/composer.json index f507f251..ba3214c5 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "cloudinary/cloudinary-magento2", "description": "Cloudinary Magento 2 Integration.", "type": "magento2-module", - "version": "1.14.8", + "version": "1.14.9", "license": "MIT", "require": { "cloudinary/cloudinary_php": "^1.20.0" diff --git a/etc/module.xml b/etc/module.xml index 87cebd17..fc4a7797 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/marketplace.composer.json b/marketplace.composer.json index 425e670d..c824feeb 100644 --- a/marketplace.composer.json +++ b/marketplace.composer.json @@ -2,7 +2,7 @@ "name": "cloudinary/cloudinary", "description": "Cloudinary Magento 2 Integration.", "type": "magento2-module", - "version": "1.14.8", + "version": "1.14.9", "license": "MIT", "require": { "cloudinary/cloudinary_php": "^1.20.0"