Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
drupal
Commits
8a70cd09
Commit
8a70cd09
authored
Jan 14, 2010
by
webchick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#652048
by chx: Factor out the caching pattern from forum.
parent
52e5064d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
14 deletions
+46
-14
includes/common.inc
includes/common.inc
+43
-0
modules/forum/forum.module
modules/forum/forum.module
+3
-14
No files found.
includes/common.inc
View file @
8a70cd09
...
...
@@ -5250,6 +5250,49 @@ function drupal_render_cache_set(&$markup, $elements) {
cache_set
(
$cid
,
$data
,
$bin
,
$expire
);
}
/**
* 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.
*
...
...
modules/forum/forum.module
View file @
8a70cd09
...
...
@@ -637,21 +637,10 @@ function forum_block_view($delta = '') {
break
;
}
$cache_keys
=
array_merge
(
array
(
'forum'
,
$delta
),
drupal_render_cid_parts
());
// Cache based on the altered query. Enables us to cache with node access enabled.
$query
->
preExecute
();
$cache_keys
[]
=
md5
(
serialize
(
array
((
string
)
$query
,
$query
->
getArguments
())));
$block
[
'subject'
]
=
$title
;
$block
[
'content'
]
=
array
(
'#access'
=>
user_access
(
'access content'
),
'#pre_render'
=>
array
(
'forum_block_view_pre_render'
),
'#cache'
=>
array
(
'keys'
=>
$cache_keys
,
'expire'
=>
CACHE_TEMPORARY
,
),
'#query'
=>
$query
,
);
// Cache based on the altered query. Enables us to cache with node access enabled.
$block
[
'content'
]
=
drupal_render_cache_by_query
(
$query
,
'forum_block_view'
);
$block
[
'content'
][
'#access'
]
=
user_access
(
'access content'
);
return
$block
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment