2

I want to execute code before cache is cleared, for example for the menu cache:

// Clear menu cache
$cache = \Drupal::cache('menu');
$cache->deleteAll();

I want to intervene when the cache clear is called, right before it is deleted. I've read the documentation on Drupal::cache and the Cache API but didn't find a solution. Do note that I want to do this using core code only, no contributed modules.

6
  • I think you'll need to provide your own cache classes to do that; none of the core classes invoke events or hooks in ::deleteAll or ::deleteMultiple which is where it would need to happen
    – Clive
    Jan 14, 2017 at 18:42
  • Exactly, thanks for your input. Could you provide a code sample to get me started? Jan 14, 2017 at 20:31
  • Why do you need this? It is a cache, caches are not persistent. Whatever you try to do there will not be reliable, especially with alternative cache backends like redis/memcache which will just drop your cache entries if the memory is used up
    – Berdir
    Jan 15, 2017 at 11:20
  • It's for the following core isse: Standard profile: make "Home" menu link to <front> editable/deletable. We're removing a link from standard.links.menu.yml and adding it with MenuLinkContent::create() instead. And now we need an upgrade path. Jan 15, 2017 at 11:22
  • Ah of course, just saw the update there. Yeah, forgot about the update path, alex pott saving us again :) I don't think that will work, adding a new hook won't exist yet in the cache either. So it won't be called..
    – Berdir
    Jan 15, 2017 at 11:27

1 Answer 1

1

It isn't clean but I'm able to do this with hook_cache_flush. It's called when a cache clear has been called, before the cache has been cleared.

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.