PHP企業級應用之常見緩衝技術篇

來源:互聯網
上載者:User

普遍緩衝技術
資料緩衝:這裡所說的資料緩衝是指資料庫查詢快取,每次訪問頁面的時候,都會先檢測相應的快取資料是否存在,如果不存在,就串連資料庫,得到資料, 並把查詢結果序列化後儲存到檔案中,以後同樣的查詢結果就直接從緩衝表或檔案中獲得。
用的最廣的例子看Discuz的搜尋功能,把結果ID緩衝到一個表中,下次搜尋相同關鍵字時先搜尋緩衝表。
舉個常用的方法,多表關聯的時候,把附表中的內容產生數組儲存到主表的一個欄位中,需要的時候數組分解一下,這樣的好處是唯讀一個表,壞處就是兩個 資料同步會多不少步驟,資料庫永遠是瓶頸,用硬碟換速度,是這個的關鍵點。
頁面緩衝:
每次訪問頁面的時候,都會先檢測相應的快取頁面面檔案是否存在,如果不存在,就串連資料庫,得到資料,顯示頁面並同時產生快取頁面面檔案,這樣下次訪問 的時候分頁檔就發揮作用了。(模板引擎和網上常見的一些緩衝類通常有此功能)
時間觸發緩衝:
檢查檔案是否存在並且時間戳記小於設定的到期時間,如果檔案修改的時間戳記比目前時間戳減去到期時間戳記大,那麼就用緩衝,否則更新緩衝。
內容觸發緩衝:
當插入資料或更新資料時,強制更新緩衝。
靜態緩衝:
這裡所說的靜態緩衝是指靜態化,直接產生HTML或xml等文字檔,有更新的時候重產生一次,適合於不太變化的頁面,這就不說了。
以上內容是代碼級的解決方案,我直接CP別的架構,也懶得改,內容都差不多,很容易就做到,而且會幾種方式一起用,但下面的內容是伺服器端的緩衝方 案,非代碼級的,要有多方的合作才能做到
記憶體緩衝:
Memcached是高效能的,分布式的記憶體對象緩衝系統,用於在Live App中減少資料庫負載,提升訪問速度。
這裡說下Memcached的例子: 複製代碼 代碼如下:<?php
$memcache = new Memcache;
$memcache->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);
?>

讀庫的例子: 複製代碼 代碼如下:<?php
$sql = ‘SELECT * FROM users';
$key = md5($sql); //memcached 物件識別碼
if ( !($datas = $mc->get($key)) ) {
// 在 memcached 中未擷取到快取資料,則使用資料庫查詢擷取記錄集。
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;
// 將資料庫中擷取到的結果集資料儲存到 memcached 中,以供下次訪問時使用。
$mc->add($key, $datas);
} else {
echo “n”.str_pad(‘Read datas from memcached.', 60, ‘_').”n”;
}
var_dump($datas);
?>

php的緩衝器:
有eaccelerator, apc, phpa,xcache,這個這個就不說了吧,搜尋一堆一堆的,自己看啦,知道有這玩意就OK
MYSQL緩衝:
這也算非代碼級的,經典的資料庫就是用的這種方式,看下面的已耗用時間,0.09xxx之類的
我貼段根據藍色那傢伙修改後部分my.ini 吧,2G的MYISAM表可以在0.05S左右,據說他前後改了有快一年
[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
基於反向 Proxy的Web緩衝:
如Nginx,SQUID,mod_PRoxy(apache2以上又分為mod_proxy和mod_cache)
NGINX的例子 複製代碼 代碼如下:<nginx.conf>
#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;
}
}
<conf/proxy_setting.conf>
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的例子:
<VirtualHost *>
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
</VirtualHost>

而SQUID的例子我就不說明了,這方面網上有寫的太多,大家自己搜尋一下
DNS輪詢:
BIND是一款開放源碼的DNS伺服器軟體,這個要說起來就大了,自己搜尋去,大家知道有這個東西就行了。
我知道的有chinacache 等大站就是這樣做的,說簡單點就是多伺服器啦,把同一個頁面或檔案快取到不同的伺服器上,按南北自動解析到相關的伺服器中。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.