Caching strategies
Learn about the caching strategies available for Magento 2 in Front-Commerce, including specific implementations like PerMagentoCustomerGroup, PerMagentoCustomerTaxZone, and PerMagentoAdminRole, and how to configure them for optimal performance.
PerMagentoCustomerGroup
The PerMagentoCustomerGroup
implementation is a decorator that is specific to
Magento 2 modules. It will decorate the existing caching strategies so that
DataLoader keys are specific to the current customer group. We highly recommend
to use it on Magento stores that have price per group, so they can leverage
other caching mechanisms (such as Redis
).
It is possible to provide a default group to use as scope for guest users.
Here is a configuration example:
export default {
strategies: [
// The PerMagentoCustomerGroup strategy MUST be registered after a persistent cache implementation
// because it has no effect in the context of the default per-request in-memory caching.
{
implementation: "Redis",
supports: "*",
config: {
host: "127.0.0.1",
},
},
{
implementation: "PerMagentoCustomerGroup",
// Will scope all data from the CatalogPrice DataLoader with the customer group
// (and other relevant price data loaders)
// before they are transmitted to the previous strategy (Redis).
// Other dataLoaders will use Redis storage in a standard fashion.
supports: [
"CatalogPrice",
"CatalogProductChildrenPrice",
"CatalogProductBundlePrice",
],
config: {
defaultGroupId: 0,
},
},
],
};
PerMagentoCustomerTaxZone
The PerMagentoCustomerTaxZone
implementation is a decorator that is specific
to the Magento platform. It will decorate the existing caching strategies so
that DataLoader keys are specific to the current customer's tax zone. We highly
recommend to use it on Magento stores that have price with taxes depending on
several tax zones, so they can leverage other caching mechanisms (such as
Redis
).
As this strategy can be complex, the tax zone definition is left to the
integrator through the taxZoneKeyFromAddress
function
Here is a configuration example:
export default {
strategies: [
// The PerMagentoCustomerTaxZone strategy MUST be registered after a persistent cache implementation
// because it has no effect in the context of the default per-request in-memory caching.
{
implementation: "Redis",
supports: "*",
config: {
host: "127.0.0.1",
},
},
{
implementation: "PerMagentoCustomerTaxZone",
supports: [
"CatalogPrice",
"CatalogProductChildrenPrice",
"CatalogProductBundlePrice",
],
config: {
addressType: "shipping", // or "billing" depending on which address is used to define the taxes showed to the user
defaultTaxZoneKey: "FR",
// Uncomment this line to override the default tax zone partitioning
// taxZoneKeyFromAddress: (address) => {
// // see https://docs.magento.com/user-guide/tax/tax-zones-rates.html
// return address.country_id;
// },
},
},
],
};
PerMagentoAdminRole
The PerMagentoAdminRole
implementation is a decorator that is specific to
Magento 2 integrations. It will decorate the existing caching strategies so that
DataLoader keys are different for admin users and customers. This is useful if a
custom Magento API returns a different result for admins and normal customers.
Here is a configuration example:
export default {
strategies: [
// The PerMagentoAdminRole strategy MUST be registered after a persistent cache implementation
// because it has no effect in the context of the default per-request in-memory caching.
{
implementation: "Redis",
supports: "*",
config: {
host: "127.0.0.1",
},
},
{
implementation: "PerMagentoAdminRole",
supports: ["CatalogProduct"],
},
],
};