Score:0

Sharing Heavy Operations between multiple Page Elements

cn flag

I have a page with two separate page elements. One is a table-select form that shows an extensive list of content data. The other page element is a block that shows a summary of that same content.

Both elements really use the exact same data, the summary block just needs to compile the table data into an overview. Reusing the data is vital because the database queries are rather expensive.

Is there a way to build the page so that the blocks are able to share information? Right now both page elements are running the same database calls causing duplicate queries and increasing the page load time?

cn flag
A common approach is to define a service which queries the data, and statically caches the results. Then both blocks use the relevant method from that service, but only the first use incurs the DB hit(s). [`drupal_static`](https://api.drupal.org/api/drupal/core%21includes%21bootstrap.inc/function/drupal_static/9.2.x) is very useful for this
4uk4 avatar
cn flag
In addition, normally things you query in the database can be cached between requests. You can put the query result in a cache bin, for example `cache_default`, and tag it so that it gets invalidated when the data changes in the database.
Rubix05 avatar
cn flag
Oh wow, I didn't even know that drupal_static was a thing. I already had the functions built out with a service so I was able to put drupal_static in real quick. I still need to do some more testing, but so far, that appears to be exactly what I needed. Thanks!
4uk4 avatar
cn flag
If you have already a service you don't need drupal_static because class properties of a service persist through the entire request.
Rainer Feike avatar
in flag
Besides from solving that performance issue with drupal or php (say on the client side) - you should be able to solve it in your DB Backend. For modern DBs running the same query multiple times should be expansive only the first time. Rather more, you should focus for client-side-cache like stated in the Drupal Cache API https://www.drupal.org/docs/8/api/cache-api/cache-api. And - by default - Drupal's cache is also stored in DB - so you may only save query calculation time but not bandwidth.... Caching is never easy.
Rainer Feike avatar
in flag
But I agree with 4k4 et.all. If it's all in the same request, just use a request-scoped variable (but it's not if i.e. your block has other contexts then your page).
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.