搜尋
首頁後端開發php教程PHP企業級應用程式快取技術詳解

之前我們曾深入的探討過PHP快取技術,其中主要提到了資料快取。資料快取主要是指資料庫查詢緩存,每次存取頁面的時候,都會先偵測對應的快取資料是否存在,如果不存在,就連接資料庫,得到數據, 並把查詢結...     




  之前我們曾深入的探討PHP快取技術,其中主要提到了資料快取。資料快取主要是指資料庫查詢緩存,每次造訪頁面的時候,都會先檢測相應的快取資料是否存在,如果不存在,就連接資料庫,得到數據, 並把查詢結果序列化後儲存到文件中,以後同樣的查詢結果就直接從快取表或文件中取得。 

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

  舉個常用的方法,多表關聯的時候,把附表中的內容生成數組保存到主表的一個字段中,需要的時候數組分解一下,這樣的好處是只讀一個表,壞處就是兩個資料同步會多不少步驟,資料庫永遠是瓶頸,用硬碟換速度,是這個的關鍵點。
  每次訪問頁面的時候,都會先檢測相應的緩存頁面文件是否存在,如果不存在,就連接數據庫,得到數據,顯示頁面並同時生成緩存頁面文件,這樣下次訪問的時候頁面文件就發揮作用了。 (模板引擎和網路上常見的一些快取類別通常有此功能) 

  時間觸發快取 

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

  內容觸發快取 

  當插入資料或更新資料時,強制更新快取。 

  靜態快取 

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

  記憶體快取 

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



    $memcache = new Memcache; 

  $memcache->connect('localhost', 11211) $memcache->connect('localhost', 11211) 11121" = $memcache->getVersion() ; 

  echo “Server's version: “.$version.”n”; 

  $tmp_object = new stdClass; 

_test.mp_object = new stdClass; 

_test. ject->int_attr = 123; 

  $ memcache->set('key', $tmp_object, false, 10) or die (“Failed to save data at the server”); 

  echo “Store data in the cache (data will expire in 10 seconds);

  $get_result = $memcache->get('key'); 

  echo “Data from the cache:n”; 

 『var_dump($get_result);
  讀庫的範例: 



    $sql = 'SELECT * FROM users'; 

  $key = md5($sql); //4mmc key)) ) { 

  // 在memcached 中未取得到快取數據,則使用資料庫查詢取得記錄集 

  echo “n”.str_pad('Read datas from My.', 60, '_'). n”; 

  $conn = mysql_connect('localhost', 'test', 'test'); 

  mysql_select_db('test'); hile ($row = mysql_fetch_object($result)) 

  $datas[] = $row; 

  // 將資料庫中取得的結果集資料儲存到memcached 中,以便在下次存取時使用$com key, $datas); 

  } else { 

  echo “n”.str_pad('Read datas from memcached.', 60,} '_').”n”; s);

  ?> 



  PHP的緩衝器 

  例如eaccelerator,apc,phpa,xcache等等。 

  MySQL快取 

  這也算非程式碼級的,經典的資料庫就是用的這種方式,看下面的運行時間,0.09xxx之類的。 



  [client] 

  …… 

  default-character-set=gbk 
『『+〦 ections=600 

max_connect_errors=500 

  back_log=200 

  interactive_timeout=7200 

 『 table_cache=512 

  …… 

  myisam_max_sort_file_size=100G  

 myisam_max_exmort uffer_size=128M 

  key_buffer_size= 1024M 

  read_buffer_size=512M 

  … 

  thread_concurrency=88『 如Nginx,SQUID,mod_PRoxy(apache2以上又分為mod_proxy和mod_cache) 

  NGINX的例子:



  #user nobody; 

  worker_processes 4; 

  error_log logs/error.log crito;  worker_rlimit_nofile 10240; 

  events { 

  use epoll; 

  worker_connections 512000 ; 

  } 

  http { 

  include mime.types; 

 『
  keepalive_timeout 65; 

  tcp_nodelay on; 

  # server
  server 10.10.10.224:80 weight=1; 

  server 10.10.10.221:80 weight=1; 㟎

  server 10.10.10.201:80 weight=1; 

  } 

  upstream bspstylesvr { 

  server 10.10.10.202:80 weight=1; 

  } 

  upstream bsphelpsvr.

  } 

  upstream bspwsisvr { 

  server 10.10.10.203:80 weight =1; 

  } 

  upstream bspadminsvr { 

  server 10.10.10.222:80 weight=1; vr { 

  server 10.10.10.223:80 weight=1; 

  } 

  upstream bspsellers } 

  upvr bspsellerssvr { 

  server 10.10.10.225:80 weight=1; 

  } 

  upstream bsploginsvr { 

  } 

  upstream bspregistersvr { 

  server 10.10.10.220:80 weight =1; 

  } 

  log_format test_com '$remote_addr – $remote_user [$time_local] “$request” ' 
