3

I have a block with the cache setting DRUPAL_CACHE_PER_USER.

I want to clear the cache when a user performs a specific action. I want to clear the cached block only for the acting user, keeping the block cache for all other users.

How can I do that in Drupal 7?

4

This data is stored in the block_cache bin under the cid format:

<MODULE_MACHINE_NAME>:<BLOCK_MACHINE_NAME>:<THEME_MACHINE_NAME>:u.<UID>

Knowing that, you can build some wrapper function for cache_clear_all() that will delete any arbitrary UID's cached block:

function clear_user_block_cache($uid, $module = 'MYMODULE', $block = 'MYBLOCK', $theme = 'MYTHEME') {
  cache_clear_all("$module:$block:$theme:u.$uid");
}

You can invoke this function call whenever the user takes the relevant action.

0

If you get an exception on cache_clear_all(), make sure you add the table name as second parameter. E.g: cache_clear_all('yourcidstring', 'fromThisDbTable');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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