function Cache::keyFromQuery

Same name in other branches
  1. 9 core/lib/Drupal/Core/Cache/Cache.php \Drupal\Core\Cache\Cache::keyFromQuery()
  2. 8.9.x core/lib/Drupal/Core/Cache/Cache.php \Drupal\Core\Cache\Cache::keyFromQuery()

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.

Deprecated

in drupal:10.1.0 and is removed from drupal:11.0.0. No replacement provided.

See also

https://www.drupal.org/node/3308507

1 call to Cache::keyFromQuery()
CacheTest::testKeyFromQuery in core/tests/Drupal/Tests/Core/Cache/CacheTest.php
@covers ::keyFromQuery @group legacy

File

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

Class

Cache
Helper methods for cache.

Namespace

Drupal\Core\Cache

Code

public static function keyFromQuery(SelectInterface $query) {
    @trigger_error(__METHOD__ . ' is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. No replacement provided. See https://www.drupal.org/node/3322044', E_USER_DEPRECATED);
    $query->preExecute();
    $keys = [
        (string) $query,
        $query->getArguments(),
    ];
    return hash('sha256', serialize($keys));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.