Edit in GitHubLog an issue

Create custom cache engines

This topic discusses how to use the database for caching. After you complete these tasks, cached objects are stored in the cache and cache_tag database tables. Nothing is stored var/cache or var/page_cache.

This topic discusses how to set up database caching and how to verify database caching is working. We discuss the following options:

  • Using the default cache frontend, in which case you modify di.xml only.
  • Using a custom cache frontend, in which case you modify env.php only.

Prerequisites#

Before you continue, if you're using your own frontend cache, make sure you associate cache frontends with cache types. If you're using the default frontend cache, you don't have to do that.

We provide sample configurations at the end of this topic.

Database caching using the default cache frontend#

To enable database caching using the default frontend, you must modify the <magento_root>/app/etc/di.xml file, which is the global deployment injection configuration for the application.

To modify di.xml:

  1. Log in to the application server as, or switch to, the file system owner.

  2. Enter the following commands to make a copy of di.xml:

    Copied to your clipboard
    cd <magento_root>/app/etc
    Copied to your clipboard
    cp di.xml di.xml.bak
  3. Open di.xml in a text editor and locate the following block:

    Copied to your clipboard
    1<type name="Magento\Framework\App\Cache\Frontend\Pool">
    2 <arguments>
    3 <argument name="frontendSettings" xsi:type="array">
    4 <item name="page_cache" xsi:type="array">
    5 <item name="backend_options" xsi:type="array">
    6 <item name="cache_dir" xsi:type="string">page_cache</item>
    7 </item>
    8 </item>
    9 </argument>
    10 </arguments>
    11</type>
    12<type name="Magento\Framework\App\Cache\Type\FrontendPool">
    13 <arguments>
    14 <argument name="typeFrontendMap" xsi:type="array">
    15 <item name="full_page" xsi:type="string">page_cache</item>
    16 </argument>
    17 </arguments>
    18</type>

    The <type name="Magento\Framework\App\Cache\Frontend\Pool"> node configures options for the in-memory pool of all frontend cache instances.

    The <type name="Magento\Framework\App\Cache\Type\FrontendPool"> node configures cache frontend options specific to each cache type.

  4. Replace the entire block with the following:

    Copied to your clipboard
    1<type name="Magento\Framework\App\Cache\Frontend\Pool">
    2 <arguments>
    3 <argument name="frontendSettings" xsi:type="array">
    4 <item name="page_cache" xsi:type="array">
    5 <item name="backend" xsi:type="string">database</item>
    6 </item>
    7 <item name="<your cache id>" xsi:type="array">
    8 <item name="backend" xsi:type="string">database</item>
    9 </item>
    10 </argument>
    11 </arguments>
    12</type>
    13<type name="Magento\Framework\App\Cache\Type\FrontendPool">
    14 <arguments>
    15 <argument name="typeFrontendMap" xsi:type="array">
    16 <item name="backend" xsi:type="string">database</item>
    17 </argument>
    18 </arguments>
    19</type>

    where <your cache id> is your unique cache identifier.

  5. Save your changes to di.xml and exit the text editor.

  6. Continue with Verify database caching is working.

Database caching using a custom cache frontend#

This section discusses how to set up database caching with a custom cache frontend.

To enable database caching using a custom cache frontend, you must modify <magento_root>/app/etc/env.php as follows:

  1. Log in to the application server as, or switch to, the file system owner.

  2. Enter the following commands to make a copy of env.php:

    Copied to your clipboard
    cd <magento_root>/app/etc
    Copied to your clipboard
    cp env.php env.php.bak
  3. Open env.php in a text editor and add the following anywhere, such as before 'cache_types' =>:

    Copied to your clipboard
    1'cache' => [
    2 'frontend' => [
    3 '<unique frontend id>' => [
    4 <cache options>
    5 ],
    6 ],
    7 'type' => [
    8 <cache type 1> => [
    9 'frontend' => '<unique frontend id>'
    10 ],
    11 ],
    12 'type' => [
    13 <cache type 2> => [
    14 'frontend' => '<unique frontend id>'
    15 ],
    16 ],
    17],

    An example is shown in Configuration examples.

  4. Save your changes to env.php and exit the text editor.

  5. Continue with the next section.

