Run code when a session ends Is there a hook or something similar that triggers when a PHP session ends?

When a user signs in, I'm writing a few things to the cache, and I need to delete them from the cache when the user exists the site. If the user manually logs out, that's no problem, but I still need to clear that data when users close the browser.
Drupal doesn't have hooks that are invoked when a session ends, probably also because the code would not be able to understand if the session cookie is not present because it expired (which means the session ended) or because the browser has not been used to log in on a Drupal site.

Drupal invokes a hook when the user is logged out, either because the user clicked on the Log out link, or because the user was forced to log out, which is what happens when the site is in maintenance mode and the account isn't exempted from being automatically logged-out.
Drupal 7 and Drupal 8 invoke hook_user_logout() when the user is logged-out, and the code invoking the hook also destroys the user session.

Since you are talking of cache to clear, if you are using Drupal 8, you can also investigate if there are cache tags or contexts that tell Drupal to invalidate the data when the session is destroyed.
Cache contexts lists session.exists as context that can be associated to cache data. I didn't test it, but from the context name, I take it invalidates the cache data when the session doesn't exist, which is what you are trying to achieve. (See also Cache API for more information on cache tags and cache contexts in Drupal 8.)

As side note, a Drupal session doesn't normally end when users close their browser. It would happen when using an incognito window with Google Chrome, or the equivalent of other browsers, but when you are not using that, the session doesn't end when the browser is closed, or when the browser tab is closed. (If that were to happen, it would mean logging off the users.)