public static function Cache::keyFromQuery

public static Cache::keyFromQuery(SelectInterface $query)

Generates a hash from a query object, to be used as part of the cache key.

This smart caching strategy saves Drupal from querying and rendering to HTML when the underlying query is unchanged.

Expensive queries should use the query builder to create the query and then call this function. Executing the query and formatting results should happen in a #pre_render callback.

Parameters

\Drupal\Core\Database\Query\SelectInterface $query: A select query object.

Return value

string A hash of the query arguments.

File

core/lib/Drupal/Core/Cache/Cache.php, line 179

Class

Cache
Helper methods for cache.

Namespace

Drupal\Core\Cache

Code

public static function keyFromQuery(SelectInterface $query) {
  $query->preExecute();
  $keys = array((string) $query, $query->getArguments());
  return hash('sha256', serialize($keys));
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Cache!Cache.php/function/Cache::keyFromQuery/8.1.x