-
Notifications
You must be signed in to change notification settings - Fork 0
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 #43 from City-of-Helsinki/revert-42-UHF-8526
Revert "UHF-8526: Use PubSub to invalidate caches"
- Loading branch information
Showing
15 changed files
with
275 additions
and
114 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
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,90 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\helfi_navigation; | ||
|
||
use Drupal\Core\Cache\CacheTagsInvalidatorInterface; | ||
use Drupal\Core\Language\LanguageManagerInterface; | ||
use Drupal\Core\TempStore\SharedTempStoreFactory; | ||
use Drupal\helfi_navigation\Plugin\Derivative\ExternalMenuBlock; | ||
|
||
/** | ||
* A service to prefetch all menu links. | ||
*/ | ||
final class CacheWarmer { | ||
|
||
/** | ||
* The TempStore storage key. | ||
*/ | ||
public const STORAGE_KEY = 'external_menu_hashes'; | ||
|
||
/** | ||
* Constructs a new instance. | ||
* | ||
* @param \Drupal\Core\TempStore\SharedTempStoreFactory $tempStoreFactory | ||
* The temp store factory. | ||
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager | ||
* The language manager. | ||
* @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cacheTagsInvalidator | ||
* The cache tags invalidator service. | ||
* @param \Drupal\helfi_navigation\ApiManager $apiManager | ||
* The api manager. | ||
*/ | ||
public function __construct( | ||
private SharedTempStoreFactory $tempStoreFactory, | ||
private LanguageManagerInterface $languageManager, | ||
private CacheTagsInvalidatorInterface $cacheTagsInvalidator, | ||
private ApiManager $apiManager, | ||
) { | ||
} | ||
|
||
/** | ||
* Invalidate cache tags for given menu and language. | ||
* | ||
* @param mixed $data | ||
* The data. | ||
* @param string $language | ||
* The language. | ||
* @param string $menuName | ||
* The menu name. | ||
*/ | ||
private function invalidateTags(mixed $data, string $language, string $menuName) : void { | ||
$key = sprintf('%s:%s', $language, $menuName); | ||
$storage = $this->tempStoreFactory->get(self::STORAGE_KEY); | ||
|
||
$currentHash = $storage->get($key); | ||
$hash = hash('sha256', serialize($data)); | ||
|
||
// Only invalidate tags if content has actually changed. | ||
if ($currentHash === $hash) { | ||
return; | ||
} | ||
$storage->set($key, $hash); | ||
// Invalidate menu block instances. | ||
$this->cacheTagsInvalidator->invalidateTags(['config:system.menu.' . $menuName]); | ||
} | ||
|
||
/** | ||
* Warm caches for all available external menus. | ||
*/ | ||
public function warm() : void { | ||
$plugin = new ExternalMenuBlock(); | ||
$derives = array_keys($plugin->getDerivativeDefinitions([])); | ||
$derives[] = 'main'; | ||
|
||
foreach ($this->languageManager->getLanguages() as $language) { | ||
foreach ($derives as $name) { | ||
try { | ||
$response = $this->apiManager | ||
->withBypassCache() | ||
->get($language->getId(), $name); | ||
$this->invalidateTags($response, $language->getId(), $name); | ||
} | ||
catch (\Exception) { | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.