Magento,如何从redis中查询集合,之前点击db
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Magento,如何从redis中查询集合,之前点击db相关的知识,希望对你有一定的参考价值。
我为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());
答案
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);
}
以上是关于Magento,如何从redis中查询集合,之前点击db的主要内容,如果未能解决你的问题,请参考以下文章
在 Magento 中加载日志访问者集合时收到“查询为空”错误