Verify database caching is working#

To verify database caching is working, clear the current cache directories, go to any cacheable page in a web browser, and verify that data is written to the database and not to the file system.

Use the following steps:

  1. If you haven't done so already, log in to the application server as, or switch to, the file system owner.

  2. Clear the current cache directories:

    Copied to your clipboard
    rm -rf <magento_root>/var/cache/* <magento_root>/var/page_cache/* <magento_root>/generated/metadata/* <magento_root>/generated/code/*
  3. In a web browser, go to any cacheable page (such as the storefront front door page).

    If exceptions display, verify di.xml syntax and try again. (To see exceptions in the browser, you must enable developer mode.)

  4. Enter the following commands:

    Copied to your clipboard
    ls <magento_root>/var/cache/*
    Copied to your clipboard
    ls <magento_root>/var/page_cache/*

    Due to a known issue, a custom cache frontend still results in some objects being cached to the file system; however, fewer assets are cached compared to file system caching. If you use the default cache frontend, you don't have this issue.

  5. Verify both directories are empty; if not, edit di.xml again and correct any issues.

  6. Use a database tool such as phpMyAdmin to verify there is data in the cache and cache_tag tables.

    The following figures show examples. The important thing is that there are rows in the tables. The data in your tables will be different than the following.

    cache table example.

    Sample contents of the cache table with database caching enabled

    cache_tag table example.

    Sample contents of the cache tag table with database caching enabled

Configuration examples#

This section contains code sample snippets to refer to when configuring database caching.

Sample di.xml for the default cache frontend#

di.xml snippet:

Copied to your clipboard
1<type name="Magento\Framework\App\Cache\Frontend\Pool">
2 <arguments>
3 <argument name="frontendSettings" xsi:type="array">
4 <item name="page_cache" xsi:type="array">
5 <item name="backend" xsi:type="string">database</item>
6 </item>
7 <item name="default" xsi:type="array">
8 <item name="backend" xsi:type="string">database</item>
9 </item>
10 </argument>
11 </arguments>
12</type>
13<type name="Magento\Framework\App\Cache\Type\FrontendPool">
14 <arguments>
15 <argument name="typeFrontendMap" xsi:type="array">
16 <item name="backend" xsi:type="string">database</item>
17 </argument>
18 </arguments>
19</type>

Sample env.php for a custom cache frontend#

env.php snippet that enables all cache types with a custom frontend named magento_cache:

Copied to your clipboard
1'cache' => [
2 'frontend' => [
3 'magento_cache' => [
4 'backend' => 'database'
5 ],
6 ],
7 'type' => [
8 'config' => [
9 'frontend' => 'magento_cache'
10 ],
11 'layout' => [
12 'frontend' => 'magento_cache'
13 ],
14 'block_html' => [
15 'frontend' => 'magento_cache'
16 ],
17 'view_files_fallback' => [
18 'frontend' => 'magento_cache'
19 ],
20 'view_files_preprocessing' => [
21 'frontend' => 'magento_cache'
22 ],
23 'collections' => [
24 'frontend' => 'magento_cache'
25 ],
26 'db_ddl' => [
27 'frontend' => 'magento_cache'
28 ],
29 'eav' => [
30 'frontend' => 'magento_cache'
31 ],
32 'full_page' => [
33 'frontend' => 'magento_cache'
34 ],
35 'translate' => [
36 'frontend' => 'magento_cache'
37 ],
38 'config_integration' => [
39 'frontend' => 'magento_cache'
40 ],
41 'config_integration_api' => [
42 'frontend' => 'magento_cache'
43 ],
44 'config_webservice' => [
45 'frontend' => 'magento_cache'
46 ],
47 ],
48],
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.