How to store temporary metadata for the remainder of a request? I am looking for a way to store some additional metadata for the remainder of a request, that should be removed after the request has finished. Sessions or SharedTempStore store persistent data across requests, which is not desirable. 

The main problem is that I am working on an inbound PathProcessor, which after some path modifications needs to check if a valid path remains by using  \Drupal::service('path.validator')-&gt;isValid($path). This in turn calls all PathProcessors again, leading to infinite processing. I wish to store some sort of metadata in the request, so that the PathProcessor is aware that it is ran for a second time and does nothing. 

Obviously, for subsequent requests the metadata should initially be empty again so that the processing is done for each 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.