Issue #3437576: Provide a new user.shortcut_set cache context
6 unresolved threads
Closes #3437576
Merge request reports
Activity
added 1 commit
added 6 commits
-
d63ae62f...61ac8eb2 - 5 commits from branch
project:11.x
- dae0b815 - Merge remote-tracking branch 'origin/11.x' into 3437576-provide-user-shortcut-ser
-
d63ae62f...61ac8eb2 - 5 commits from branch
64 $this->user = User::create([ 65 'name' => 'first', 66 'mail' => 'first@example.com', 67 ]); 68 $this->user->save(); 69 70 $this->entityTypeManager = \Drupal::entityTypeManager(); 71 } 72 73 /** 74 * Tests cache context behavior depending on shortcut set assignments. 75 * 76 * @covers ::getContext 77 * @covers ::getCacheableMetadata 78 */ 79 public function testCacheContext() { changed this line in version 6 of the diff
33 */ 34 public static function getLabel() { 35 return t("User's shortcut set"); 36 } 37 38 /** 39 * {@inheritdoc} 40 */ 41 public function getContext() { 42 return $this->displayedShortcutSet()->id(); 43 } 44 45 /** 46 * {@inheritdoc} 47 */ 48 public function getCacheableMetadata() { changed this line in version 6 of the diff
26 */ 27 public function __construct(AccountInterface $user, protected EntityTypeManagerInterface $entityTypeManager) { 28 parent::__construct($user); 29 } 30 31 /** 32 * {@inheritdoc} 33 */ 34 public static function getLabel() { 35 return t("User's shortcut set"); 36 } 37 38 /** 39 * {@inheritdoc} 40 */ 41 public function getContext() { changed this line in version 6 of the diff
19 /** 20 * Constructs a new UserShortcutSetCacheContext service. 21 * 22 * @param \Drupal\Core\Session\AccountInterface $user 23 * The current user. 24 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager 25 * The entity type manager. 26 */ 27 public function __construct(AccountInterface $user, protected EntityTypeManagerInterface $entityTypeManager) { 28 parent::__construct($user); 29 } 30 31 /** 32 * {@inheritdoc} 33 */ 34 public static function getLabel() { 44 45 /** 46 * {@inheritdoc} 47 */ 48 public function getCacheableMetadata(): CacheableMetadata { 49 return (new CacheableMetadata())->addCacheableDependency($this->displayedShortcutSet()); 50 } 51 52 /** 53 * Gets the user's shortcut set displayed. 54 * 55 * @return \Drupal\shortcut\ShortcutSetInterface 56 * The user's shortcut set displayed. 57 */ 58 protected function displayedShortcutSet(): ShortcutSetInterface { 59 return $this->entityTypeManager At the moment, the per-user render cache means that the database query and/or hook implementations in ShortcutSetStorage::getDisplayedToUser() are cached behind the render cache.
This will result in more efficient caching, but it will also mean that logic running on every page. Would it be worth adding caching to ::getDisplayedToUser() too? It would be a much smaller cache item than the render array, so still less to store overall, but would mean no extra database query.
89 88 * {@inheritdoc} 90 89 */ 91 90 public function assignUser(ShortcutSetInterface $shortcut_set, $account) { 92 $current_shortcut_set = $this->getDisplayedToUser($account);
Please register or sign in to reply