8

I am facing a situation where I need to invalidate a specific View in order to initiate clearing the cache on that View.

views_invalidate_cache() clears ALL cached Views data, for every View on the site.

How can I trigger a cache invalidation for a specific View from inside my module code?

3

I believe the function you are looking for is cache_flush from the views cache plugin.

| improve this answer | |
  • Correct me if I'm wrong, but it looks like you have to have the entire View in scope to execute this. That's possible of course, but it seems like there should be a function I can call like cache_flush($view_id) without having to load the whole view. Just curious :-) – sheldonkreger Nov 2 '14 at 15:53
  • cache_flush is Drupal 6 and 7 only? I'm not seeing it in the Views module for Drupal 8. – Yzmir Ramirez Oct 24 '16 at 20:26
10

If you know the name of the view that you want to clear, then you can clear the caches specific to that view by calling a function like the following:

/**
 * Function to invalidate selected views caches to enable the action forms to work
 */
function mymodule_invalidate_views_cache($view_name) {
  cache_clear_all('ctools_export:views_view:' . $view_name, 'cache_views', TRUE);
  cache_clear_all($view_name, 'cache_views_data', TRUE);
}

I needed this to allow VBO actions with configuration forms to be able to work while views caching was enabled (I am using content caching). I called this twice, once in the function mymodule_myaction_form() and again in the function mymodule_myaction_submit().

| improve this answer | |
1

You might consider using views content cache.

It's normally used to automatically clear a views cache when related content is added or updated, however it provides a framework to monitor a cache segment. I haven't done this, but developer docs are available.

| improve this answer | |
  • I'm specifically looking for a way to target a Views cache with PHP from within my own module code. Thanks for sharing this, though, it might help others. My cache problem isn't suited for this module. – sheldonkreger Oct 31 '14 at 20:48
1

Take a look at the project Cache Actions. It does exactly what you want. If you are able to use rules in your situation, then you don't even need to write custom code, you can just use the module as-is and set up rules to clear the caches of certain views. Otherwise, check out the internals of how it works as a guide to clearing view caches programmatically.

| improve this answer | |
  • Good info for others who may land here, but I specifically needed a way to flush a views cache in a specific place in my PHP code. – sheldonkreger Nov 13 '14 at 0:39
0

Not to second-guess you too much, but have you considered a reverse-proxy caching system like varnish? Those things give incredible performance and make db caching of views, nodes, etc somewhat moot (unless you have a lot of logged-in users and need this caching for them).

| improve this answer | |
  • This is great general performance advice but doesn't answer the specific question I asked. Good info though. – sheldonkreger Nov 13 '14 at 0:38

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.