_$s
 靦""

  #————— ———————————————– 

  #img.test.com 

  server { 

  listen 10.10.10.230:80; location / { 

  proxy_pass http://bspimgsvr; 

  include proxy_setting.conf; 

  }  test>setting.conf; 

 〜}  test>test.test;  } 

  #style.test.com 

  server { 

  listen 10.10.10.230:80; 

  server_name style.test.com; 

  location / { 

 proxy_pass http://bspstylescom

  } 

  access_log logs/style.log test_com; 

  } 

  #help.test.com 

  server { 

  listen 10.10.10.

  listen 10.10.10.230:80$com;
  location / { 

  proxy_pass http://bsphelpsvr; 

  include proxy_setting .conf; 

  } 

  access_log logs/help.log test_com; 

  } 🎀  listen 10.10.10.230:80; 

  server_name admin.test.com; 

  location / { 

  proxy_pass http://bspadminsvr; 

  include proxy_setting.conf; 

  include

  } 

  #buyer.test.com 

  server { 

listen 10.10.10.230:80; 

  server_name buyer.test.com; 

  location / { 

 、 . 

  } 

  access_log logs/buyer.log test_com;

  } 



  #seller.test.com 

伺服器{

聽10.10.10.230:80; 

伺服器名稱seller.test.com; 

位置/ {


proxy_pass http://bspsellersvr;
佛access_log 日誌/seller.log test_com; 



#wsi.test.com 

伺服器{ 

聽10.10.10.233); / {


proxy_pass http://bspwsisvr; 

包括proxy_setting.conf; 

佛} 

佛語
凡伺服器{ 

凡聽10.10.10.230:80; 

server_name www.test.com *.test.com; 

位置~ ^/NginxStatus/ {


位置~


位置/ { 

proxy_pass http://bspfrontsvr; 

包含proxy_setting.conf; 

佛} 
505_Ftest> $505050505050 4票化。 2 503 504 / 50x.html; 

位置= /50x.html {

凡} 

位置= /50x.html {

凡} 

凡#login.test.com
凡} 

凡#login.test.comcom ;

伺服器名稱login.test.com; 

ssl 開啟; 

ssl_certificate cert.pem; 

:ssl_certificate_key cert.pem; 

:ssl_certificate_key cert.key; cols SSLv2 SSLv3 TLSv1; 

ssl_ciphers 全部:!ADH :!EXPORT56:RC4+RSA:+高:+中:+低:+SSLv2:+EXP; 

ssl_prefer_server_ciphers 開啟; 

位置/ {

_c

佛} 

佛access_log 日誌/login.log test_com; 

凡} 

凡#login.test.comcomcomo
凡} 

凡#login.test.com7050300600030003700370037名伺服器50. in .test.com; 

位置/ {


proxy_pass http://bspregistersvr; 

包含proxy_setting.conf; 

佛}  fxy test
test🠎.
proxy_set_header 主機$host; 

proxy_set_header X-真實IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

client_max_body_size 10m_xy 85; _timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k ; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; 



mod_proxy 的範例: 


irt;
 ServerAdmin admin@zxsv.com 

  #反向代理設定

ProxyPass / http://www.zxsv.com:8080/ 

  ProxyPassReverse / http://www.zxsv. com:8080/

# 快取目錄根根

/Rache

# 最大快取儲存 

CacheSize 50000000 

# 小時:每4 小時

CacheGcInterval 4 
# 小時:每4 小時

:CacheGcInterval 4 Max 707 月
#過期時間= (now – Last_modified) * CacheLastModifiedFactor

CacheLastModifiedFactor 0.1

預設過期標籤:小時

CacheDefaultExpire 1
#ache 檢索內容百分比後強製完成 10% CustomLog /usr/local /apache/logs/dev_access_log 組合


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
優化PHP代碼:減少內存使用和執行時間優化PHP代碼:減少內存使用和執行時間May 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP電子郵件:分步發送指南PHP電子郵件:分步發送指南May 09, 2025 am 12:14 AM

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自動化intifications andMarketingCampaigns.1)設置設置yourphpenvenvironnvironnvironmentwithaweberswithawebserverserververandphp,確保themailfunctionisenabled.2)useabasicscruct

如何通過PHP發送電子郵件:示例和代碼如何通過PHP發送電子郵件:示例和代碼May 09, 2025 am 12:13 AM

發送電子郵件的最佳方法是使用PHPMailer庫。 1)使用mail()函數簡單但不可靠,可能導致郵件進入垃圾郵件或無法送達。 2)PHPMailer提供更好的控制和可靠性,支持HTML郵件、附件和SMTP認證。 3)確保正確配置SMTP設置並使用加密(如STARTTLS或SSL/TLS)以增強安全性。 4)對於大量郵件,考慮使用郵件隊列系統來優化性能。

高級PHP電子郵件:自定義標題和功能高級PHP電子郵件:自定義標題和功能May 09, 2025 am 12:13 AM

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP發送電子郵件的指南使用PHP和SMTP發送電子郵件的指南May 09, 2025 am 12:06 AM

使用PHP和SMTP發送郵件可以通過PHPMailer庫實現。 1)安裝並配置PHPMailer,2)設置SMTP服務器細節,3)定義郵件內容,4)發送郵件並處理錯誤。使用此方法可以確保郵件的可靠性和安全性。

使用PHP發送電子郵件的最佳方法是什麼?使用PHP發送電子郵件的最佳方法是什麼?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依賴注入的最佳實踐PHP中依賴注入的最佳實踐May 08, 2025 am 12:21 AM

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

PHP性能調整技巧和技巧PHP性能調整技巧和技巧May 08, 2025 am 12:20 AM

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器