Don't use procedural code. Session is now a Symfony object:
class MymoduleSessionCounter {
function increment(Request $request) {
$session = $request->getSession();
$value = $session->get('mymodule_count', 0);
$session->set('mymodule_count', $value + 1);
return $value;
}
}
Source: https://www.drupal.org/node/2380327
Edit:
What you need to do for debugging depends on your dev environment. If for example you have disabled caching you are not able to debug caching issues. In this case enable caching in dev and see what happens if you add cache metadata to the returned result:
return [
'#markup' => $value,
'#cache' => ['max-age' => 0],
];
Cache max-age = 0, because the result in this example changes on each request and is not worth caching.
When this is working the next step is to optimize production performance. Put the code in a #lazy_builder callback or in a block, which is built lazily by default. In both cases this is then auto-placeholdered for certain cache condition like cache max-age = 0, so that the rest of the page can still be cached.