Skip to content

Commit

Permalink
Merge branch '3045485-better-cache-metadata' into 8.x-2.2-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jesconstantine committed Apr 4, 2019
2 parents 8c39aae + 7011d2b commit 2949ce3
Showing 1 changed file with 3 additions and 24 deletions.
27 changes: 3 additions & 24 deletions src/Controller/SolrProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Drupal\search_api_federated_solr\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\search_api\Entity\Server;
use Drupal\search_api\SearchApiException;
use Drupal\search_api_federated_solr\Utility\Helpers;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class SolrProxyController extends ControllerBase {
Expand Down Expand Up @@ -156,28 +155,8 @@ public function getResultsJson(Request $request) {
watchdog_exception('search_api_federated_solr', $e, '%type while executed query on @server: @message in %function (line %line of %file).', array('@server' => $server->label()));
}

// Get Browser and proxy cache maximum age config setting
$performance_config = \Drupal::configFactory()->getEditable('system.performance');
$max_age_setting = $performance_config->get('cache.page.max_age');
// Honor the max age setting, if set, default to one hour.
// Setting to one hour to help decrease the load for autocomplete queries.
$max_age = $max_age_setting || 3600;

// Add Cache settings for Max-age and URL context.
$data['#cache'] = [
'contexts' => [
'url', // vary by the entire url, including path + query args
],
];

// Create cacheable json response with 200 response code.
$response = new CacheableJsonResponse($data, 200);
// Add cache metadata set in $data['#cache'].
$response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
// Set max age.
// @see: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Cache%21CacheableJsonResponse.php/class/CacheableJsonResponse/8.2.x
$response->setMaxAge($max_age);
$response->setSharedMaxAge($max_age);
// Create json response with 200 response code.
$response = new JsonResponse($data, 200);
return $response;
}
}

0 comments on commit 2949ce3

Please sign in to comment.