首頁  >  文章  >  後端開發  >  細說PHP下的快取技術

細說PHP下的快取技術

WBOY
WBOY原創
2016-07-25 09:11:47816瀏覽

常用快取技術

資料快取:這裡所說的資料快取是指資料庫查詢緩存,每次造訪頁面的時候,都會先偵測對應的快取資料是否存在,如果不存在,就連結資料庫,得到數據,並把查詢結果序列化後保存到文件中,以後同樣的查詢結果就直接從快取表或文件中獲得。

用的最廣的例子看Discuz的搜尋功能,把結果ID快取到一個表中,下次搜尋相同關鍵字時先搜尋快取表。

舉個常用的方法,多表關聯的時候,把附表中的內容生成數組保存到主表的一個字段中,需要的時候數組分解一下,這樣的好處是只讀一個表,壞處就是兩個資料同步會多不少步驟,資料庫永遠是瓶頸,用硬碟換速度,是這個的關鍵點。

頁面快取: 每次訪問頁面的時候,都會先檢測相應的緩存頁面文件是否存在,如果不存在,就連接數據庫,得到數據,顯示頁面並同時生成緩存頁面文件,這樣下次訪問的時候頁面文件就發揮作用了。 (模板引擎和網路上常見的一些快取類別通常有此功能)

時間觸發快取: 檢查檔案是否存在且時間戳小於設定的過期時間,如果檔案修改的時間戳比當前時間戳減去過期時間戳大,那麼就用緩存,否則更新快取。

內容觸發快取: 當插入資料或更新資料時,強制更新快取。

靜態快取: 這裡所說的靜態快取是指靜態化,直接產生HTML或XML等文字文件,有更新的時候重生成一次,適合於不太變化的頁面,這就不說了。

以上內容是程式碼級的解決方案,我直接CP別的框架,也懶得改,內容都差不多,很容易就做到,而且會幾種方式一起用,但下面的內容是伺服器端的快取方案,非代碼級的,要有多方的合作才能做到

記憶體快取: Memcached是高效能的,分散式的記憶體物件快取系統,用於在動態應用中減少資料庫負載,提升存取速度。

這裡說下Memcached的例子:

  1. $memcache = new Memcache;
  2. $memcache->connect('localhost', 11211) or die"CouldCould not connect");
  3. $version = $memcache->getVersion();
  4. echo "Server's version: ".$version."n";
  5. $tmp_object = new stdClass;
  6. $tmp_object ->str_attr = 'test';
  7. $tmp_object->int_attr = 123;
  8. $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server ");
  9. echo "Store data in the cache (data will expire in 10 seconds)n";
  10. $get_result = $memcache->get('key');
  11. echo "Data from the cache :n";
  12. var_dump($get_result);
  13. ?>
複製程式碼

讀庫的範例:

  1. $sql = 'SELECT * FROM users';
  2. $key = md5($sql); //memcached 物件';
  3. $key = md5($sql); //memcached 物件標識符
  4. if ( !($datas = $mc->get($key)) ) {
  5. // 在memcached 中未取得到快取數據,則使用資料庫查詢取得記錄集。
  6. echo "n".str_pad('Read datas from MySQL.', 60, '_')."n";
  7. $conn = mysql_connect('localhost', 'test', 'test');
  8. mysql_select_db('test');
  9. $result = mysql_query($sql);
  10. while ($row = mysql_fetch_object($result))
  11. $datas[] = $row;$mc->add($key, $datas);
  12. } else {
  13. echo "n".str_pad('Read datas from memcached.', 60, '_')."n" ;
  14. }
  15. var_dump($datas);
  16. ?>
複製程式碼

php的緩衝器: 有eaccelerator, apc, phpa,xcache,這個這個就不說了吧,搜尋一堆一堆的,自己看啦,知道有這玩意就OK

MYSQL快取: 這也算非程式碼級的,經典的資料庫就是用的這種方式,看下面的運行時間,0.09xxx之類的 我貼段根據藍色那傢伙修改後部分my.ini吧,2G的MYISAM表可以在0.05S左右,據說他前後改了有快一年

