Skip to content

24025 add caching for magento product version #24030

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/internal/Magento/Framework/App/ProductMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
namespace Magento\Framework\App;

use Magento\Framework\Composer\ComposerFactory;
use \Magento\Framework\Composer\ComposerJsonFinder;
use \Magento\Framework\App\Filesystem\DirectoryList;
use \Magento\Framework\Composer\ComposerInformation;
use Magento\Framework\Composer\ComposerJsonFinder;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Composer\ComposerInformation;

/**
* Class ProductMetadata
*
* @package Magento\Framework\App
*/
class ProductMetadata implements ProductMetadataInterface
Expand All @@ -28,6 +29,11 @@ class ProductMetadata implements ProductMetadataInterface
*/
const PRODUCT_NAME = 'Magento';

/**
* Magento Product Version Cache Key
*/
private const MAGENTO_PRODUCT_VERSION_CACHE_KEY = 'magento-product-version';

/**
* Product version
*
Expand All @@ -46,12 +52,19 @@ class ProductMetadata implements ProductMetadataInterface
*/
private $composerInformation;

/**
* @var CacheInterface
*/
private $cache;

/**
* @param ComposerJsonFinder $composerJsonFinder
* @param CacheInterface|null $cache
*/
public function __construct(ComposerJsonFinder $composerJsonFinder)
public function __construct(ComposerJsonFinder $composerJsonFinder, CacheInterface $cache = null)
{
$this->composerJsonFinder = $composerJsonFinder;
$this->cache = $cache ?: ObjectManager::getInstance()->get(CacheInterface::class);
}

/**
Expand All @@ -61,6 +74,9 @@ public function __construct(ComposerJsonFinder $composerJsonFinder)
*/
public function getVersion()
{
if ($cachedVersion = $this->cache->load(self::MAGENTO_PRODUCT_VERSION_CACHE_KEY)) {
$this->version = $cachedVersion;
}
if (!$this->version) {
if (!($this->version = $this->getSystemPackageVersion())) {
if ($this->getComposerInformation()->isMagentoRoot()) {
Expand All @@ -69,6 +85,7 @@ public function getVersion()
$this->version = 'UNKNOWN';
}
}
$this->cache->save($this->version, self::MAGENTO_PRODUCT_VERSION_CACHE_KEY);
}
return $this->version;
}
Expand Down