自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP數(shù)據(jù)緩存類必要性分析

開發(fā) 后端
PHP數(shù)據(jù)緩存類對(duì)于一個(gè)不經(jīng)常變化內(nèi)容的數(shù)據(jù)庫(kù)來(lái)說(shuō)是非常必要的,可以幫助我們減輕大量的負(fù)擔(dān)。下面就來(lái)一起看看具體使用方法。

大家通過(guò)對(duì)PHP語(yǔ)言進(jìn)一步的了解,可以知道,它是一款功能強(qiáng)大的嵌入式HTML腳本語(yǔ)言。雖然PHP的執(zhí)行效率很高,速度很快,但是連接數(shù)據(jù)庫(kù)、查詢數(shù)據(jù)庫(kù)等還是比較耗時(shí)的。#t#

如果訪問(wèn)量大的話會(huì)給數(shù)據(jù)庫(kù)造成很大的負(fù)擔(dān),所以對(duì)于變化不經(jīng)常的內(nèi)容要做好PHP數(shù)據(jù)cache(緩存)是十分必要的,我做了一個(gè)簡(jiǎn)單的PHP數(shù)據(jù)緩存類,希望對(duì)大家有所幫助。

思路是這樣的:

對(duì)于一般的變量,把該變量變成PHP語(yǔ)言的格式,寫到文件中,用時(shí)只要include這個(gè)文件就相當(dāng)于加載了PHP數(shù)據(jù)緩存類了;

對(duì)于array型的變量,把a(bǔ)rray轉(zhuǎn)化為PHP語(yǔ)言定義array的字符串,寫到文件中,用時(shí)也只要include就相當(dāng)于加載了cache了;

PHP數(shù)據(jù)緩存類時(shí)間上的控制,通過(guò)獲取緩存文件的創(chuàng)建時(shí)間和現(xiàn)在的時(shí)間進(jìn)行對(duì)比,如果沒(méi)有到更新時(shí)間,直接讀取緩存,如果到了更新時(shí)間,查詢數(shù)據(jù)庫(kù),返回?cái)?shù)據(jù),再更新緩存。(尚未實(shí)現(xiàn))

下面是我的PHP-kcache類(PHP_kcache_class.PHP):

注:如果是緩存字符串,請(qǐng)把轉(zhuǎn)義字符多寫一個(gè)’\',即”\n”要寫成”\\n”。

  1. /*  
  2. //PHP-kcache class v_0.1  
  3. //Author: kangzj  
  4. //Email : kangzj@mail.bnu.edu.cn  
  5. //Blog : http://kangzj.net.ru  
  6. //作者不保證本程序沒(méi)有bug,對(duì)于使用本程序  
  7. //而引起的任何問(wèn)題不擔(dān)負(fù)任何責(zé)任。  
  8. */  
  9. class PHP_kcache {  
  10. //相對(duì)或者絕對(duì)目錄,末尾不要加 '/'  
  11. var $cache_dir = './cache';  
  12. var $cache_extension = '.cache.PHP';  
  13. function set_cache($name, $value){  
  14. $pre = "< ?\n//Cache Created at: "
    .date('Y-m-d H:i:s')."\n";  
  15. if(!is_array($value)){  
  16. $value = $value;  
  17. $str = "\$$name = '$value';";  
  18. }else{  
  19. $str = "\$$name = " . $this->
    arrayeval($value) . ';';  
  20. }  
  21. $end = "\n?>";  
  22. echo $cache = $pre . $str . $end;  
  23. $cache_file = $this->cache_dir . 
    '/' . $name . $this-
    >cache_extension;  
  24. if($fp = @fopen($cache_file, 'wb')) {  
  25. fwrite($fp, $cache);  
  26. fclose($fp);  
  27. return true;  
  28. } else {  
  29. echo $cache_file;  
  30. exit('Can not write to cache files, 
    please check cache directory ');  
  31. return false;  
  32. }  
  33. }  
  34. //將array變成字符串, 來(lái)自discuz!  
  35. function arrayeval($array, $level = 0) {  
  36. if(!is_array($array)) {  
  37. return "'".$array."'";  
  38. }  
  39. $space = '';  
  40. for($i = 0; $i < = $level; $i++) {  
  41. $space ."\t";  
  42. }  
  43. $evaluate = "Array\n$space(\n";  
  44. $comma = $space;  
  45. if(is_array($array)) {  
  46. foreach($array as $key => $val) {  
  47. $key = is_string($key) ? '\''.addcslashes
    ($key, '\'\\').'\'' : $key;  
  48. $val = !is_array($val) &&
     (!preg_match("/^\-?[1-9]\d*$/", $val)
     || strlen($val) 
    > 12) ? '\''.addcslashes
    ($val, '\'\\').'\'' : $val;  
  49. if(is_array($val)) {  
  50. $evaluate ."$comma$key => ".
    arrayeval($val, $level + 1);  
  51. } else {  
  52. $evaluate ."$comma$key => $val";  
  53. }  
  54. $comma = ",\n$space";  
  55. }  
  56. }  
  57. $evaluate ."\n$space)";  
  58. return $evaluate;  
  59. }  


最簡(jiǎn)單的PHP數(shù)據(jù)緩存類調(diào)用方法:

  1. include './PHP_kcache_class.PHP';  
  2. $pc = new PHP_kcache;  
  3. $a = array('a', 'b', 'c');  
  4. $pc->set_cache('a', addslashes($a));  
責(zé)任編輯:曹凱 來(lái)源: 百度博客
相關(guān)推薦

2023-05-31 10:02:29

人工智能

2014-02-17 09:37:31

亞馬遜WorkSpacesVDI

2012-06-15 09:37:40

Linuxmark

2011-11-08 21:19:25

2023-08-14 16:13:08

數(shù)字化轉(zhuǎn)型數(shù)據(jù)中臺(tái)

2009-07-03 17:37:54

JSP數(shù)據(jù)庫(kù)

2018-10-17 10:46:54

區(qū)塊鏈互操作性比特幣

2021-10-24 08:39:07

攻擊面管理網(wǎng)絡(luò)攻擊黑客

2011-06-14 10:20:20

URL標(biāo)準(zhǔn)化

2023-09-14 15:58:34

C++表達(dá)式

2017-12-12 10:45:50

機(jī)房空調(diào)防雷

2022-02-13 00:29:57

云安全云計(jì)算安全

2010-06-24 16:38:20

綜合布線

2024-08-29 15:14:52

2011-12-13 20:36:26

Android

2020-05-20 07:00:00

DevOps端點(diǎn)檢測(cè)網(wǎng)絡(luò)攻擊

2021-01-21 21:07:03

信息安全漏洞治理

2010-12-28 09:29:00

2023-03-07 13:28:17

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)