有一个名为"Model"的属性,其类型为下拉。属性码为wheel_model
。这个下拉菜单有2585个选项。
我已经在很多页面上使用了这个下拉菜单,允许用户选择车轮型号和过滤器产品列表。
下面是从数据库中获取下拉值的代码:$arrval = array();
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'wheel_model');
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
$arrval[$option['value']] = $option['label'];
}
我在数组中得到所有下拉值,我在php中使用它如下:
<select name='wheel_model'>
<option>Select Model</option>
<?php
foreach ($arrval as $value => $label) {
echo "<option value='" . $value . "'>" . $label . "<option>";
}
?>
</select>
加载下拉值花费太多时间。是否有任何方法可以将此下拉存储在缓存中,并在需要时从缓存中检索?
使用下面的代码在缓存中存储数据,仅此而已。
$cacheId = 'my_cache_id';
if (false !== ($data = Mage::app()->getCache()->load($cacheId))) {
$data = unserialize($data);
} else {
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'wheel_model');
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
$data[$option['value']] = $option['label'];
}
Mage::app()->getCache()->save(serialize($data), $cacheId);
}