代碼拷貝框

  1. [client]
  2. ……
  3. default-character-set=gbk
  4. default-storage-engine=MYISAM
  5. _connection =600
  6. max_connect_errors=500
  7. back_log=200
  8. interactive_timeout=7200
  9. query_cache_size=64M
  10. …table_cache_5
  11. myisam_max_extra_sort_file_size=100G
  12. myisam_sort_buffer_size=128M
  13. key_buffer_size=1024M
  14. read_buffer_size=512M
  15. 複製程式碼

基於反向代理的Web快取: 如Nginx,SQUID,mod_proxy(apache2以上又分為mod_proxy和mod_cache) NGINX的例子

  1. #user nobody;
  2. worker_processes 4;
  3. error_log 日誌/error.log crit;
  4. pid log/nginx.pid;
  5. worker_rlimit_nofile 10240;
  6. 事件{
  7. 使用epoll;
  8. worker_connections 51200;
  9. }
  10. httpdefault_type application/octet-stream;
  11. sendfile on;
  12. keepalive_timeout 65;
  13. tcp_nodelay on;
  14. #33:0🎜>上游80 權重=1;
  15. 伺服器10.10.10.221:80 權重=1;
  16. }
  17. 上游bspimgsvr {

  18. 伺服器10.10.10.201:80 權重; >}
  19. 上游bspstylesvr {

  20. 伺服器10.10.10.202:80 權重=1;
  21. }
  22. 上游bsphelpsvr {

  23. 伺服器10.100伺服器10.10 .10.204:80 權重=1;
  24. }
  25. 上游bspwsisvr {

  26. 伺服器10.10.10.203:80 權重=1;
  27. }
  28. 上游bspadminsvr {
  29. 伺服器10.10.10.222:80 權重=1;
  30. }
  31. 上游bspbuyersvr {

  32. 伺服器10.10.10.223:80 權重; >}
  33. 上游bspsellersvr {

  34. 伺服器10.10.10.225:80 權重=1;
  35. }
  36. 上游bsploginsvr {
  37. 伺服器10.10.1
  38. 上游bsploginsvr {
  39. 伺服器10.10.10.220:43 重權;
  40. }
  41. 上游bspregistersvr {
  42. 伺服器10.10.10.220:80 權重=1;
  43. }
  44. log_format test_com '$remote_addr - $remote_user [$time_local ' >'$status $body_bytes_sent "$http_referer" "$http_user_agent" ';
  45. #---------------- -------------- ------------------------------------ --
  46. #img.test.com
  47. 伺服器{
  48. 監聽10.10.10.230:80;
  49. server_name img.test.com;
  50. 位置/ {
  51. proxy_pass http:// bspimgsvr;
  52. include proxy_setting.conf;
  53. access_log logs/img.log test_com;
  54. }
  55. #style.test.com

  56. 伺服器{
  57. 監聽10.10.10.230:80
  58. 伺服器名稱style.test.com;
  59. 位置/ {
  60. proxy_pass http://bspstylesvr;
  61. 包括proxy_setting.conf;
  62. }
  63. access_log logs/style.log test_com;
  64. }
  65. }
  66. #help.test.com

  67. 伺服器{
  68. 監聽10.10.10.230:80;
  69. 伺服器名稱help.test.com;
  70. 位置/ {
  71. proxy_pass http://bsphelpsvr;
  72. 包括proxy_setting.conf;
  73. }
  74. access_log 日誌/help.log test_com;
  75. }
  76. #admin.test. com

  77. 伺服器{
  78. 監聽10.10.10.230:80;
  79. 伺服器名稱admin.test.com;
  80. 位置/ {
  81. proxy_pass http://bspadminsvr;
  82. 包含proxy_setting.confproxy ;
  83. }
  84. access_log logs/admin.log test_com;
  85. }
  86. # buy.test.com

  87. 伺服器{
  88. 監聽10.10.10.230:80 ;
  89. server_name buy.test.com;
  90. 位置/ {
  91. proxy_pass http://bspbuyersvr;
  92. 包括proxy_setting.conf;
  93. }
  94. access_log logs/buyer.log test_com;
  95. }
  96. #seller.test.com

  97. 伺服器{
  98. 監聽10.10.10.230:80;
  99. 伺服器名稱seller.test.com;
  100. 位置/ {
  101. proxy_pass http://bspsellersvr;
  102. 包含proxy_setting.conf;
  103. }
  104. access_log 日誌/ seller.log test_com;
  105. }
  106. #wsi.test.com伺服器{
  107. 監聽10.10.10.230:80;
  108. 伺服器名稱wsi.test.com;
  109. 位置/ {
  110. proxy_pass http://bspwsisvr;
  111. 包括proxy_setting.conf;}
  112. access_log logs/wsi.log test_com;
  113. }
  114. #www.test.com
  115. server {
  116. 聽10.10.10.230:80;
  117. 伺服器名稱. com *.test.com;
  118. 位置~ ^/NginxStatus/ {
  119. stub_status 開啟;
  120. access_log 關閉;
  121. }
  122. 位置/ {
  123. proxy_pass http://bspfrontsvr; 🎜>包含proxy_setting.conf;
  124. }
  125. access_log 日誌/www.log test_com;
  126. error_page 500 502 503 504 /50x .html;
  127. 位置= /50x html {html> ;
  128. }
  129. }
  130. #login.test.com
  131. 伺服器{
  132. 監聽10.10.10.230:443 ;
  133. 伺服器名稱login.test.com;
  134. ssl 開啟;
  135. ssl_certificate cert.pem;
  136. ssl_certificate_key cert.key;
  137. ssl_session_timeout 5m;
  138. ssl_protocols SSLv2 SSLv3 TLSv1;
  139. ssl_protocols SSLv2 SSLv3 TLSv1;
  140. ssl_protocols SSLv2 SSLv3 TLSv1;
  141. ssl_protocols SSLv2 SSLv3 TLSv1;
  142. MEDIUM:+LOW:+SSLv2:+EXP;
  143. ssl_prefer_server_ciphers on;
  144. 位置/ {
  145. proxy_pass https://bsploginsvr;
  146. 包含proxy_setting.conf;}
  147. #login.test.com 用來註冊
  148. 伺服器{
  149. 監聽10.10.10.230 :80;
  150. 伺服器名稱login.test.com;
  151. 位置/ {
  152. proxy_pass http://bspregistersvr;
  153. 包含proxy_setting.conf;
  154. }
  155. access_log 日誌/register.log test_com;
  156. }

    }

  157. proxy_redirect 關閉;
  158. proxy_set_header 主機$host;
  159. proxy_set_header X-真實IP $remote_addr;
  160. proxy_set_header X-真實IP $remote_addr;<_proward_proward_proward_prodward>client_max_body_size 10m;
  161. client_body_buffer_size 128k;
  162. proxy_connect_timeout 90;
  163. proxy_send_timeout 90;proxy_busy_buffers_size 64k;
  164. proxy_temp_file_write_size 64k;
  165. 複製程式碼

mod_proxy的範例:

  1. ServerName www.zxsv.com
  2. ServerAdmin admin@zxsv.com
  3. ProxyPass / http://www.zxsv.com:8080/
  4. ProxyPassReverse / http://www.zxsv.com:8080/
  5. # cache dir root
  6. CacheRoot "/var/www/proxy"
  7. # max cache storage
  8. CacheSize 50000000
  9. # hour: every 4 hour
  10. CacheGcInterval 4
  11. # max page expire time: hourCacheLastModifiedFactor 0.1
  12. # defalt expire tag: hour
  13. CacheDefaultExpire 1
  14. # force complete 資料>CacheDefaultExpire 1
  15. # force complete 資料片> tomLog / usr/local/apache/logs/dev_access_log combined
  16. 複製程式碼
而有關SQUID的例子,可以參考下這裡。 DNS輪詢: BIND是一款開放原始碼的DNS伺服器軟體,這個要說起來就大了,自己搜尋去,大家知道有這個東西就行了。 我知道的有chinacache等大站就是這樣做的,說簡單點就是多伺服器啦,把同一個頁面或檔案快取到不同的伺服器上,按南北自動解析到相關的伺服器中。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn