I'm using a Prestashop 1.7 multistore. One of the stores has VAT enabled, while the other does not. I've created a custom module that hooks into the updateproduct
hook. In this module, I update the price of a product, but only for the store without VAT. This ensures that both stores have the same final prices.
To enhance performance, I utilize Memcached. But I struggle to invalidate the cache correctly. Once the value for the non-VAT store gets cached (the one I alter in my module), it's never changed after attempting to set it again in the product admin. It changes properly in database, but Memcached retrieves the old value. How can I invalidate or update the stored value in Memcached? I have also no clue how to get the correct Memcached key as it's a hash like for example ps_288e7d9f7adabca7ca488166fced130a.
Currently, the only method I've found to refresh Memcached is to invalidate the entire cache, which seems to be a suboptimal solution in terms of performance.
Cache::getInstance()->delete('*');
This is my simplified hook function to update the price:
private function updateProductPrice($params)
{
$id_product = $params['id_product'];
$price = $params['product']->price;
$id_tax_rules_group = $params['product']->id_tax_rules_group;
$shop_ids = $this->getShopIds();
$tax_rate = $this->getTaxRate($id_tax_rules_group);
foreach ($shop_ids as $shop_id) {
$is_tax = Configuration::get('PS_TAX', null, null, $shop_id);
$sql = 'SELECT `price`
FROM `' . _DB_PREFIX_ . 'product`
WHERE `id_product` = ' . (int) $id_product;
$base_price = (float)Db::getInstance()->getValue($sql);
if (!$is_tax) {
$new_price = $base_price * (100 + $tax_rate) / 100;
Db::getInstance()->update('product_shop', ['price' => $new_price], 'id_product = '.(int)$id_product . ' AND id_shop = '.(int)$shop_id);
}
}
}
Without the Memcached enabled everything works just fine.
source https://stackoverflow.com/questions/77260888/how-to-invalidate-a-memcached-value-when-updating-a-products-price-in-my-custom
No comments:
Post a Comment