범용 캐싱 기술
데이터 캐시: 여기서 언급하는 데이터 캐시는 데이터베이스 쿼리 캐시를 의미하며, 페이지에 액세스할 때마다 해당 캐시된 데이터가 감지됩니다. 존재하지 않는 경우 데이터베이스에 연결하여 데이터를 얻은 다음 쿼리 결과를 직렬화하여 파일에 저장합니다. 나중에 동일한 쿼리 결과를 캐시 테이블이나 파일에서 얻을 수 있습니다.
가장 널리 사용되는 예는 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> = ‘SELECT * FROM users’; </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가 있습니다. 이것저것 검색해서 직접 확인해 보세요.
MYSQL 캐시:
이것도 코드 수준이 아닌 것으로 간주됩니다. 클래식 데이터베이스에서는 이 방법을 사용합니다. 아래의 실행 시간은 0.09xxx 등입니다.
게시된 단락입니다. 파란색 옷을 입은 남자가 수정한 my.ini 부분을 기반으로 한 것입니다. 2G MYISAM 테이블은 거의 1년 동안 수정했다고 합니다
<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>
역방향 프록시 기반 웹 캐시:
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 서버 소프트웨어입니다. 직접 검색해 보면 누구나 그 존재를 알 수 있습니다.
차이나캐시 같은 일부 대형 웹사이트에서는 이렇게 하는 것으로 알고 있습니다. 간단히 말하면 동일한 페이지나 파일이 서로 다른 서버에 캐시되어 북쪽에 따라 해당 서버로 자동 파싱되는 것입니다. 그리고 남쪽.
http://www.cnblogs.com/cnblogs20112011/archive/2011/01/27/1946286.html