What cache service should I use?

iq flag

I just used a cache service explicitely for the first time, in a function that calls a remote service. The result is a simple string that may change day by day, but it's very rare that it changes for the past – so I expire it at midnight.

I injected cache.default in my controller – then I saw that in core services there is also a service called What's the difference? Is there any reason to prefer one to the other?

cn flag

Cache bins

Cache storage is separated into "bins", each containing various cache items. Each bin can be configured separately; see Configuration.

When you request a cache object, you can specify the bin name in your call to \Drupal::cache(). Alternatively, you can request a bin by getting service "cache.nameofbin" from the container. The default bin is called default, with service name cache.default, it is used to store common and frequently used caches.

Other common cache bins are the following:

  • bootstrap: Data needed from the beginning to the end of most requests, that has a very strict limit on variations and is invalidated rarely.
  • render: Contains cached HTML strings like cached pages and blocks, can grow to large size.
  • data: Contains data that can vary by path or similar context.
  • discovery: Contains cached discovery data for things such as plugins, views_data, or YAML discovered data such as library info.!core.api.php/group/cache

Francesco Marchetti-Stasi avatar
iq flag
Yeah, sorry, I had missed that – Drupal documentation can be overwhelming, sometimes :/
Francesco Marchetti-Stasi avatar
iq flag
And it seems that `cache.default` is the right choice, after all :)
I sit in a Tesla and translated this thread with Ai:


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.