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

PCC-63: Add caching for SmartComponentManager. #23

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ pcx_smart_components.api_component_schema:
_controller: '\Drupal\pcx_smart_components\Controller\ComponentSchemaController'
requirements:
_permission: 'access content'
pcx_smart_components:api_component_schema_component:
path: '/api/pantheoncloud/component_schema/{component_id}'
defaults:
_title: 'Smart Component Schema'
_controller: '\Drupal\pcx_smart_components\Controller\ComponentSchemaController::getComponent'
requirements:
_permission: 'access content'
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
pcx_smart_components.smart_component_manager:
class: Drupal\pcx_smart_components\Service\SmartComponentManager
arguments: ['@plugin.manager.sdc']
arguments: ['@plugin.manager.sdc', '@cache.default']
pcx_smart_components.renderer:
class: Drupal\pcx_smart_components\Service\SmartComponentRenderer
arguments: ['@logger.factory', '@pcx_smart_components.smart_component_manager']
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\pcx_smart_components\Controller;

use Drupal\Core\Cache\CacheableJsonResponse;
gauravgoyal marked this conversation as resolved.
Show resolved Hide resolved
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\pcx_smart_components\Service\SmartComponentManager;
Expand Down Expand Up @@ -42,7 +43,12 @@ public static function create(ContainerInterface $container) {
*/
public function __invoke(): JsonResponse {
$smartComponents = $this->smartComponentManager->getAllSmartComponents();
return new JsonResponse($smartComponents);
return new CacheableJsonResponse($smartComponents);
}

public function getComponent(string $component_id) {
$smartComponent = $this->smartComponentManager->getSmartComponent($component_id);
return new CacheableJsonResponse($smartComponent);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\pcx_smart_components\Service;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\sdc\Component\ComponentMetadata;
use Drupal\sdc\ComponentPluginManager;
use Drupal\sdc\Plugin\Component;
Expand All @@ -11,21 +12,36 @@
*/
class SmartComponentManager {

/**
* Cache ID.
*/
const CACHE_ID = 'pcx_smart_components';

/**
* SDC Component Plugin Manager.
*
* @var ComponentPluginManager $componentPluginManager
*/
protected ComponentPluginManager $componentPluginManager;

/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;

/**
* Constructs SmartComponentManager.
*
* @param ComponentPluginManager $componentPluginManager
* SDC Component Plugin Manager.
* @param CacheBackendInterface $cacheBackend
* Cache Backend Interface.
*/
public function __construct(ComponentPluginManager $componentPluginManager) {
public function __construct(ComponentPluginManager $componentPluginManager, CacheBackendInterface $cacheBackend) {
$this->componentPluginManager = $componentPluginManager;
$this->cacheBackend = $cacheBackend;
}

/**
Expand All @@ -35,6 +51,11 @@ public function __construct(ComponentPluginManager $componentPluginManager) {
* Array of smart components.
*/
public function getAllSmartComponents(): array {
$cachedData = $this->cacheBackend->get(self::CACHE_ID);
if (!empty($cachedData)) {
return $cachedData->data;
}

$pccComponents = $this->getAllPccComponents();

$smartComponents = [];
Expand All @@ -43,9 +64,31 @@ public function getAllSmartComponents(): array {
$smartComponents[strtoupper($componentId)] = $this->toSmartComponent($pccComponent);
}

$this->cacheBackend->set(self::CACHE_ID, $smartComponents);
return $smartComponents;
}

/**
* Get Smart Component converting available Drupal SDC components.
*
* @param string $componentId
* Component ID / name in uppercase.
*
* @return array
* Smart Component data in array.
*/
public function getSmartComponent(string $componentId): array {
$cachedData = $this->cacheBackend->get(self::CACHE_ID);
if (!empty($cachedData)
&& !empty($cachedData->data)
&& !empty($cachedData->data[$componentId])) {
return $cachedData->data[$componentId];
}

$smartComponents = $this->getAllSmartComponents();
return $smartComponents[$componentId] ?? [];
}

/**
* Finds SDC Component based on machineName.
*
Expand Down