diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-01-14 06:20:15 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-01-14 06:20:15 +0000 |
commit | 8a70cd09ca92526fa78d75bce8a7843baa8f4a6b (patch) | |
tree | d021ed33a39b8dee89930ad5a1ea5be9a615ce2e /includes | |
parent | 52e5064d3241784ed110a9f268b9b39eb24dac1b (diff) | |
download | brdo-8a70cd09ca92526fa78d75bce8a7843baa8f4a6b.tar.gz brdo-8a70cd09ca92526fa78d75bce8a7843baa8f4a6b.tar.bz2 |
#652048 by chx: Factor out the caching pattern from forum.
Diffstat (limited to 'includes')
-rw-r--r-- | includes/common.inc | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/includes/common.inc b/includes/common.inc index 46bc96e01..75dbe5685 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -5251,6 +5251,49 @@ function drupal_render_cache_set(&$markup, $elements) { } /** + * Prepare an element for caching based on a query. 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. + * + * @param $query + * A select query object as returned by db_select(). + * @param $function + * The name of the function doing this caching. A _pre_render suffix will be + * added to this string and is also part of the cache key in + * drupal_render_cache_set() and drupal_render_cache_get(). + * @param $expire + * The cache expire time, passed eventually to cache_set(). + * @param $granularity + * One or more granularity constants passed to drupal_render_cid_parts(). + * + * @return + * A renderable array with the following keys and values: + * - #query: The passed in $query. + * - #pre_render: $function with a _pre_render suffix. + * - #cache: An associative array prepared for drupal_render_cache_set(). + */ +function drupal_render_cache_by_query($query, $function, $expire = CACHE_TEMPORARY, $granularity = NULL) { + $cache_keys = array_merge(array($function), drupal_render_cid_parts($granularity)); + $query->preExecute(); + $cache_keys[] = md5(serialize(array((string) $query, $query->getArguments()))); + return array( + '#query' => $query, + '#pre_render' => array($function . '_pre_render'), + '#cache' => array( + 'keys' => $cache_keys, + 'expire' => $expire, + ), + ); +} + +/** + + +/** * Helper function for building cache ids. * * @param $granularity |