If you want to run code before the Drupal kernel is started use a middleware.
Use the drush code generator drush gen middleware and put your code in handle() of the generated middleware class:
In general, never send responses from custom code. See How do I set a cookie using $response->headers->setCookie() in a form submission handler?
In this case you are sending a redirect response to the client while Drupal builds the real response containing the cookie to the session where the message gets stored. The real response never reaches the ...
The code depends on host name and IP address. So you could add the cache contexts url.site and ip:
$response = new TrustedRedirectResponse('https://site.ch/');
In this case, though, caching doesn't make much sense because it doesn't save many ...