Use Drupal's page cache for toolbar/subtrees/*, even for authenticated users.
This gets invoked after full bootstrap, so must duplicate some of what's done by _drupal_bootstrap_page_cache().
@todo Replace this hack with something better integrated with DrupalKernel once Drupal's page caching itself is properly integrated.
function _toolbar_initialize_page_cache() {
$GLOBALS['conf']['system.performance']['cache']['page']['enabled'] = TRUE;
drupal_page_is_cacheable(TRUE);
// If we have a cache, serve it.
// @see _drupal_bootstrap_page_cache()
$cache = drupal_page_get_cache();
if (is_object($cache)) {
$response = new Response();
$request = \Drupal::request();
$response->headers
->set('X-Drupal-Cache', 'HIT');
// Restore the metadata cached with the page.
$_GET['q'] = $cache->data['path'];
date_default_timezone_set(drupal_get_user_timezone());
drupal_serve_page_from_cache($cache, $response, $request);
$response
->prepare($request);
$response
->send();
// We are done.
exit;
}
// Otherwise, create a new page response (that will be cached).
drupal_add_http_header('X-Drupal-Cache', 'MISS');
// The Expires HTTP header is the heart of the client-side HTTP caching. The
// additional server-side page cache only takes effect when the client
// accesses the callback URL again (e.g., after clearing the browser cache or
// when force-reloading a Drupal page).
$max_age = 3600 * 24 * 365;
drupal_add_http_header('Expires', gmdate(DATE_RFC1123, REQUEST_TIME + $max_age));
drupal_add_http_header('Cache-Control', 'private, max-age=' . $max_age);
}