1

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')->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.

  • 1
    It seems you are looking for the Drupal 8, class/service, equivalent of drupal_static(). – kiamlaluno Nov 10 at 12:06
  • @kiamlaluno would you believe that in the past week it never occurred to me to use a private variable within the class. Your comment pushed me in the right direction. Thanks :) – Neograph734 Nov 10 at 13:17
  • 2
    Yes, because a PathProcessor is a service class, which is in Drupal by default instantiated only once (as singleton) and so private properties persists during the remainder of a request. – 4k4 Nov 10 at 15:23
0

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.

  • I believe this to be a good answer to the initial question, but IMO this added complexity only makes sense when the caches need to be invalidated from outside the class. For now I think I'll stick with my private variable :) – Neograph734 Nov 10 at 14:31
  • Like with drupal_static(), it should be used only when another class/function needs to reset its value, or change its value. It's not that every class property should be replaced from an instance of MemoryCache. – kiamlaluno Nov 10 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.