![]() ![]() As its name suggests, it's the part of the code that builds the HeyTaco! leaderboard block. ![]() I created a module called heytaco and below is the build() function from my HeyTacoBlock class. The above quote is what I'll try to explain, showing some of the nitty gritty with the help of a custom module and the HeyTaco! block it builds. "It is of the utmost importance that you inform the Render API of the cacheability of a render array." - From D.O.'s page about the cacheability of render arrays We have to remember that it's important that our render array - the thing that renders the HTML - knows to cache itself. Multithreading provides better utilization of cache memory as threads share. ![]() ![]() 2.97s vs 281ms in my local environment.) Communicate with your Render Array Context switching is faster between the threads as compared to processes. Instead of taking seconds to generate the page holding the block, it takes milliseconds to display the cached version. So, instead of each page refresh requiring a call to the HeyTaco! API, we can just tell Drupal to cache the leaderboard block and display the cached results. It's the difference between grabbing a Big Mac from McDonald's vs buying the ingredients from the supermarket, going home and making a Big Mac in your kitchen. What do we save by caching? Grabbing something that has already been built is quicker than building it from scratch. It's not like the taco leaderboard needs minute by minute updates so it is a good candidate for caching. ( Remember, this is an, ummmm, hypothetical example.) Now, say we wanted to display the taco leaderboard as a block on the Chromatic HQ home page. Uses cache context services (services tagged with ntext, and whose service ID has the cachecontext. To have a complete Controller that could look like this example.Say our partners who form the Chromatic brain trust, (Chris, Dave and Mark), do something crazy like base our reward system on the number of HeyTaco! emojis given out amongst team members in Slack. $this->cacheBackend->set($cid, $data, CacheBackendInterface::CACHE_PERMANENT, $tags) $data_cached = $this->cacheBackend->get($cid) Which can be translated by these additional lines: Then within our Controller, we have to condition the computation intensive construction of our hierarchical tree to the existence or not of our cache. To declare our cache, let's create the my. file in the directory of our my_module module.Ĭlass: Drupal\Core\Cache\CacheBackendInterfaceĪnd we declare our cache whose identifier will be my_cache. We can also use the default cache provided by Drupal 8 (the fault service), or declare and use our own cache, which will then have a dedicated table but which can also be managed separately, especially if we want to put this cache on a third party service such as Redis or Memcache. In order to remedy this small inconvenience, we will set up a specific cache for our business needs and use the Cache API. Once the hierarchical tree built, with all the included business data, satisfied, you can finally consult the fruit of your work. Imagine the need to build a hierarchical tree for a user, a tree based on a very prolific taxonomy. Context caches allow you to specify the criteria by which the cache of a page can vary (by user, by path, by language, etc.) while the max-age property can be used to define a maximum duration of cache validity.īut the purpose of this post is not to go into the details of this cache system, but rather to illustrate the use of the Cache API to set up its own cache for a specific use case. Tag caches allow you to tag content, pages, page elements with very precise tags allowing to easily and accurately invalidate all pages or page elements that have these caches tags. This cache system is based on three basic concepts: Drupal 8 has an automatic caching system activated by default that is truly revolutionary, which makes it possible to offer a cache for anonymous visitors and especially for authenticated users without any configuration. As we say in terms of computer programming, only two things are extremely complex: naming variables and invalidating the cache. ![]()
0 Comments
Leave a Reply. |