首頁  >  文章  >  後端開發  >  PHP企業級應用常見快取技術深入解讀_PHP教程

PHP企業級應用常見快取技術深入解讀_PHP教程

WBOY
WBOY原創
2016-07-22 09:02:15745瀏覽

普遍快取技術

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

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

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

頁面快取:

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

時間觸發快取:

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

內容觸發快取:

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

靜態快取:

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

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

記憶體快取:

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

這裡說下Memcached的例子:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="vars">$memcache</span><span> = </span><span class="keyword">new</span><span> Memcache;  </span></li><li class="alt"><span class="vars">$memcache</span><span>->connect(‘localhost’, 11211) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span> (“Could not connect”);  </span></span></li>
<li>
<span class="vars">$version</span><span> = </span><span class="vars">$memcache</span><span>->getVersion();  </span>
</li>
<li class="alt">
<span class="func">echo</span><span> “Server’s version: “.</span><span class="vars">$version</span><span>.”n”;  </span>
</li>
<li>
<span class="vars">$tmp_object</span><span> = </span><span class="keyword">new</span><span> stdClass;  </span>
</li>
<li class="alt">
<span class="vars">$tmp_object</span><span>->str_attr = ‘test’;  </span>
</li>
<li>
<span class="vars">$tmp_object</span><span>->int_attr = 123;  </span>
</li>
<li class="alt">
<span class="vars">$memcache</span><span>->set(‘key’, </span><span class="vars">$tmp_object</span><span>, false, 10) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span> (“Failed to save data at the server”);  </span>
</li>
<li>
<span class="func">echo</span><span> “Store data in the cache (data will expire in 10 seconds)n”;  </span>
</li>
<li class="alt">
<span class="vars">$get_result</span><span> = </span><span class="vars">$memcache</span><span>->get(‘key’);  </span>
</li>
<li>
<span class="func">echo</span><span> “Data from the cache:n”;  </span>
</li>
<li class="alt">
<span>var_dump(</span><span class="vars">$get_result</span><span>);  </span>
</li>
<li><span>?> </span></li>
</ol>

