How to store temporary metadata for the remainder of a request?
Drupal introduced the Drupal\Core\Cache\MemoryCache\MemoryCache class to, as Added memory cache says, allow classes that would previously maintain state on a protected property (as a static cache to avoid persistent cache or database lookups) to inject a service to hold that state instead.

Drupal core implements two services that use that class, one of them is the entity.memory_cache service, which is defined from the following code.

entity.memory_cache:
  class: Drupal\Core\Cache\MemoryCache\MemoryCache


The other service using that class is the cache.jsonapi_memory service, which is defined from the following code.

cache.jsonapi_memory:
  class: Drupal\Core\Cache\MemoryCache\MemoryCache
  public: false


A module that needs to store data that is valid only for the current request can define a similar service. Setting the service as private is optional, but it avoids other modules uses it, making the stored data private for the module.