chore: Cache available price rule attributes (#12144)
**What** We found out that the pricing context from the cart always contains the entire cart, even though it is kind of wrong. The issue is that even though we improve the performances of the query, it will cost a lot to have hundreds of constraint for nothing potentially. For that reason, we cache the attributes in memory with the best possible query we can do to gather them and we renew them when we perform a calculate prices if it has been reset. That way, we ensure we don't have unnecessary checks on attributes that does not have rules. Since we don't have the type table anymore which was doing that for us and until we have a proper caching layer it would do IMO. But the rules type table was very useful for these attributes findings
This commit is contained in:
@@ -19,12 +19,46 @@ export class PricingRepository
|
||||
extends MikroOrmBase
|
||||
implements PricingRepositoryService
|
||||
{
|
||||
#availableAttributes: Set<string> = new Set()
|
||||
|
||||
constructor() {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
}
|
||||
|
||||
clearAvailableAttributes() {
|
||||
this.#availableAttributes.clear()
|
||||
}
|
||||
|
||||
async #cacheAvailableAttributes() {
|
||||
const manager = this.getActiveManager<SqlEntityManager>()
|
||||
const knex = manager.getKnex()
|
||||
|
||||
const { rows } = await knex.raw(
|
||||
`
|
||||
SELECT DISTINCT attribute
|
||||
FROM (
|
||||
SELECT attribute
|
||||
FROM price_rule
|
||||
UNION ALL
|
||||
SELECT attribute
|
||||
FROM price_list_rule
|
||||
) as combined_rules_attributes
|
||||
`
|
||||
)
|
||||
this.#availableAttributes.clear()
|
||||
rows.forEach(({ attribute }) => {
|
||||
this.#availableAttributes.add(attribute)
|
||||
})
|
||||
}
|
||||
|
||||
async #cacheAvailableAttributesIfNecessary() {
|
||||
if (this.#availableAttributes.size === 0) {
|
||||
await this.#cacheAvailableAttributes()
|
||||
}
|
||||
}
|
||||
|
||||
async calculatePrices(
|
||||
pricingFilters: PricingFilters,
|
||||
pricingContext: PricingContext = { context: {} },
|
||||
@@ -53,7 +87,7 @@ export class PricingRepository
|
||||
const flattenedKeyValuePairs = flattenObjectToKeyValuePairs(context)
|
||||
|
||||
// First filter by value presence
|
||||
const flattenedContext = Object.entries(flattenedKeyValuePairs).filter(
|
||||
let flattenedContext = Object.entries(flattenedKeyValuePairs).filter(
|
||||
([, value]) => {
|
||||
const isValuePresent = !Array.isArray(value) && isPresent(value)
|
||||
const isArrayPresent = Array.isArray(value) && value.flat(1).length
|
||||
@@ -62,6 +96,13 @@ export class PricingRepository
|
||||
}
|
||||
)
|
||||
|
||||
if (flattenedContext.length > 10) {
|
||||
await this.#cacheAvailableAttributesIfNecessary()
|
||||
flattenedContext = flattenedContext.filter(([key]) =>
|
||||
this.#availableAttributes.has(key)
|
||||
)
|
||||
}
|
||||
|
||||
const hasComplexContext = flattenedContext.length > 0
|
||||
|
||||
const query = knex
|
||||
@@ -227,7 +268,6 @@ export class PricingRepository
|
||||
.orderBy("pl.id", "asc")
|
||||
.orderBy("price.amount", "asc")
|
||||
|
||||
console.log(query.toString())
|
||||
return await query
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user