[development] Block caching in D7

Randy Fay randy at randyfay.com
Sat Dec 11 05:37:59 UTC 2010

Hi Fredrik -

I think you may have been testing as user 1.  I started testing your problem
and noticed the same thing and started debugging and then noticed this code:

function _block_get_cache_id($block) {
  global $user;

  // User 1 being out of the regular 'roles define permissions' schema,
  // it brings too many chances of having unwanted output get in the cache
  // and later be served to other users. We therefore exclude user 1 from
  // block caching.

My testing (after I logged in as a different user) did show caching work
correctly, at least in casual testing.

As a result of your note I've added a timestamp into one of the blocks
provided by the Block Example module, part of the Examples project (
http://drupal.org/project/examples) so that one can easily explore whether
caching is happening or not. When http://drupal.org/node/995316 goes in
(maybe tonight) it will make an easier way to explore block caching.


On Tue, Dec 7, 2010 at 4:25 PM, Fredrik Sandve Kilander <
fredrik.kilander at gmail.com> wrote:

> Hello. I am the maintainer of the yr_verdata module, and as I am trying to
> finish up updating my module for D7, I am having trouble understanding how
> block caching works. On my development site, I have enabled caching, and in
> hook_block_info() i have specified caching as per the example at
> api.drupal.org:
>   $blocks['yr_verdata_block'] = array(
>     'info' => t('Yr weather forecast for all locations'),
>     'cache' => DRUPAL_CACHE_PER_ROLE, // I have tried with per page and
> global as well.
>   );
> This does not cache the block. If I set caching in the render array that's
> passed to $block['content'] in hook_block_view(), the caching works, but
> there are two things i wonder:
> 1. Is that the correct way to cache blocks?
> 2. Using render arrays with the '#cache' element in hook_block_view() works
> independently of whether or not I 'enable' caching in hook_block_view(). Why
> is this?
> What's the best practice for going about this?
> Source code can be seen at drupalcode.org: http://bit.ly/g586j6
> Lines: 185 (hook_block_info), 209 (hook_block_view). Theme function for the
> block is at 477, which gets its array of stuff from line 327 (the function
> called there, yr_verdata_generate_forecastbox(), starts at 349).
>  (if you find any other stuff in my code to bash at, feel free :) )
> Sincerely,
> Fredrik Kilander
> Maintainer of yr_verdata.module

Randy Fay
Drupal Module and Site Development
randy at randyfay.com
+1  970.462.7450
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20101210/d78d5540/attachment-0001.html 

More information about the development mailing list