如果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() 函数,原始的大概是:

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

我的思路就是不读取所有的商品信息,而是Best、Hot、New够用展示就行了,所以把代码调整了一下,限制了Bulid的商品数,去掉了一些我不需要的Join表,当然大家不一定要照着我做,我讲的都是思路,看自己的实际情况,要有点程序基本功。

更改后的代码如下:

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

这样改了以后,Bulid的静态文件应该只有13K左右,速度大大加快。

转载:http://blog.sina.com.cn/s/blog_70ea94110101h5d3.html

ECshop网店系统百万级商品量性能优化-加快首页访问速度的更多相关文章

  1. ECshop网店系统百万级商品量性能优化-简单的一些Cache内存配置

    ECshop网店系统对于产品的数据.模板.Query都可以缓存,也就是把一些商品详情页.分类页.Search页的数据经过一次访问后,用文件的形式保存下来,下次有人访问相同的页面时,不用再查数据库,直接 ...

  2. 让Ecshop网店系统用户自动登陆

    让Ecshop网店系统用户户自动登陆,打开ecshop includes/init.php文件,可以发现Ecshop系统判断用户的SESSION不存在的时候会去读取存储在COOKIES里面的值.如下代 ...

  3. DBShop 电子商务网店系统

    DBShop 电子商务网店系统,采用业界知名框架 ZendFramework 2 开发而成. 下面为功能简介 1.在线更新:在线系统更新和在线模板安装与更新,简单.方便.快捷,省却了手动更新的繁琐步骤 ...

  4. 《开源网店系统iWebShop2.0模板开发教程》的说明

    <开源网店系统iWebShop2.0模板开发教程>是网上广为流传的一个文档,有点问题. 其中的第4章: ========================================== ...

  5. 10款PHP开源网店系统

    在当今经济危机的大环境下,网上购物越来越来吃香,网上开店成本低,快捷方便,出名的电子商务网站有淘宝,拍拍,Ebay或是最新的百度有啊,这些网站都提供开店的机会,如果是想自己搭建购物平台,可以从下面选择 ...

  6. 海盗(Haidao)网店系统最新官方版

    产品介绍:经过开发团队半个月的努力,Haidao v1.1 beta版终于和大家见面了,在这个版本中我们重点系统目录结构和整体框架进行了改进,同时新增加了多个功能模块. 四大功能模块意在打造最灵活的电 ...

  7. 亿级 Elasticsearch 性能优化

    前言 最近一年使用 Elasticsearch 完成亿级别日志搜索平台「ELK」,亿级别的分布式跟踪系统.在设计这些系统的过程中,底层都是采用 Elasticsearch 来做数据的存储,并且数据量都 ...

  8. Spark难道比oracle性能还差?百万级数据测试性能

    本人对大数据方面也是刚刚研究,由于工作需要在实时查询与统计的性能方面要深入学习.现测试性能如下:   环境:VirtualBox  host-only ubuntu版本: Linux master 4 ...

  9. 设置ecShop网店用户名和email均可登录

    修改user.php文件,如果您网站的该文件修改过,按照下面的修改说明修改文件. 查找代码:elseif ($action == 'act_login') 在:$back_act = isset($_ ...

随机推荐

  1. 《Linux内核修炼之道》 之 高效学习Linux内核

    http://blog.csdn.net/fudan_abc/article/details/5738436

  2. Gradle的安装与配置

    Gradle的下载地址:http://gradle.org/gradle-download/ Tips:要先安装JDK并配置其环境变量. 下载完成后将里面的文件解压出来,如下图所示: 添加一个名称为G ...

  3. C++中名字隐藏,名字查找优先于类型检查

    题目 C++中名字隐藏是什么? 解答 让我们通过一个例子来讲解C++中的名字隐藏.在C++中,如果一个类里有一个重载的方法, 你用另一个类去继承它并重写(覆盖)那个方法.你必须重写所有的重载方法, 否 ...

  4. phpcms v9 模板标签说明整理

    1.{template "content","header"} 2.网站网址调用:{siteurl($siteid)}: 3.标签get:分页,{pc:get ...

  5. mod_rewrite模块详解

    mod_rewrite模块提供了一个基于规则的(使用正则表达式分析器的)实时转向URL请求的引擎. 支持每个规则可以拥有不限数量的规则以及附加条件规则的灵活而且强大的URL操作机制. 此URL操作可以 ...

  6. Internationalization

    Internationalization If you are building a site for an international audience, you will likely want ...

  7. STC89C52单片机内部EEPROM驱动

    STC89C52单片机自身带有4K的存储空间,分为8个扇区,每个扇区512字节,第一扇区起始地址为:0x2000, 结束地址为:21FF, 第八扇区起始地址为0x2E00,结束地址是2FFF #inc ...

  8. VIM 分割窗口

    VIM 分割窗口     *08.1*  分割窗口 打开新窗口最简单的命令如下: :split 这个命令把屏幕分解成两个窗口并把光标置于上面的窗口中: +----------------------- ...

  9. jvm内存模型及分配参数

    jvm内存模型 程序计数器:是一块很小的内存空间.当线程数量超过cpu数量时,线程之间根据时间片轮询抢夺cpu资源.每一个线程都必须用一个独立的程序计数器,用于记录下一条要运行的指令. java虚拟机 ...

  10. C语言---字符

    1.三元符(三字母词):由三个字符组合起来代表其他字符,三元符可以在没有一些字符时使用 ??( [ ??) ] ??! | ??< { ??> } ??' ^ ??= # ??/ \ ?? ...