Programmatically add cache max-age I tried setting the cache max-age to 0 in hook_views_pre_view, in hook_views_pre_build and in hook_views_query_alter but it just doesn't work.

Eg: 

function my_module_views_pre_build(ViewExecutable $view) {
  $view-&gt;element['#cache']['max-age'] = 0;
}


The views caches the response until the caches are cleared. 

I need to do this programatically because I need to force the views to not cache the results for more than 10 minutes based on some conditions.

Later Edit: The issue discussed in "How to use max-age" does not apply in my case. I know how to use cache tags, contexts and max-age for pages/rendered arrays and they work just fine. But for views I can't make them work.
Views has a cache plugin for no caching which you can configure in UI. Better use this method, setting a cache max-age in a hook might not be enough to disable all caching in a view. Also you can't control traffic from anonymous users by this. If you have dynamic views you want to deliver to anonymous users uninstall the Internal Page Cache and set the Cache-Control headers in /admin/config/development/performance accordingly.