Skip to content

Commit

Permalink
PCC-63: Add caching for SmartComponentManager. (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
purushotamrai authored Jun 19, 2024
1 parent ee554a3 commit 3485902
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
7 changes: 7 additions & 0 deletions modules/pcx_smart_components/pcx_smart_components.routing.yml
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;
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

0 comments on commit 3485902

Please sign in to comment.