4

We have a module that creates a custom navigation block. It runs a metric ton of queries, and it's really affecting performance. (Note that I inherited this site; I did not write this code!)

We want to cache the block, but the built-in block caching (admin/settings/performance) is greyed-out, with a message below that says block caching is inactive when modules defining content access restrictions are enabled. We do have several modules that have access restrictions, and cannot disable those.

Is there any other way to cache the navigation block? I know about the BLOCK_CACHE_* constants, but am assuming that the built-in block caching needs to be turned on, and that those constants modify the default behavior. If we can't turn on the built-in caching, what (if anything) can we do?

1 Answer 1

5

Assuming that the module that creates the block and is this performance heavy is a custom module, a simple solution would be to cache the output of the block from within the block. Take a look at the cache_set and cache_get functions on http://api.drupal.org

5
  • Thanks. I'll take a look at those functions. Hopefully they will let us cache per role, since different roles need to see different versions of the block.
    – EmmyS
    Apr 25, 2011 at 21:34
  • 2
    If you do the caching yourself, you can cache based on whatever conditions you want to. The only thing you need to do is make sure that the cache $key, which identifies a single cache entry, contains all these conditions. To cache based on the set of roles a user has, you can use something like: $key = 'yourcachename_ ' . implode('_', array_keys($user->roles));
    – Berdir
    Apr 26, 2011 at 4:11
  • 2
    @Berdir is spot on. You might want to cache based on permissions instead if you have a lot of different role combinations. Roles is just a collection of permissions anyways.
    – googletorp
    Apr 26, 2011 at 8:04
  • 2
    Good point about the permissions. Roles can be more than a collection of permissions however, for example node access rules can be based directly on nodes.
    – Berdir
    Apr 26, 2011 at 8:25
  • @Berdir True, I should have added most often, as some modules uses roles and not just the permissions they grant. Most often, using user_access is the simplest solution.
    – googletorp
    Apr 26, 2011 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.