The first step is to start looking at how your code is running to ensure that caching is off. Otherwise, you may find yourself wondering why your errors appear only intermittently. You might want to start off by disabling both the dynamic page cache and render cache. If you have not done it already, you should enable the settings.local.php file by finding and uncommenting the line in the default /sites/default/settings.php file that has the following:
if (file_exists(__DIR__ . '/settings.local.php'))
{ include __DIR__ . '/settings.local.php'; }
From there, add the following lines to your /sites/default/settings.local.php file:
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null'; $settings['cache']['bins']['render'] = 'cache.backend.null';