Score:1

Service continues to act as shared when shared is set to false

in flag

According to Symfony docs, doing shared: false on a service should mean that each time you call a service, you get a new instance. However, this doesn't seem to work for me. I am using Drupal 9 which runs on Symfony 4.4.

My services.yml :

services:
  my_module.arbitrary_data:
    class: Drupal\my_module\Services\ArbitraryData
    arguments: ['@another_module.contextual_uid']
    shared: false

https://symfony.com/doc/4.4/service_container/shared.html

I've also tried the deprecated scope: prototype and scope: request methods to no avail. If I load a page, my services outputs unchanged data from the last time the page was loaded until I clear cache.

Regardless of doing this, the services fail to run after the first page load after clearing cache and just proceed to output unchanged data every time. This feels like "unwanted caching".

Edit 11/15 3:47PM The render array from the plugin using the service, as requested:

$service = \Drupal::service('my_module.arbitrary_data');
$data = $service->get('acr');
$data = array_shift($data);

return [
  '#markup' => $data['value'],
];
id flag
I agree that according to https://www.drupal.org/docs/drupal-apis/services-and-dependency-injection/structure-of-a-service-file this is supposed to be supported. I don't, however, understand the symptoms you described in the way you described them.
cn flag
Where are you invoking the service? Inside a hook which is cached maybe? What does the cache look like on the render array which is outputting the data?
4uk4 avatar
cn flag
Setting a service to not shared only has an effect on the service properties in memory, not the Drupal cache data stored in the database.
pixel5 avatar
in flag
@Clive It's being used in a controller that is not being cached, in fact the route that calls the controller is set to no_cache.
pixel5 avatar
in flag
@cilefen Imagine you have a custom service which returns the current time. On the first request, the service returns the time. On the second request, the same time as the first request is returned. I have a feeling that there is a cache context that needs to be changed somewhere, I just don't know where or what.
cn flag
Could you post a copy of the render array being returned from the controller in the question (or whatever other method you're using to return the content). Your problem isn't related to the `shared` parameter by the way, as 4uk4 mentioned
pixel5 avatar
in flag
@Clive I have added it to the original post for readability. The controller is part of a plugin, if that matters.
Score:2
in flag

Thanks to user @Clive 's questioning, it led me to assess this issue as a Plugin problem rather than a Service problem. Since the service is being used in a plugin, and the plugin was being cached (by default), my output was not changing.

Implementing cache max-age into the plugin's render array fixed this issue.

return [
  '#markup' => $data['value'],
  '#cache' => [
    'max-age' => 0,
  ],
];
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.