假如ECshop的產品數到達幾萬,十幾萬的時分,假如主頁沒有緩存,初次拜訪的時分,你會發現其慢無比,緣由即是清空了Cache后或許沒有Cache的情況下,ECshop會Bulid一些Cache數據,致使拜訪很慢,但咱們有時分后臺修改類目或許別的的,經常會觸發清空Cache,所以主頁初次拜訪也成了疑問。
在大數據量的情況下,影響主頁速度最大的即是引薦的Best、Hot、New Item的數據Bulid,它會把一切的復合條件的產品都會讀一遍,然后存到/temp/static_caches/recommend_goods.php這個文件下,有時分會到達10M或許數十M,本來咱們并不需要一切的產品都Bulid進入,由于這個緩存只用在主頁和Category頁的調用,有點糟蹋。(P.S 由于Category拜訪本來就比較慢,所以我把暢銷產品在Category的展現屏蔽了,所以只剩主頁調用)
主頁展現的時分,三種類型Best、Hot、New只展現10個產品(我沒有挑選展現多個類目),所以這個上面有很大的優化空間。
翻開include目錄下的lib_goods.php文件,找到function get_recommend_goods() 函數,初始的大概是
[php]
function get_recommend_goods($type = '', $cats = '')
{
if (!in_array($type, array('best', 'new', 'hot')))
{
return array();
}
//取不同推薦對應的商品
static $type_goods = array();
if (empty($type_goods[$type]))
{
//初始化數據
$type_goods['best'] = array();
$type_goods['new'] = array();
$type_goods['hot'] = array();
$data = read_static_cache('recommend_goods');
if ($data === false)
{
$sql = 'SELECT g.goods_id, g.is_best, g.is_new, g.is_hot, g.is_promote, b.brand_name,g.sort_order ' .
' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
' LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND (g.is_best = 1 OR g.is_new =1 OR g.is_hot = 1)'.
' ORDER BY g.sort_order, g.last_update DESC';
$goods_res = $GLOBALS['db']->getAll($sql);
//定義推薦,最新,熱門,促銷商品
$goods_data['best'] = array();
$goods_data['new'] = array();
$goods_data['hot'] = array();
$goods_data['brand'] = array();
if (!empty($goods_res))
{
foreach($goods_res as $data)
{
if ($data['is_best'] == 1)
{
$goods_data['best'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
if ($data['is_new'] == 1)
{
$goods_data['new'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
if ($data['is_hot'] == 1)
{
$goods_data['hot'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
if ($data['brand_name'] != '')
{
$goods_data['brand'][$data['goods_id']] = $data['brand_name'];
}
}
}
write_static_cache('recommend_goods', $goods_data);
}
else
{
$goods_data = $data;
}
$time = gmtime();
$order_type = $GLOBALS['_CFG']['recommend_order'];
//按推薦數量及排序取每一項推薦顯示的商品 order_type可以根據后臺設定進行各種條件顯示
static $type_array = array();
$type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot');
if (empty($type_array))
{
foreach($type2lib as $key => $data)
{
if (!empty($goods_data[$key]))
{
$num = get_library_number($data);
$data_count = count($goods_data[$key]);
$num = $data_count > $num ? $num : $data_count;
if ($order_type == 0)
{
//usort($goods_data[$key], 'goods_sort');
$rand_key = array_slice($goods_data[$key], 0, $num);
foreach($rand_key as $key_data)
{
$type_array[$key][] = $key_data['goods_id'];
}
}
else
{
$rand_key = array_rand($goods_data[$key], $num);
if ($num == 1)
{
$type_array[$key][] = $goods_data[$key][$rand_key]['goods_id'];
}
else
{
foreach($rand_key as $key_data)
{
$type_array[$key][] = $goods_data[$key][$key_data]['goods_id'];
}
}
}
}
else
{
$type_array[$key] = array();
}
}
}
//取出所有符合條件的商品數據,并將結果存入對應的推薦類型數組中
$sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
"IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
"promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img, RAND() AS rnd " .
'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
"LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
"ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ";
$type_merge = array_merge($type_array['new'], $type_array['best'], $type_array['hot']);
$type_merge = array_unique($type_merge);
$sql .= ' WHERE g.goods_id ' . db_create_in($type_merge);
$sql .= ' ORDER BY g.sort_order, g.last_update DESC';
$result = $GLOBALS['db']->getAll($sql);
foreach ($result AS $idx => $row)
{
if ($row['promote_price'] > 0)
{
$promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
$goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
}
else
{
$goods[$idx]['promote_price'] = '';
}
$goods[$idx]['id'] = $row['goods_id'];
$goods[$idx]['name'] = $row['goods_name'];
$goods[$idx]['brief'] = $row['goods_brief'];
$goods[$idx]['brand_name'] = isset($goods_data['brand'][$row['goods_id']]) ? $goods_data['brand'][$row['goods_id']] : '';
$goods[$idx]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
$goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
$goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
$goods[$idx]['market_price'] = price_format($row['market_price']);
$goods[$idx]['shop_price'] = price_format($row['shop_price']);
$goods[$idx]['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$goods[$idx]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
$goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
if (in_array($row['goods_id'], $type_array['best']))
{
$type_goods['best'][] = $goods[$idx];
}
if (in_array($row['goods_id'], $type_array['new']))
{
$type_goods['new'][] = $goods[$idx];
}
if (in_array($row['goods_id'], $type_array['hot']))
{
$type_goods['hot'][] = $goods[$idx];
}
}
}
return $type_goods[$type];
}
[/php]
我們思路就是不讀取所有的商品信息,而是Best、Hot、New夠用展示就行了,所以把代碼調整了一下,限制了Bulid的商品數,去掉了一些我不需要的Join表,當然大家不一定要照著我做,我講的都是思路,看自己的實際情況,要有點程序基本功。
更改后的代碼如下:
[php]
function get_recommend_goods($type = '', $cats = '')
{
if (!in_array($type, array('best', 'new', 'hot')))
{
return array();
}
//取不同推薦對應的商品
static $type_goods = array();
if (empty($type_goods[$type]))
{
//初始化數據
$type_goods['best'] = array();
$type_goods['new'] = array();
$type_goods['hot'] = array();
$data = read_static_cache('recommend_goods');
if ($data === false)
{
$sql = 'SELECT g.goods_id, g.sort_order ' .
' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
' WHERE (g.is_best = 1)'.
' ORDER BY g.goods_number DESC limit 50';
$goods_res = $GLOBALS['db']->getAll($sql);
$goods_data['best'] = array();
if (!empty($goods_res))
{
foreach($goods_res as $data)
{
$goods_data['best'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
}
$sql = 'SELECT g.goods_id, g.sort_order ' .
' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
' WHERE (g.is_new = 1 and g.is_best = 0 and g.is_hot = 0)'.
' ORDER BY g.goods_id DESC limit 50';
$goods_res = $GLOBALS['db']->getAll($sql);
$goods_data['new'] = array();
if (!empty($goods_res))
{
foreach($goods_res as $data)
{
$goods_data['new'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
}
$sql = 'SELECT g.goods_id, g.sort_order ' .
' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
' WHERE (g.is_hot = 1 and g.is_best = 0)'.
' ORDER BY g.goods_number DESC limit 50';
$goods_res = $GLOBALS['db']->getAll($sql);
$goods_data['hot'] = array();
if (!empty($goods_res))
{
foreach($goods_res as $data)
{
$goods_data['hot'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
}
}
write_static_cache('recommend_goods', $goods_data);
}
else
{
$goods_data = $data;
}
$time = gmtime();
$order_type = $GLOBALS['_CFG']['recommend_order'];
//按推薦數量及排序取每一項推薦顯示的商品 order_type可以根據后臺設定進行各種條件顯示
static $type_array = array();
$type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot');
if (empty($type_array))
{
foreach($type2lib as $key => $data)
{
if (!empty($goods_data[$key]))
{
$num = get_library_number($data);
$data_count = count($goods_data[$key]);
$num = $data_count > $num ? $num : $data_count;
if ($order_type == 0)
{
//usort($goods_data[$key], 'goods_sort');
$rand_key = array_slice($goods_data[$key], 0, $num);
foreach($rand_key as $key_data)
{
$type_array[$key][] = $key_data['goods_id'];
}
}
else
{
$rand_key = array_rand($goods_data[$key], $num);
if ($num == 1)
{
$type_array[$key][] = $goods_data[$key][$rand_key]['goods_id'];
}
else
{
foreach($rand_key as $key_data)
{
$type_array[$key][] = $goods_data[$key][$key_data]['goods_id'];
}
}
}
}
else
{
$type_array[$key] = array();
}
}
}
//取出所有符合條件的商品數據,并將結果存入對應的推薦類型數組中
$sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, g.shop_price, ' .
"promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img, RAND() AS rnd " .
'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
" ";
$type_merge = array_merge($type_array['new'], $type_array['best'], $type_array['hot']);
$type_merge = array_unique($type_merge);
$sql .= ' WHERE g.goods_id ' . db_create_in($type_merge);
$sql .= ' ORDER BY g.goods_number DESC';
$result = $GLOBALS['db']->getAll($sql);
foreach ($result AS $idx => $row)
{
if ($row['promote_price'] > 0)
{
$promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
$goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
}
else
{
$goods[$idx]['promote_price'] = '';
}
$goods[$idx]['id'] = $row['goods_id'];
$goods[$idx]['name'] = $row['goods_name'];
$goods[$idx]['brief'] = $row['goods_brief'];
$goods[$idx]['brand_name'] = isset($goods_data['brand'][$row['goods_id']]) ? $goods_data['brand'][$row['goods_id']] : '';
$goods[$idx]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
$goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
$goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
$goods[$idx]['market_price'] = price_format($row['market_price']);
$goods[$idx]['shop_price'] = price_format($row['shop_price']);
$goods[$idx]['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$goods[$idx]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
$goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
if (in_array($row['goods_id'], $type_array['best']))
{
$type_goods['best'][] = $goods[$idx];
}
if (in_array($row['goods_id'], $type_array['new']))
{
$type_goods['new'][] = $goods[$idx];
}
if (in_array($row['goods_id'], $type_array['hot']))
{
$type_goods['hot'][] = $goods[$idx];
}
}
}
return $type_goods[$type];
}
[/php]
這樣改了以后,Bulid的靜態文件應該只有13K左右,速度大大加快。
新聞熱點
疑難解答