{"id":57915,"date":"2023-08-10T16:43:21","date_gmt":"2023-08-10T11:13:21","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=57915"},"modified":"2023-08-30T17:07:31","modified_gmt":"2023-08-30T11:37:31","slug":"supercharge-your-drupal-9-website-with-custom-cache-bins","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/supercharge-your-drupal-9-website-with-custom-cache-bins\/","title":{"rendered":"Supercharge Your Drupal 9 Website with Custom Cache Bins"},"content":{"rendered":"<p>In Drupal 9, &#8220;cache bins&#8221; refer to a mechanism used to store and manage cached data. Caching is a technique employed by content management systems like Drupal to improve the performance of websites by storing frequently used data in memory or storage so that it can be retrieved faster when needed.<\/p>\n<p>Drupal 9 utilizes a cache system that divides cached data into different &#8220;bins&#8221; based on their purpose or usage. Each cache bin represents a separate storage area for cached data. This separation allows Drupal to manage different types of cached data independently, improving efficiency and flexibility.<\/p>\n<p>The core cache bins in Drupal 9 include:<\/p>\n<ul>\n<li><strong><span data-preserver-spaces=\"true\">&#8216;cache&#8217; bin:<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0This general-purpose cache bin stores various types of cached data. It&#8217;s used for content and data that are frequently requested but not specific to a particular purpose.<\/span><\/li>\n<li><strong><span data-preserver-spaces=\"true\">&#8216;config&#8217; bin:<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0This bin stores configuration-related cache data. Drupal configuration is cached to optimize performance, and this bin plays a role in that process.<\/span><\/li>\n<li><strong><span data-preserver-spaces=\"true\">&#8216;render&#8217; bin:<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0The &#8216;render&#8217; cache bin stores pre-rendered content, such as rendered blocks and pages. This can significantly speed up the delivery of dynamic content.<\/span><\/li>\n<li><strong><span data-preserver-spaces=\"true\">&#8216;dynamic_page_cache&#8217; bin:<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0This bin is responsible for caching full HTML pages with dynamic parts excluded. It improves the response time for anonymous users.<\/span><\/li>\n<li><strong><span data-preserver-spaces=\"true\">Custom cache bins:<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0In addition to the core cache bins, developers can create their own cache bins for specific purposes. For example, a module might define a custom cache bin to store data that doesn&#8217;t fit well into the existing bins.<\/span><\/li>\n<\/ul>\n<p>Drupal provides an API that developers can use to interact with cache bins, including storing, retrieving, and clearing cached data. This allows them to control what data is cached, how it&#8217;s cached, and when it should be invalidated or cleared.<\/p>\n<p>Using cache bins effectively is important for optimizing the performance of a Drupal website. By carefully selecting the appropriate cache bins and managing cache data properly, developers can improve page load times and overall user experience.<\/p>\n<p>Certainly! Let&#8217;s dive deeper into the concept of custom cache bins in Drupal 9.<\/p>\n<p>Custom cache bins are additional cache storage areas that developers can create to cater to specific needs that might not be adequately addressed by the core cache bins. These custom bins allow developers to have fine-grained control over caching for their module&#8217;s data, ensuring optimal performance and minimizing cache conflicts.<\/p>\n<p>Here&#8217;s how you can create and use a custom cache bin in Drupal 9:<\/p>\n<h2><strong>Defining a Custom Cache Bin:<\/strong><\/h2>\n<p>In your module&#8217;s <code>.services.yml<\/code> file, you can define a custom cache bin like this:<\/p>\n<blockquote>\n<pre>services:\r\nmy_module.my_custom_cache:\r\nclass: Drupal\\Core\\Cache\\CacheBackendInterface\r\nfactory: ['@cache_factory', 'get']\r\narguments: ['my_custom_cache']\r\n\r\n<\/pre>\n<\/blockquote>\n<h2><strong>Using the Custom Cache Bin:<\/strong><\/h2>\n<p>To use the custom cache bin in Drupal 9, you can do the following:<\/p>\n<blockquote>\n<pre>use Drupal\\Core\\Cache\\CacheBackendInterface;\r\nuse Drupal\\Core\\Cache\\Cache;\r\nuse Symfony\\Component\\DependencyInjection\\ContainerInterface;\r\n\r\nclass MyModuleService {\r\n\r\nprotected $customCache;\r\n\r\npublic function __construct(CacheBackendInterface $cache_backend) {\r\n$this-&gt;customCache = $cache_backend-&gt;get('my_custom_cache');\r\n}\r\n\r\npublic function storeData($key, $data) {\r\n$this-&gt;customCache-&gt;set($key, $data, Cache::PERMANENT);\r\n}\r\n\r\npublic function retrieveData($key) {\r\nreturn $this-&gt;customCache-&gt;get($key)-&gt;data ?? NULL;\r\n}\r\n\r\npublic function deleteData($key) {\r\n$this-&gt;customCache-&gt;delete($key);\r\n}\r\n}\r\n\r\n\r\n<\/pre>\n<\/blockquote>\n<p><strong>Benefits of Custom Cache Bins:<\/strong> Creating custom cache bins offers several benefits:<\/p>\n<ul>\n<li><strong><span data-preserver-spaces=\"true\">Isolation:<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0You can separate cached data related to your module from other modules, minimizing cache conflicts.<\/span><\/li>\n<li><strong><span data-preserver-spaces=\"true\">Fine-grained Control:<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0You have full control over how data is cached, retrieved, and invalidated.<\/span><\/li>\n<li><strong><span data-preserver-spaces=\"true\">Optimization:<\/span><\/strong><span data-preserver-spaces=\"true\">\u00a0You can optimize caching for your module&#8217;s specific use cases, improving performance.<\/span><\/li>\n<\/ul>\n<p>When implementing a custom cache bin, it&#8217;s important to consider factors like cache granularity, data validity, and cache-clearing strategies. Custom cache bins are a powerful tool in a developer&#8217;s toolkit for fine-tuning caching mechanisms in Drupal 9 applications.<\/p>\n<p>Feed your curiosity \u2013<a href=\"https:\/\/www.tothenew.com\/blog\/\"> read more<\/a> of our insightful blogs.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In Drupal 9, &#8220;cache bins&#8221; refer to a mechanism used to store and manage cached data. Caching is a technique employed by content management systems like Drupal to improve the performance of websites by storing frequently used data in memory or storage so that it can be retrieved faster when needed. Drupal 9 utilizes a [&hellip;]<\/p>\n","protected":false},"author":1368,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":232},"categories":[3602],"tags":[4862],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57915"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1368"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=57915"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57915\/revisions"}],"predecessor-version":[{"id":58205,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57915\/revisions\/58205"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=57915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=57915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=57915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}