Database tutorial query cache files under the ecshop/upload/temp/query_cache Directory
Compile a test file test1.php tutorial under ecshop/upoad/to test its query cache.
The code is as follows:
Php code
1. <? Php
2. // The first three sentences mainly involve initialization, so that I can use the class library functions in ecshop.
3. define ('in _ ECS', true );
4. define ('ecs _ admin', true );
5. require (dirname (_ FILE _). '/includes/init. Php ');
6 ./*******
7. The GetALLCached function contains an SQL statement parameter,
8. Here we can simply understand this function as an operation to execute an SQL query to obtain the result.
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. // The output result of the printed array is as follows:
14 ./*
15. Array
16 .(
17. [0] => Array
18 .(
19. [article_id] => 35
20. [title] => I am the master of the "wo" world
21 .)
22.
23. [1] => Array
24 .(
25. [article_id] => 34
26. [title] => 3G knowledge popularization
27 .)
28.
29. [2] => Array
30 .(
31. [article_id] => 32
32. [title] => mobile game download
33 .)
34.
35. [3] => Array
36 .(
37. [article_id] => 31
38. [title] => Nokia 6681 mobile phone advertisement appreciation
39 .)
40.
41 .*/
42.?>
Now let's go to the ecshop/upload/temp/query_caches directory and find that there is an additional sqlcache file. Let's open it.
Php code
1. <? Php exit;?> Listen 3944294a: 4: {I: 0; a: 2: {s: 10: "article_id"; s: 2: "35"; s: 5: "title"; s: 27: "The world of" wo "is my master";} 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: "mobile game download";} I: 3; a: 2: {s: 10: "article_id "; s: 2: "31"; s: 5: "title"; s: 31: "Nokia 6681 mobile phone ad appreciation ";}}
Discover the processing of a string (actually processed by the serialize function)
The content in this sqlcache is actually the result of print_r ($ article_array) just printed.
$ Db-> GetALLCached () is used in the test1.php file.
It is from the ecshop/upload/uplodes/cls_mysql tutorial. Php file.
GetALLCached () this function calls two functions that actually process the cache (that is, the sqlcache file just now.
The first function is setSqlCacheData.
Php code
1.
2 ./*
3. sets the query cache function. If the query result of the SQL statement in the GetALLCached function parameter is not cached, this function is executed first, this function is used to serialize the SQL result array and store it under a specific path.
4 .*/
5. function setSqlCacheData ($ result, $ data)
6 .{
7. if ($ result ['storecache'] ===true & $ result ['filename'])
8 .{
9. @ file_put_contents ($ result ['filename'], '<? Php exit;?> '. Time (). serialize ($ data ));
10. clearstatcache ();
11 .}
12 .}
The second function is getSqlCacheData.
Php code
1 ./*******
2.
3. This function is mainly used to restore serialized files from the SQL cache database query cache files.
4. In this way, if you execute another SQL statement and the result of the SQL statement has been cached, you do not need to query the database and read it directly from the cache file.
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 .}