Run code once on each page load

In the past there were some hooks, such as described here:
How to run check on each page load?

to execute arbitrary code on each page load.

Now this has been replaced with an event based approach. I wrote this to check it out:

class MyModuleEventSubscriber implements EventSubscriberInterface {
    public function onLoad(GetResponseEvent $event) {
        Drupal::logger('my_module')->info('test');
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        $events[KernelEvents::REQUEST][] = ['onLoad'];

        return $events;
    }
}

However, it seems like there is any number of requests on each page load—it actually captures each request, and in my case there are five (5). My intended code, however, has to call an outside API, and I’d like to avoid unnecessary calls.

Is there an event that is guaranteed to run just once on each page load, without saving a state on my side (that seems quite complicated for this problem)?


Go to Source of this post
Author Of this post: Ynhockey
Title Of post: Run code once on each page load
Author Link: {authorlink}