-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from discoverygarden/fix/caching
SUP-6613: Improve caching
- Loading branch information
Showing
8 changed files
with
125 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Drupal\embargo\Cache\Context; | ||
|
||
use Drupal\Core\Cache\CacheableMetadata; | ||
use Drupal\Core\Cache\Context\CacheContextInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Session\AccountInterface; | ||
|
||
/** | ||
* Context based of the given user being exempt from _any_ embargoes. | ||
*/ | ||
class UserExemptedCacheContext implements CacheContextInterface { | ||
|
||
/** | ||
* Memoize if exempted. | ||
* | ||
* @var bool | ||
*/ | ||
protected bool $exempted; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct( | ||
protected AccountInterface $currentUser, | ||
protected EntityTypeManagerInterface $entityTypeManager, | ||
) { | ||
// No-op, other than stashing properties. | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function getLabel() { | ||
return \t('User, has any embargo exemptions'); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getContext() { | ||
return $this->isExempted() ? '1' : '0'; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getCacheableMetadata() { | ||
return (new CacheableMetadata()) | ||
->addCacheContexts([$this->isExempted() ? 'user' : 'user.permissions']) | ||
->addCacheTags(['embargo_list']); | ||
} | ||
|
||
/** | ||
* Determine if the current user has _any_ exemptions. | ||
* | ||
* @return bool | ||
* TRUE if the user is exempt to at least one embargo; otherwise, FALSE. | ||
* | ||
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException | ||
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException | ||
*/ | ||
protected function isExempted() : bool { | ||
if (!isset($this->exempted)) { | ||
$results = $this->entityTypeManager->getStorage('embargo')->getQuery() | ||
->accessCheck(FALSE) | ||
->condition('exempt_users', $this->currentUser->id()) | ||
->range(0, 1) | ||
->execute(); | ||
$this->exempted = !empty($results); | ||
} | ||
|
||
return $this->exempted; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters