Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iqual-ch/authenticated_cache_warmer
https://github.com/iqual-ch/authenticated_cache_warmer
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/iqual-ch/authenticated_cache_warmer
- Owner: iqual-ch
- Created: 2024-04-22T08:53:18.000Z (9 months ago)
- Default Branch: 1.x
- Last Pushed: 2024-11-14T11:07:55.000Z (about 2 months ago)
- Last Synced: 2024-11-14T12:19:07.998Z (about 2 months ago)
- Language: PHP
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# Authenticated cache warmer
This module allows to warm cache on routes that need authentication. It achieves this by impersonating a user in an EventSubscriber using the `account_switcher` service and a state key to authenticate the warming requests (see Security).## Usage
The module only provides the API to create your own cache warmer service. You need to implement your own drush command or cron job to actually run the cache warming.To use the API, you need the following data:
- The route you want to call (e.g. `entity.node.canonical`)
- The entity ids to request
- The user ids to impersonate
- (Optional) Http options for the GuzzleHttpClientYou then create one or more CacheWarmUrl and provide them to the `authenticated_cache_warmer.cache_warmer` service. The service will call all the provided urls with the corresponding user account, limited to 10 parallel calls.
### Example
The following snippet will warm both nodes 1 and 2 simulating users 2 and 3.
```php
use Drupal\authenticated_cache_warmer\CacheWarmerUrl;// Set up data.
$route = 'entity.node.canonical';
$entity_ids = [1, 2];
$user_ids = [2, 3];// Set up urls.
$urls = [];
foreach($user_ids as $user_id) {
foreach($entity_ids as $entity_id) {
$urls[] = CacheWarmerUrl::create($route, ['node' => $entity_id], $user_id);
}
}// Run cache warming (use proper DI for service).
$cacheWarmer = \Drupal::service('authenticated_cache_warmer.cache_warmer');
$cacheWarmer->seturls($urls);
$cacheWarmer->warm();
```## Tips on CacheWarmerUrl
- CacheWarmerUrl extends Drupal/Core/Url, so you can use all the `from*` methods. You then need to set the user account using `setAccountId()`.
- You can pass options to the GuzzleHttpClient used to do the call using `setHttpOptions()` or via the fourth argument on the create method. See `GuzzleHttp\ClientInterface::requestAsync` for details.## Security
The requests are secured by generating and storing a random key on starting the warming, which is then passed along as a cookie value with the request. The EventSubscriber checks if the key is in Drupal's state and is active.This means the calling client needs to run with the same Drupal environment/db.