Magento,如何在点击数据库之前从 redis 查询集合



我已经为 magento 1.9 设置了 redis。但是,我观察到当我查询集合时,它会非常严重地打击我的数据库。似乎 Redis 没有缓存我的数据库集合,所以我想知道我在查询集合时是否必须做点什么?比如,首先检查 redis 中是否存在收集密钥,如果没有 db 来获取集合,然后放入 redis 中。我获得收藏的方式是这样的:

$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToFilter($attribute_code, array('eq' => $attribute_value))
            ->addAttributeToSelect('*')
            ->joinTable('review/review_aggregate', 'entity_pk_value = entity_id', array('fhs_reviews_count' => 'reviews_count', 'fhs_rating_summary' => 'rating_summary'), '{{table}}.store_id=1', 'left')
            ->joinTable('multistoreviewpricingpriceindexer/product_index_price', 'entity_id = entity_id', array('final_price' => 'final_price', 'min_price' => 'min_price'), '{{table}}.store_id='.Mage::app()->getStore()->getId(), 'left')
            ->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left')
            ->addStoreFilter()
            ->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
            ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
            ->addAttributeToFilter('min_price', array('gt' => 0))                
            ->setPageSize($this->get_prod_count($isMobile))
            ->setCurPage($this->get_cur_page());

Magento的Redis模块仅适用于会话和缓存数据,而不是作为主要存储源。如果您希望能够从 Redis 查询集合,则必须将此集合存储在缓存中,并在之后进行查询,例如:

$storeId = Mage::app()->getStore()->getId();
$cache = Mage::getSingleton('core/cache');
$key = 'your-custom-key' . $storeId; // feel free to add specific variables to the key
if(! $data = $cache->load($key)) {
    $collection = Mage::getResourceModel('catalog/product_collection');
    $collection->addAttributeToFilter($attribute_code, ['eq' => $attribute_value])
        ->addAttributeToSelect('*')
        ->joinTable('review/review_aggregate', 'entity_pk_value = entity_id', [
            'fhs_reviews_count' => 'reviews_count',
            'fhs_rating_summary' => 'rating_summary'
        ], '{{table}}.store_id=1', 'left')
        ->joinTable('multistoreviewpricingpriceindexer/product_index_price', 'entity_id = entity_id', [
            'final_price' => 'final_price',
            'min_price'   => 'min_price'
        ], '{{table}}.store_id=' . Mage::app()->getStore()->getId(), 'left')
        ->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left')
        ->addStoreFilter()
        ->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
        ->addAttributeToFilter('status', ['eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED])
        ->addAttributeToFilter('min_price', ['gt' => 0])
        ->setPageSize($this->get_prod_count($isMobile))
        ->setCurPage($this->get_cur_page());
    $data = serialize($collection->getItems()); //or something else
    $cache->save($data, $key, array("homepage_cache"), 60*60*24);
}
else{
    $data = unserialize($data);
}

最新更新