[ecshop模板]ecshop 数据库查询缓存详解

时间:2016-06-17  来源:开发手册  阅读:

数据库教程查询缓存文件在ecshop/upload/temp/query_cache目录下面
在ecshop/upoad/下面编写一个测试文件test1.php教程,用来测试它的查询缓存。
代码如下:
 
Php代码
1.  2. //前面三句主要是做些初始化工作,让我可以使用ecshop里面的类库函数等 
3. define("IN_ECS", true); 
4. define("ECS_ADMIN", true); 
5. require(dirname(__FILE__) . "/includes/init.php"); 
6. /*******
7. 这里的GetALLCached函数里面带了一个sql语句的参数,
8. 这里可以先简单的把这个函数理解成执行sql查询获取结果的操作。
9. *******/ 
10

. $article_array = $db->GetALLCached("SELECT article_id, title FROM " . $ecs->table("article") . " WHERE cat_id != 0 AND is_open = 1 AND open_type = 0 ORDER BY article_id DESC LIMIT 0,4"); 
11.  
12. print_r($article_array); 
13. //打印出来的数组结果如下 
14. /*
15. Array
16. (
17.     [0] => Array
18.         (
19.             [article_id] => 35
20.             [title] => “沃”的世界我做主
21.         )
22. 
23.     [1] => Array
24.         (
25.             [article_id] => 34
26.             [title] => 3G知识普及
27.         )
28. 
29.     [2] => Array
30.         (
31.             [article_id] => 32
32.             [title] => 手机游戏下载
33.         )
34. 
35.     [3] => Array
36.         (
37.             [article_id] => 31
38.             [title] => 诺基亚6681手机广告欣赏
39.         )
40. 
41. */ 
42. ?> 
 


 我们现在去看ecshop/upload/temp/query_caches目录,发现多了一个sqlcache的文件,我们打开来看
 
Php代码

1. 1273944294a:4:{i:0;a:2:{s:10:"article_id";s:2:"35";s:5:"title";s:27:"“沃”的世界我做主";}i:1;a:2:{s:10:"article_id";s:2:"34";s:5:"title";s:14:"3G知识普及";}i:2;a:2:{s:10:"article_id";s:2:"32";s:5:"title";s:18:"手机游戏下载";}i:3;a:2:{s:10:"article_id";s:2:"31";s:5:"title";s:31:"诺基亚6681手机广告欣赏";}} 


 
 发现是一串经过什么处理的东西(实际上是经过serialize函数处理)
这个sqlcache里面的东西实际上就是刚才打印出来的print_r($article_array)的结果。
在刚才的test1.php文件中使用了$db->GetALLCached()这个函数
它来自ecshop/upload/includes/cls_mysql教程.php文件中
GetALLCached()该函数会调用到2个实际来处理缓存(就是刚才的sqlcache文件)的函数。
第一个函数是setSqlCacheData
 
Php代码
1.      
2. /*
3. 设置查询缓存的函数,如果GetALLCached函数参数中的sql语句的查询结果没有被缓存, 则先执行的是这个函数,该函数的作用是将sql获取的结果数组经过序列化以后存储在某个路径下面
4. */ 

5. function setSqlCacheData($result, $data) 
6.     { 
7.         if ($result["storecache"] === true && $result["filename"]) 
8.         { 
9.             @file_put_contents($result["filename"], "" . time() . serialize($data)); 
10.             clearstatcache(); 
11.         } 
12.     } 


 第二个函数是getSqlCacheData
Php代码
1. /*******
2. 
3. 该函数的主要作用是从sqlcache数据库查询缓存文件中将被序列化过的文件还原。
4. 这样就如果再执行某条sql语句,该sql语句的结果已经被缓存下来,就不用再去数据库查询了,直接去缓存文件中读取。
5. ******/  
6.

 function getSqlCacheData($sql, $cached = "") 
7.     { 
8.         $sql = trim($sql); 
9.  
10.         $result = array(); 
11.         $result["filename"] = $this->root_path . $this->cache_data_dir . "sqlcache_" . abs(crc32($this->dbhash . $sql)) . "_" . md5($this->dbhash . $sql) . ".php"; 
12.  
13.         $data = @file_get_contents($result["filename"]); 
14.         if (isset($data{23})) 
15.         { 
16.             $filetime = substr($data, 13, 10); 
17.             $data     = substr($data, 23); 
18.              
19.             if (($cached == "FILEFIRST" && time() > $filetime + $this->max_cache_time) || ($cached == "MYSQLFIRST" && $this->table_lastupdate($this->get_table_name($sql)) > $filetime)) 
20.             { 
21.                 $result["storecache"] = true; 
22.             } 
23.             else 
24.             { 
25.                 $result["data"] = @unserialize($data); 
26.                 if ($result["data"] === false) 
27.                 { 
28.                     $result["storecache"] = true; 
29.                 } 
30.                 else 
31.                 { 
32.                     $result["storecache"] = false; 
33.                 } 
34.             } 
35.         } 
36.         else 
37.         { 
38.             $result["storecache"] = true; 
39.         } 
40.  
41.         return $result; 
42.     }   

[ecshop模板]ecshop 数据库查询缓存详解

http://m.bbyears.com/shipin/24686.html

推荐访问:ecshop下载 ecshop二次开发教程 ecshop官网 ecshop安装教程 ecshop数据库配置文件 ecshop表结构
相关阅读 猜你喜欢
本类排行 本类最新