Skip to content

Commit

Permalink
Cache workgroup api response for only a short time
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed May 28, 2024
1 parent 7fa15c9 commit 21d9a99
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
31 changes: 12 additions & 19 deletions src/Service/WorkgroupApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\stanford_samlauth\Service;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Site\Settings;
Expand All @@ -17,20 +18,6 @@ class WorkgroupApi implements WorkgroupApiInterface {

const WORKGROUP_API = 'https://workgroupsvc.stanford.edu/workgroups/2.0';

/**
* Config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Guzzle client service.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $guzzle;

/**
* Logger channel service.
*
Expand All @@ -55,16 +42,16 @@ class WorkgroupApi implements WorkgroupApiInterface {
/**
* StanfordSSPWorkgroupApi constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory service.
* @param \GuzzleHttp\ClientInterface $guzzle
* Http client guzzle service.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger
* Logger channel factory service.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* Caching service.
*/
public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $guzzle, LoggerChannelFactoryInterface $logger) {
$this->configFactory = $config_factory;
$this->guzzle = $guzzle;
public function __construct(protected ConfigFactoryInterface $configFactory, protected ClientInterface $guzzle, LoggerChannelFactoryInterface $logger, protected CacheBackendInterface $cache) {
$this->logger = $logger->get('stanford_samlauth');

$config = $this->configFactory->get('stanford_samlauth.settings');
Expand Down Expand Up @@ -159,6 +146,10 @@ public function isSunetValid(string $sunet): bool {
* API response or false if fails.
*/
protected function callApi(string $workgroup = NULL, string $sunet = NULL): ?array {
$cached_data = $this->cache->get("samlauth:$workgroup:$sunet");
if ($cached_data) {
return $cached_data->data;
}
$config = $this->configFactory->get('stanford_samlauth.settings');
$options = [
'cert' => $this->getCert(),
Expand All @@ -173,7 +164,9 @@ protected function callApi(string $workgroup = NULL, string $sunet = NULL): ?arr
$api_url = Settings::get('stanford_samlauth.workgroup_api', self::WORKGROUP_API);
try {
$result = $this->guzzle->request('GET', $api_url, $options);
return json_decode($result->getBody(), TRUE);
$result = json_decode($result->getBody(), TRUE, 512, JSON_THROW_ON_ERROR);
$this->cache->set("samlauth:$workgroup:$sunet", $result, time() + ini_get('max_execution_time') ?: 60);
return $result;
}
catch (GuzzleException $e) {
$this->logger->error('Unable to connect to workgroup api. @message', ['@message' => $e->getMessage()]);
Expand Down
2 changes: 1 addition & 1 deletion stanford_samlauth.services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
stanford_samlauth.workgroup_api:
class: Drupal\stanford_samlauth\Service\WorkgroupApi
arguments: [ '@config.factory', '@http_client', '@logger.factory' ]
arguments: [ '@config.factory', '@http_client', '@logger.factory', '@cache.default' ]

stanford_samlauth.event_subscriber:
class: Drupal\stanford_samlauth\EventSubscriber\StanfordSamlAuthSubscriber
Expand Down

0 comments on commit 21d9a99

Please sign in to comment.