刚接触Ecmall的二次开发不久,接到一个任务。很常见的任务,主要是对数据库进行一些操做,其中查询的方法我写成这样:php
01 |
function get_order_data( $goods_id ) |
02 |
{ |
03 |
include_once ( "gonndb/nmdb.php" ); |
04 |
include_once ( "gonndb/dbinfo.php" ); |
05 |
|
06 |
$connector = new nmdb( $host , $username , $password ); |
07 |
$connector -> select_db( $database ); |
08 |
09 |
$sql = "select a.buyer_name, a.add_time, a.status, b.phone_tel, b.phone_mob, c.price, c.quantity |
10 |
from shop_order a, shop_order_extm b, shop_order_goods c |
11 |
where a.order_id = b.order_id and b.order_id = c.order_id |
12 |
and c.goods_id = '".$goods_id."' |
13 |
order by a.add_time desc |
14 |
limit 3 "; |
15 |
|
16 |
$result = $connector -> query( $sql ); |
17 |
|
18 |
//$r = array(); |
19 |
while ( $myrow = $connector -> fetch_array( $result )) |
20 |
{ |
21 |
$r [] = $myrow ; |
22 |
} |
23 |
|
24 |
return $r ; |
25 |
} |
发现第一次查询的数据是对的,而后通过模板解析后的数据怎么都不正确。后来发现,Ecmall有这么一个机制。先是通过app进行数据库操做,操做完毕后会在 temp/compileed/ 下留下模板缓存,并且第一次数据库查询后会产生数据库缓存。这压根就说明,二次开发,最好不要用本身的数据库函数,用Ecmall自带的比较好。上面的方法改为:html
01 |
function get_order_data( $goods_id ) |
02 |
{ |
03 |
$db = &db(); |
04 |
|
05 |
$sql = "select a.buyer_name, a.add_time, a.status, b.phone_tel, b.phone_mob, c.price, c.quantity |
06 |
from shop_order a, shop_order_extm b, shop_order_goods c |
07 |
where a.order_id = b.order_id and b.order_id = c.order_id |
08 |
and c.goods_id = '".$goods_id."' |
09 |
order by a.add_time desc |
10 |
limit 3 "; |
11 |
12 |
$result = $db -> query( $sql ); |
13 |
|
14 |
$r = array (); |
15 |
while ( $myrow = $db -> fetch_array( $result )) |
16 |
{ |
17 |
$r [] = $myrow ; |
18 |
} |
19 |
|
20 |
return $r ; |
21 |
} |
这个函数只是使用了Ecmall自带的数据库函数,仍是没有产生数据库缓存。看一下别人写的,如何才能产生数据库缓存呢?sql
下面是一个公告挂件的程序:数据库
01 |
<?php |
02 |
03 |
/** |
04 |
* 公告栏挂件 |
05 |
* |
06 |
* @param string $ad_image_url 广告图片地址 |
07 |
* @param string $ad_link_url 广告连接地址 |
08 |
* @return array |
09 |
*/ |
10 |
class NotWidget extends BaseWidget |
11 |
{ |
12 |
var $_name = 'not' ; |
13 |
var $_ttl = 86400; |
14 |
var $_num = 3; |
15 |
16 |
function _get_data() |
17 |
{ |
18 |
// 建立一个缓存对象 |
19 |
$cache_server =& cache_server(); |
20 |
// 获取该缓存对象数据的id |
21 |
$key = $this ->_get_cache_id(); |
22 |
// 凭证领取对象书记 |
23 |
$data = $cache_server ->get( $key ); |
24 |
$data1 = $cache_server ->get( $key ); |
25 |
if ( $data === false) |
26 |
{ |
27 |
$acategory_mod =& m( 'acategory' ); |
28 |
$article_mod =& m( 'article' ); |
29 |
$data = $article_mod ->find( array ( |
30 |
'conditions' => 'cate_id=' . $acategory_mod ->get_ACC(ACC_NOTICE) . ' AND if_show = 1' , |
31 |
'order' => 'sort_order ASC, add_time DESC' , |
32 |
'fields' => 'article_id, title, add_time' , |
33 |
'limit' => $this ->_num, |
34 |
)); |
35 |
$cache_server ->set( $key , $data , $this ->_ttl); |
36 |
} |
37 |
if ( $data1 === false) |
38 |
{ |
39 |
$acategory_mod1 =& m( 'acategory' ); |
40 |
$article_mod1 =& m( 'article' ); |
41 |
$data1 = $article_mod1 ->find( array ( |
42 |
'conditions' => 'cate_id=' . $acategory_mod1 ->get_ACC(ACC_HELP) . ' AND if_show = 1' , |
43 |
'order' => 'sort_order ASC, add_time DESC' , |
44 |
'fields' => 'article_id, title, add_time' , |
45 |
'limit' => $this ->_num, |
46 |
)); |
47 |
$cache_server ->set( $key , $data1 , $this ->_ttl); |
48 |
} |
49 |
50 |
return array ( |
51 |
'notices' => $data , |
52 |
'systems' => $data1 , |
53 |
); |
54 |
} |
55 |
} |
56 |
57 |
?> |
看了程序,我发现ECMALL的文章调用是这样的。定义一个data,一堆调用最后经过 ACC_NOTICE 来肯定调用的分类的。最后经过缓存
1 |
return array ( |
2 |
'notices' => $data , |
3 |
); |
来对应一下。网络
谈谈写入数据查询缓存的步骤:app
1 |
// 1. 建立缓存对象 |
2 |
$cache_server =& cache_server(); |
3 |
// 2. 获取缓存数据的 id |
4 |
$key = $this ->_get_cache_id(); |
5 |
// 自定义也能够 |
6 |
$key = 'page_of_goods_' . $id ; |
7 |
// 将key,数据,缓存时间设置好 |
8 |
$cache_server ->set( $key , $data , 1800); |
若是是缓存模板文件的话,那就算不使用内存来缓存从数据库读取的数据也不要紧,由于最后会把模板文件连同数据一块儿缓存进一个文件,那后面读取时就只读取这个文件而不用再去缓存数据库,但与静态化有点不一样,这个文件里面还有一点php信息,并不能用来直接输出,模板引擎会在去掉这点php信息后输出。frontend
在使用ecmall2时也以为有时反应不过来,相比来说ecshop还快,通过观察,其实ecshop在display时是加了一个id的,就是用来缓存此次输出的。而咱们如今要作的就是在ecmall2里面实现缓存模板输出,通俗点讲就是在$this->display()时给一个id这个id要惟一。函数
其实全部的东西ecmall2已经准备好了,只是不知道为何没有使用,详细的原理再也不介绍修改完后就可使你的商城在运行方面的速度上一层楼,可是网络方面可不包哦。fetch
咱们以商品详细页为例:
再修改eccore/view/template.php的function fetch($filename, $cache_id = '')方法以下。
01 |
function fetch( $filename , $cache_id = '' ) |
02 |
{ |
03 |
if (! $this ->_seterror) |
04 |
{ |
05 |
error_reporting (E_ALL ^ E_NOTICE); |
06 |
} |
07 |
$this ->_seterror++; |
08 |
09 |
if ( strncmp ( $filename , 'str:' , 4) == 0) |
10 |
{ |
11 |
$out = $this ->_eval( $this ->fetch_str( substr ( $filename , 4))); |
12 |
} |
13 |
else |
14 |
{ |
15 |
if ( $this ->_checkfile) |
16 |
{ |
17 |
if (! is_file ( $filename )) |
18 |
{ |
19 |
$filename = $this ->template_dir . '/' . $filename ; |
20 |
} |
21 |
} |
22 |
else |
23 |
{ |
24 |
$filename = $this ->template_dir . '/' . $filename ; |
25 |
} |
26 |
27 |
if ( $this ->direct_output) |
28 |
{ |
29 |
$this ->_current_file = $filename ; |
30 |
$out = $this ->_eval( $this ->fetch_str( file_get_contents ( $filename ))); |
31 |
} |
32 |
else |
33 |
{ |
34 |
35 |
if ( $this ->is_cached( $filename , $cache_id )&& $cache_id && $this ->caching) |
36 |
{ |
37 |
$out = $this ->template_out; |
38 |
} |
39 |
else |
40 |
{ |
41 |
if (!in_array( $filename , $this ->template)) |
42 |
{ |
43 |
$this ->template[] = $filename ; |
44 |
} |
45 |
46 |
$out = $this ->make_compiled( $filename ); |
47 |
48 |
if ( $cache_id ) |
49 |
{ |
50 |
if ( $this ->appoint_cache_id) |
51 |
{ |
52 |
$cachename = $cache_id ; |
53 |
} |
54 |
else |
55 |
{ |
56 |
$cachename = basename ( $filename , strrchr ( $filename , '.' )) . '_' . $cache_id ; |
57 |
} |
58 |
$data = serialize( array ( 'template' => $this ->template, 'expires' => $this ->_nowtime + $this ->cache_lifetime, 'maketime' => $this ->_nowtime)); |
59 |
$out = str_replace ( "\r" , '' , $out ); |
60 |
61 |
while ( strpos ( $out , "\n\n" ) !== false) |
62 |
{ |
63 |
$out = str_replace ( "\n\n" , "\n" , $out ); |
64 |
} |
65 |
66 |
if (! file_exists ( $this ->cache_dir)) |
67 |
{ |
68 |
ecm_mkdir( $this ->cache_dir); |
69 |
} |
70 |
71 |
if ( file_put_contents ( $this ->cache_dir . '/' . $cachename . '.php' , '<?php exit;?>' . |