4

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?

2 Answers 2

5

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.

1
  • You can find the module and block machine names by going to the edit page of the block and checking out the URL. Should be admin/structure/block/manage/<MODULE>/<BLOCK>/configure.
    – donut
    Jul 28, 2021 at 17:55
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 and acknowledge you have read our privacy policy.

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