讀庫的例子:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="vars">$sql</span><span> = &lsquo;SELECT * FROM users&rsquo;;  </span></li><li class="alt"><span class="vars">$key</span><span> = md5(</span><span class="vars">$sql</span><span>);   </span><span class="comment">//memcached 对象标识符 </span><span> </span></li><li><span class="keyword">if</span><span> ( !(</span><span class="vars">$datas</span><span> = </span><span class="vars">$mc</span><span>->get(</span><span class="vars">$key</span><span>)) ) {  </span></span></li>
<li class="alt">
<span class="comment">//  在 memcached 中未获取到缓存数据,则使用数据库查询获取记录集。 </span><span> </span>
</li>
<li>
<span class="func">echo</span><span> “n”.</span><span class="func">str_pad</span><span>(‘Read datas from MySQL.’, 60, ‘_’).”n”;  </span>
</li>
<li class="alt">
<span class="vars">$conn</span><span> = mysql_connect(‘localhost’, ‘test’, ‘test’);  </span>
</li>
<li><span>mysql_select_db(‘test’);  </span></li>
<li class="alt">
<span class="vars">$result</span><span> = mysql_query(</span><span class="vars">$sql</span><span>);  </span>
</li>
<li>
<span class="keyword">while</span><span> (</span><span class="vars">$row</span><span> = mysql_fetch_object(</span><span class="vars">$result</span><span>))  </span>
</li>
<li class="alt">
<span class="vars">$datas</span><span>[] = </span><span class="vars">$row</span><span>;  </span>
</li>
<li>
<span class="comment">//  将数据库中获取到的结果集数据保存到 memcached 中,以供下次访问时使用。 </span><span> </span>
</li>
<li class="alt">
<span class="vars">$mc</span><span>->add(</span><span class="vars">$key</span><span>, </span><span class="vars">$datas</span><span>);  </span>
</li>
<li>
<span>} </span><span class="keyword">else</span><span> {  </span>
</li>
<li class="alt">
<span class="func">echo</span><span> “n”.</span><span class="func">str_pad</span><span>(‘Read datas from memcached.’, 60, ‘_’).”n”;  </span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span>var_dump(</span><span class="vars">$datas</span><span>);  </span>
</li>
<li><span>?> </span></li>
</ol>

PHP的緩衝器:

有eaccelerator, apc, PHPa,xcache,這個這個就不說了吧,搜尋一堆一堆的,自己看啦,知道有這玩意就OK

MYSQL快取:

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

我貼段根據藍色那傢伙修改後部分my.ini 吧,2G的MYISAM表可以在0.05S左右,據說他前後改了有快一年

<ol class="dp-c">
<li class="alt"><span><span>[client]  </span></span></li>
<li><span>……  </span></li>
<li class="alt">
<span class="keyword">default</span><span>-character-set=gbk  </span>
</li>
<li>
<span class="keyword">default</span><span>-storage-engine=MYISAM  </span>
</li>
<li class="alt"><span>max_connections=600  </span></li>
<li><span>max_connect_errors=500  </span></li>
<li class="alt"><span>back_log=200  </span></li>
<li><span>interactive_timeout=7200  </span></li>
<li class="alt"><span>query_cache_size=64M  </span></li>
<li><span>……  </span></li>
<li class="alt"><span>table_cache=512  </span></li>
<li><span>……  </span></li>
<li class="alt"><span>myisam_max_sort_file_size=100G  </span></li>
<li><span>myisam_max_extra_sort_file_size=100G  </span></li>
<li class="alt"><span>myisam_sort_buffer_size=128M  </span></li>
<li><span>key_buffer_size=1024M  </span></li>
<li class="alt"><span>read_buffer_size=512M  </span></li>
<li><span>……  </span></li>
<li class="alt"><span>thread_concurrency=8 </span></li>
</ol>

基於反向代理的Web快取:

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

NGINX的例子

<ol class="dp-c">
<li class="alt"><span><span><nginx.conf>  </span></span></li>
<li><span>#user  nobody;  </span></li>
<li class="alt"><span>worker_processes  4;  </span></li>
<li>
<span class="func">error_log</span><span>  logs/error.log crit;  </span>
</li>
<li class="alt"><span>pid        logs/nginx.pid;  </span></li>
<li><span>worker_rlimit_nofile 10240;  </span></li>
<li class="alt"><span>events {  </span></li>
<li>
<span class="keyword">use</span><span> epoll;  </span>
</li>
<li class="alt"><span>worker_connections  51200;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>http {  </span></li>
<li>
<span class="keyword">include</span><span>       mime.types;  </span>
</li>
<li class="alt"><span>default_type  application/octet-stream;  </span></li>
<li><span>sendfile    on;  </span></li>
<li class="alt"><span>keepalive_timeout 65;  </span></li>
<li><span>tcp_nodelay on;  </span></li>
<li class="alt"><span># server pool  </span></li>
<li><span>upstream bspfrontsvr {  </span></li>
<li class="alt"><span>server 10.10.10.224:80   weight=1;  </span></li>
<li><span>server 10.10.10.221:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspimgsvr {  </span></li>
<li><span>server 10.10.10.201:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspstylesvr {  </span></li>
<li><span>server 10.10.10.202:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bsphelpsvr {  </span></li>
<li><span>server 10.10.10.204:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspwsisvr {  </span></li>
<li><span>server 10.10.10.203:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspadminsvr {  </span></li>
<li><span>server 10.10.10.222:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspbuyersvr {  </span></li>
<li><span>server 10.10.10.223:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>upstream bspsellersvr {  </span></li>
<li><span>server 10.10.10.225:80   weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>upstream  bsploginsvr  {  </span></li>
<li class="alt"><span>server 10.10.10.220:443  weight=1;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>upstream  bspregistersvr  {  </span></li>
<li><span>server 10.10.10.220:80  weight=1;  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span>log_format  test_com  ‘</span><span class="vars">$remote_addr</span><span> – </span><span class="vars">$remote_user</span><span> [</span><span class="vars">$time_local</span><span>] “</span><span class="vars">$request</span><span>” ‘  </span>
</li>
<li class="alt">
<span>‘</span><span class="vars">$status</span><span> </span><span class="vars">$body_bytes_sent</span><span> “</span><span class="vars">$http_referer</span><span>” “</span><span class="vars">$http_user_agent</span><span>” ‘;  </span>
</li>
<li><span>#——————————————————————–  </span></li>
<li class="alt"><span>#img.test.com  </span></li>
<li><span>server {  </span></li>
<li class="alt"><span>listen       10.10.10.230:80;  </span></li>
<li><span>server_name  img.test.com;  </span></li>
<li class="alt"><span>location / {  </span></li>
<li>
<span>proxy_pass      http:</span><span class="comment">//bspimgsvr; </span><span> </span>
</li>
<li class="alt">
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>access_log  logs/img.log  test_com;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li><span>#style.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:80;  </span></li>
<li class="alt"><span>server_name  style.test.com;  </span></li>
<li><span>location / {  </span></li>
<li class="alt">
<span>proxy_pass      http:</span><span class="comment">//bspstylesvr; </span><span> </span>
</li>
<li>
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>access_log  logs/style.log  test_com;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>#help.test.com  </span></li>
<li><span>server {  </span></li>
<li class="alt"><span>listen       10.10.10.230:80;  </span></li>
<li><span>server_name  help.test.com;  </span></li>
<li class="alt"><span>location / {  </span></li>
<li>
<span>proxy_pass      http:</span><span class="comment">//bsphelpsvr; </span><span> </span>
</li>
<li class="alt">
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>access_log  logs/help.log  test_com;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li><span>#admin.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:80;  </span></li>
<li class="alt"><span>server_name  admin.test.com;  </span></li>
<li><span>location / {  </span></li>
<li class="alt">
<span>proxy_pass      http:</span><span class="comment">//bspadminsvr; </span><span> </span>
</li>
<li>
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>access_log  logs/admin.log  test_com;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span> </span></li>
<li class="alt"><span>#buyer.test.com  </span></li>
<li><span>server {  </span></li>
<li class="alt"><span>listen       10.10.10.230:80;  </span></li>
<li><span>server_name  buyer.test.com;  </span></li>
<li class="alt"><span>location / {  </span></li>
<li>
<span>proxy_pass      http:</span><span class="comment">//bspbuyersvr; </span><span> </span>
</li>
<li class="alt">
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>access_log  logs/buyer.log  test_com;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li><span>#seller.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:80;  </span></li>
<li class="alt"><span>server_name  seller.test.com;  </span></li>
<li><span>location / {  </span></li>
<li class="alt">
<span>proxy_pass      http:</span><span class="comment">//bspsellersvr; </span><span> </span>
</li>
<li>
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>access_log  logs/seller.log  test_com;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>#wsi.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:80;  </span></li>
<li class="alt"><span>server_name  wsi.test.com;  </span></li>
<li><span>location / {  </span></li>
<li class="alt">
<span>proxy_pass      http:</span><span class="comment">//bspwsisvr; </span><span> </span>
</li>
<li>
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>access_log  logs/wsi.log  test_com;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>#www.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:80;  </span></li>
<li class="alt"><span>server_name  www.test.com   *.test.com;  </span></li>
<li><span>location ~ ^/NginxStatus/ {  </span></li>
<li class="alt"><span>stub_status on;  </span></li>
<li><span>access_log off;  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>location / {  </span></li>
<li class="alt">
<span>proxy_pass      http:</span><span class="comment">//bspfrontsvr; </span><span> </span>
</li>
<li>
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>access_log  logs/www.log  test_com;  </span></li>
<li class="alt"><span>error_page   500 502 503 504  /50x.html;  </span></li>
<li><span>location = /50x.html {  </span></li>
<li class="alt"><span>root   html;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>#login.test.com  </span></li>
<li class="alt"><span>server {  </span></li>
<li><span>listen       10.10.10.230:443;  </span></li>
<li class="alt"><span>server_name  login.test.com;  </span></li>
<li><span>ssl                  on;  </span></li>
<li class="alt"><span>ssl_certificate      cert.pem;  </span></li>
<li><span>ssl_certificate_key  cert.key;  </span></li>
<li class="alt"><span>ssl_session_timeout  5m;  </span></li>
<li><span>ssl_protocols  SSLv2 SSLv3 TLSv1;  </span></li>
<li class="alt">
<span>ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+</span><span class="func">EXP</span><span>;  </span>
</li>
<li><span>ssl_prefer_server_ciphers   on;  </span></li>
<li class="alt"><span>location / {  </span></li>
<li>
<span>proxy_pass        https:</span><span class="comment">//bsploginsvr; </span><span> </span>
</li>
<li class="alt">
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>access_log  logs/login.log  test_com;  </span></li>
<li><span>}  </span></li>
<li class="alt">
<span>#login.test.com </span><span class="keyword">for</span><span> register  </span>
</li>
<li><span>server {  </span></li>
<li class="alt"><span>listen       10.10.10.230:80;  </span></li>
<li><span>server_name  login.test.com;  </span></li>
<li class="alt"><span>location / {  </span></li>
<li>
<span>proxy_pass        http:</span><span class="comment">//bspregistersvr; </span><span> </span>
</li>
<li class="alt">
<span class="keyword">include</span><span>         proxy_setting.conf;  </span>
</li>
<li><span>}  </span></li>
<li class="alt"><span>access_log  logs/register.log  test_com;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span> </span></li>
<li><span>}  </span></li>
<li class="alt"><span><conf/proxy_setting.conf>  </span></li>
<li><span>proxy_redirect          off;  </span></li>
<li class="alt">
<span>proxy_set_header        Host </span><span class="vars">$host</span><span>;  </span>
</li>
<li>
<span>proxy_set_header        X-Real-IP </span><span class="vars">$remote_addr</span><span>;  </span>
</li>
<li class="alt">
<span>proxy_set_header        X-Forwarded-For </span><span class="vars">$proxy_add_x_forwarded_for</span><span>;  </span>
</li>
<li><span>client_max_body_size    10m;  </span></li>
<li class="alt"><span>client_body_buffer_size 128k;  </span></li>
<li><span>proxy_connect_timeout   90;  </span></li>
<li class="alt"><span>proxy_send_timeout      90;  </span></li>
<li><span>proxy_read_timeout      90;  </span></li>
<li class="alt"><span>proxy_buffer_size       4k;  </span></li>
<li><span>proxy_buffers           4 32k;  </span></li>
<li class="alt"><span>proxy_busy_buffers_size 64k;  </span></li>
<li><span>proxy_temp_file_write_size 64k;  </span></li>
<li class="alt"><span>mod_proxy的例子:  </span></li>
<li><span> </span></li>
<li class="alt"><span><VirtualHost *>  </span></li>
<li><span>ServerName www.zxsv.com  </span></li>
<li class="alt"><span>ServerAdmin admin@zxsv.com  </span></li>
<li><span># reverse proxy setting  </span></li>
<li class="alt">
<span>ProxyPass / http:</span><span class="comment">//www.zxsv.com:8080/ </span><span> </span>
</li>
<li>
<span>ProxyPassReverse / http:</span><span class="comment">//www.zxsv.com:8080/ </span><span> </span>
</li>
<li class="alt"><span># cache dir root  </span></li>
<li>
<span>CacheRoot “/</span><span class="keyword">var</span><span>/www/proxy”  </span>
</li>
<li class="alt"><span># max cache storage  </span></li>
<li><span>CacheSize 50000000  </span></li>
<li class="alt"><span># hour: every 4 hour  </span></li>
<li><span>CacheGcInterval 4  </span></li>
<li class="alt"><span># max page expire time: hour  </span></li>
<li><span>CacheMaxExpire 240  </span></li>
<li class="alt"><span># Expire time = (now – last_modified) * CacheLastModifiedFactor  </span></li>
<li><span>CacheLastModifiedFactor 0.1  </span></li>
<li class="alt"><span># defalt expire tag: hour  </span></li>
<li><span>CacheDefaultExpire 1  </span></li>
<li class="alt"><span># force complete after precent of content retrived: 60-90%  </span></li>
<li><span>CacheForceCompletion 80  </span></li>
<li class="alt"><span>CustomLog /usr/local/apache/logs/dev_access_log combined  </span></li>
<li><span></VirtualHost> </span></li>
</ol>
而SQUID的例子我就不說明了,這方面網上有寫的太多,大家自己搜尋一下

DNS輪詢:

BIND是一款開放原始碼的DNS伺服器軟體,這個要說起來就大了,自己搜尋去,大家知道有這個東西就行了。

我知道的有chinacache 等大站就是這樣做的,說簡單點就是多伺服器啦,把同一個頁面或檔案快取到不同的伺服器上,按南北自動解析到相關的伺服器中。

http://www.cnblogs.com/cnblogs20112011/archive/2011/01/27/1946286.html


http://www.bkjia.com/PHPjc/445846.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445846.htmlTechArticle普遍快取技術資料快取:這裡所說的資料快取是指資料庫查詢緩存,每次造訪頁面的時候,都會先偵測對應的快取資料是否存在,如果不存...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn