本篇內(nèi)容主要講解“PHP緩存技術(shù)的實(shí)例應(yīng)用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“PHP緩存技術(shù)的實(shí)例應(yīng)用”吧!
創(chuàng)新互聯(lián)建站專業(yè)IDC數(shù)據(jù)服務(wù)器托管提供商,專業(yè)提供成都服務(wù)器托管,服務(wù)器租用,服務(wù)器托管機(jī)柜,服務(wù)器托管機(jī)柜,成都多線服務(wù)器托管等服務(wù)器托管服務(wù)。
之前我們曾深入的探討過PHP緩存技術(shù),其中主要提到了數(shù)據(jù)緩存。數(shù)據(jù)緩存主要是指數(shù)據(jù)庫查詢緩存,每次訪問頁面的時(shí)候,都會先檢測相應(yīng)的緩存數(shù)據(jù)是否存在,如果不存在,就連接數(shù)據(jù)庫,得到數(shù)據(jù), 并把查詢結(jié)果序列化后保存到文件中,以后同樣的查詢結(jié)果就直接從緩存表或文件中獲得。
用的最廣的例子看Discuz的搜索功能,把結(jié)果ID緩存到一個(gè)表中,下次搜索相同關(guān)鍵字時(shí)先搜索緩存表。
舉個(gè)常用的方法,多表關(guān)聯(lián)的時(shí)候,把附表中的內(nèi)容生成數(shù)組保存到主表的一個(gè)字段中,需要的時(shí)候數(shù)組分解一下,這樣的好處是只讀一個(gè)表,壞處就是兩個(gè) 數(shù)據(jù)同步會多不少步驟,數(shù)據(jù)庫永遠(yuǎn)是瓶頸,用硬盤換速度,是這個(gè)的關(guān)鍵點(diǎn)。
頁面緩存
每次訪問頁面的時(shí)候,都會先檢測相應(yīng)的緩存頁面文件是否存在,如果不存在,就連接數(shù)據(jù)庫,得到數(shù)據(jù),顯示頁面并同時(shí)生成緩存頁面文件,這樣下次訪問 的時(shí)候頁面文件就發(fā)揮作用了。(模板引擎和網(wǎng)上常見的一些緩存類通常有此功能)
時(shí)間觸發(fā)緩存
檢查文件是否存在并且時(shí)間戳小于設(shè)置的過期時(shí)間,如果文件修改的時(shí)間戳比當(dāng)前時(shí)間戳減去過期時(shí)間戳大,那么就用緩存,否則更新緩存。
內(nèi)容觸發(fā)緩存
當(dāng)插入數(shù)據(jù)或更新數(shù)據(jù)時(shí),強(qiáng)制更新緩存。
靜態(tài)緩存
這里所說的靜態(tài)緩存是指靜態(tài)化,直接生成HTML或xml等文本文件,有更新的時(shí)候重生成一次,適合于不太變化的頁面,這就不說了。
內(nèi)存緩存
Memcached是高性能的,分布式的內(nèi)存對象緩存系統(tǒng),用于在動態(tài)應(yīng)用中減少數(shù)據(jù)庫負(fù)載,提升訪問速度。
connect(‘localhost’, 11211) or die (“Could not connect”); $version = $memcache->getVersion(); echo “Server’s version: “.$version.”\n”; $tmp_object = new stdClass; $tmp_object->str_attr = ‘test’; $tmp_object->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)\n”; $get_result = $memcache->get(‘key’); echo “Data from the cache:\n”; var_dump($get_result); ?>
讀庫的例子:
get($key)) ) { // 在 memcached 中未獲取到緩存數(shù)據(jù),則使用數(shù)據(jù)庫查詢獲取記錄集 echo “n”.str_pad(‘Read datas from MySQL.’, 60, ‘_’).”n”; $conn = mysql_connect(‘localhost’, ‘test’, ‘test’); mysql_select_db(‘test’); $result = mysql_query($sql); while ($row = mysql_fetch_object($result)) $datas[] = $row; // 將數(shù)據(jù)庫中獲取到的結(jié)果集數(shù)據(jù)保存到 memcached 中,以供下次訪問時(shí)使用 $mc->add($key, $datas); } else { echo “n”.str_pad(‘Read datas from memcached.’, 60, ‘_’).”n”; } var_dump($datas); ?>
PHP的緩沖器
比如eaccelerator,apc,phpa,xcache等等。
MySQL緩存
這也算非代碼級的,經(jīng)典的數(shù)據(jù)庫就是用的這種方式,看下面的運(yùn)行時(shí)間,0.09xxx之類的。
[client] …… default-character-set=gbk default-storage-engine=MYISAM max_connections=600 max_connect_errors=500 back_log=200 interactive_timeout=7200 query_cache_size=64M …… table_cache=512 …… myisam_max_sort_file_size=100G myisam_max_extra_sort_file_size=100G myisam_sort_buffer_size=128M key_buffer_size=1024M read_buffer_size=512M …… thread_concurrency=8
基于反向代理的Web緩存
如Nginx,SQUID,mod_PRoxy(apache2以上又分為mod_proxy和mod_cache)
NGINX的例子:
#user nobody; worker_processes 4; error_log logs/error.log crit; pid logs/nginx.pid; worker_rlimit_nofile 10240; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; tcp_nodelay on; # server pool upstream bspfrontsvr { server 10.10.10.224:80 weight=1; server 10.10.10.221:80 weight=1; } upstream bspimgsvr { server 10.10.10.201:80 weight=1; } upstream bspstylesvr { server 10.10.10.202:80 weight=1; } upstream bsphelpsvr { server 10.10.10.204:80 weight=1; } upstream bspwsisvr { server 10.10.10.203:80 weight=1; } upstream bspadminsvr { server 10.10.10.222:80 weight=1; } upstream bspbuyersvr { server 10.10.10.223:80 weight=1; } upstream bspsellersvr { server 10.10.10.225:80 weight=1; } upstream bsploginsvr { server 10.10.10.220:443 weight=1; } upstream bspregistersvr { server 10.10.10.220:80 weight=1; } log_format test_com ‘$remote_addr – $remote_user [$time_local] “$request” ‘ ‘$status $body_bytes_sent “$http_referer” “$http_user_agent” ‘; #——————————————————————– #img.test.com server { listen 10.10.10.230:80; server_name img.test.com; location / { proxy_pass http://bspimgsvr; include proxy_setting.conf; } access_log logs/img.log test_com; } #style.test.com server { listen 10.10.10.230:80; server_name style.test.com; location / { proxy_pass http://bspstylesvr; include proxy_setting.conf; } access_log logs/style.log test_com; } #help.test.com server { listen 10.10.10.230:80; server_name help.test.com; location / { proxy_pass http://bsphelpsvr; include proxy_setting.conf; } access_log logs/help.log test_com; } #admin.test.com server { listen 10.10.10.230:80; server_name admin.test.com; location / { proxy_pass http://bspadminsvr; include proxy_setting.conf; } access_log logs/admin.log test_com; } #buyer.test.com server { listen 10.10.10.230:80; server_name buyer.test.com; location / { proxy_pass http://bspbuyersvr; include proxy_setting.conf; } access_log logs/buyer.log test_com; }
#seller.test.com server { listen 10.10.10.230:80; server_name seller.test.com; location / { proxy_pass http://bspsellersvr; include proxy_setting.conf; } access_log logs/seller.log test_com; } #wsi.test.com server { listen 10.10.10.230:80; server_name wsi.test.com; location / { proxy_pass http://bspwsisvr; include proxy_setting.conf; } access_log logs/wsi.log test_com; } #www.test.com server { listen 10.10.10.230:80; server_name www.test.com *.test.com; location ~ ^/NginxStatus/ { stub_status on; access_log off; } location / { proxy_pass http://bspfrontsvr; include proxy_setting.conf; } access_log logs/www.log test_com; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #login.test.com server { listen 10.10.10.230:443; server_name login.test.com; ssl on; ssl_certificate cert.pem; ssl_certificate_key cert.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; location / { proxy_pass https://bsploginsvr; include proxy_setting.conf; } access_log logs/login.log test_com; } #login.test.com for register server { listen 10.10.10.230:80; server_name login.test.com; location / { proxy_pass http://bspregistersvr; include proxy_setting.conf; } access_log logs/register.log test_com; } }proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_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的例子:
ServerName www.zxsv.com ServerAdmin admin@zxsv.com # reverse proxy setting ProxyPass / http://www.zxsv.com:8080/ ProxyPassReverse / http://www.zxsv.com:8080/ # cache dir root CacheRoot “/var/www/proxy” # max cache storage CacheSize 50000000 # hour: every 4 hour CacheGcInterval 4 # max page expire time: hour CacheMaxExpire 240 # Expire time = (now – last_modified) * CacheLastModifiedFactor CacheLastModifiedFactor 0.1 # defalt expire tag: hour CacheDefaultExpire 1 # force complete after precent of content retrived: 60-90% CacheForceCompletion 80 CustomLog /usr/local/apache/logs/dev_access_log combined
到此,相信大家對“PHP緩存技術(shù)的實(shí)例應(yīng)用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!