Magento,如何从redis中查询集合,之前点击db

时间:2018-03-02 07:37:41

标签: magento redis magento-1.9

我为magento 1.9设置了redis。但是,我观察到当我查询集合时,它非常重视我的数据库。好像Redis没有缓存我的db集合,所以我不知道在查询集合时我必须做些什么吗?比如,首先检查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());

1 个答案:

答案 0 :(得分:1)

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);
}