What is the best one to create a sidebar that query the database which will the same for many pages

I have an app with many pages and each page contains a sidebar that has a statistics data from the database model.

I will like to query this data once since it will the same for all pages rather doing it in every page request.

I have considered using Middleware but I want to know if there is a better way to achieve it.

Thanks in advance

Middleware would be a fine approach. The best implementation might depend on your environment and hardware. For example you might cache the result in Redis if you have that. you could also consider a tool like PHP-Cache.

@tflight, thanks for the options. I am not using Redis, I will check the PHP-Cache before I decide. Thanks once again.

I implemented something like this:
which is superfast because of the opcode caching