以前に、主にデータ キャッシュについて言及しながら、PHP キャッシュ テクノロジについて詳しく説明しました。データ キャッシュは主にデータベース クエリ キャッシュを指します。ページにアクセスするたびに、まず対応するキャッシュ データが存在するかどうかを検出し、存在しない場合はデータベースに接続してデータを取得し、クエリ結果をシリアル化して保存します。同じクエリ結果がキャッシュ テーブルまたはファイルから直接取得されます。
最も広く使用されている例は、結果 ID をテーブルにキャッシュし、次回同じキーワードを検索するときに最初にキャッシュ テーブルを検索する Discuz の検索機能です。
一般的な方法として、複数のテーブルを関連付ける場合、アタッチされたテーブルの内容を配列に生成し、必要に応じて配列を分解することができます。ただし、欠点は、2 つのデータの同期にはさらに多くの手順が必要であり、速度のためにハードディスクを犠牲にすることが常にボトルネックになることです。
ページのキャッシュ
ページにアクセスされるたびに、まず対応するキャッシュされたページ ファイルが存在するかどうかを検出し、存在しない場合はデータベースに接続してデータを取得し、ページを表示すると同時にキャッシュされたページ ファイルを生成します。これにより、次回アクセスしたときにページ ファイルが有効になります。 (インターネット上のテンプレート エンジンと一部の一般的なキャッシュ クラスには通常、この機能があります)
時間トリガーキャッシュ
ファイルが存在し、タイムスタンプが設定された有効期限よりも小さいかどうかを確認します。ファイル変更タイムスタンプが現在のタイムスタンプから有効期限タイムスタンプを引いた値より大きい場合は、キャッシュを使用し、それ以外の場合はキャッシュを更新します。
コンテンツトリガーキャッシュ
データが挿入または更新されると、キャッシュは強制的に更新されます。
静的キャッシュ
ここでいう静的キャッシュとは、HTMLやXMLなどのテキストファイルを静的に直接生成し、更新があったときに再生成するものを指しますので、あまり変更のないページに適していますので、ここでは説明しません。
メモリキャッシュ
Memcached は、データベースの負荷を軽減し、動的アプリケーションのアクセス速度を向上させるために使用される、高性能の分散メモリ オブジェクト キャッシュ システムです。
$memcache = 新しい Memcache;
$memcache->connect(‘localhost’, 11211) or die (“接続できませんでした”);
$version = $memcache->getVersion();
echo "サーバーのバージョン: ".$version."n";
$tmp_object = new stdClass;
$tmp_object->str_attr = ‘テスト’;
$tmp_object->int_attr = 123;
$memcache->set(‘key’, $tmp_object, false, 10) or die (“サーバーへのデータの保存に失敗しました”);
echo "データをキャッシュに保存します (データは 10 秒で期限切れになります)n";
$get_result = $memcache->get(‘key’);
echo “キャッシュからのデータ:n”;
var_dump($get_result);
?>
ライブラリの読み取り例:
$sql = ‘SELECT * FROM users’;
$key = md5($sql) //memcached オブジェクト識別子
if ( !($datas = $mc->get($key)) ) {
// memcached でキャッシュされたデータが取得できない場合は、データベース クエリを使用してレコード セットを取得します
echo “n”.str_pad(‘MySQL からデータを読み取ります。’, 60, ‘_’).”n”;
$conn = mysql_connect(‘localhost’, ‘test’, ‘test’);
mysql_select_db(‘テスト’);
$result = mysql_query($sql);
while ($row = mysql_fetch_object($result))
$datas[] = $row;
// 次回の訪問時に使用できるよう、データベースから取得した結果セット データを memcached に保存します
$mc->add($key, $datas);
} else {
echo “n”.str_pad(‘memcached からデータを読み取ります。’, 60, ‘_’).”n”;
}
var_dump($datas);
?>
PHP バッファ
たとえば、eaccelerator、apc、phpa、xcache などです。
MySQL キャッシュ
これは、非コード レベルともみなされます。クラシック データベースでは、この方法が使用されます。以下の実行時間を見ると、0.09xxx のようです。
[クライアント]
……
デフォルト文字セット=gbk
デフォルトストレージエンジン=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 の例:
#userEveryone;
worker_processes 4;
error_log logs/error.log crit;
pid logs/nginx.pid;
worker_rlimit_nofile 10240;
イベント {
epoll を使用します;
worker_connections 51200;
}
http {
mime.types を含める;
default_type application/octet-stream;
sendfile オン;
keepalive_timeout 65;
tcp_nodelay オン;
# サーバープール
アップストリーム bspfrontsvr {
サーバー 10.10.10.224:80weight=1;
サーバー 10.10.10.221:80 重み = 1;
}
アップストリーム bspimgsvr {
サーバー 10.10.10.201:80 重み = 1;
}
アップストリーム bspstylesvr {
サーバー 10.10.10.202:80 重み = 1;
}
アップストリーム bsphelpsvr {
サーバー 10.10.10.204:80weight=1;
}
アップストリーム bspwsisvr {
サーバー 10.10.10.203:80weight=1;
}
アップストリーム bspadminsvr {
サーバー 10.10.10.222:80weight=1;
}
アップストリーム bspbuyersvr {
サーバー 10.10.10.223:80weight=1;
}
アップストリーム bspsellersvr {
サーバー 10.10.10.225:80weight=1;
}
アップストリーム bsploginsvr {
サーバー 10.10.10.220:443 Weight=1;
}
アップストリーム bspregistersvr {
サーバー 10.10.10.220:80weight=1;
}
log_format test_com ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” “$http_user_agent” ‘;
#——————————————————————–
#img.test.com
サーバー {
10.10.10.230:80を聞いてください;
server_name img.test.com;
場所 / {
proxy_pass http://bspimgsvr;
proxy_setting.conf を含める;
}
access_log logs/img.log test_com;
}
#style.test.com
サーバー {
10.10.10.230:80;
を聞いてくださいserver_name style.test.com;
場所 / {
proxy_pass http://bspstylesvr;
proxy_setting.conf を含める;
}
access_log logs/style.log test_com;
}
#help.test.com
サーバー {
10.10.10.230:80を聞いてください;
server_name help.test.com;
場所 / {
proxy_pass http://bsphelpsvr;
proxy_setting.conf を含める;
}
access_log logs/help.log test_com;
}
#admin.test.com
サーバー {
10.10.10.230:80を聞いてください;
server_name admin.test.com;
場所 / {
proxy_pass http://bspadminsvr;
proxy_setting.conf を含める;
}
access_log logs/admin.log test_com;
}
#buyer.test.com
サーバー {
10.10.10.230:80を聞いてください;
server_name buyer.test.com;
場所 / {
proxy_pass http://bspbuyersvr;
proxy_setting.conf を含める;
}
access_log logs/buyer.log test_com;
}
#seller.test.com
サーバー {
10.10.10.230:80を聞いてください;
server_name sell.test.com;
場所 / {
proxy_pass http://bspsellersvr;
proxy_setting.conf を含める;
}
access_log logs/seller.log test_com;
}
#wsi.test.com
サーバー {
10.10.10.230:80を聞いてください;
server_name wsi.test.com;
場所 / {
proxy_pass http://bspwsisvr;
proxy_setting.conf を含める;
}
access_log logs/wsi.log test_com;
}
#www.test.com
サーバー {
10.10.10.230:80を聞いてください;
server_name www.test.com *.test.com;
location ~ ^/NginxStatus/ {
stub_status on;
access_log off;
}
場所 / {
proxy_pass http://bspfrontsvr;
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
サーバー {
10.10.10.230:443;
を聞いてくださいserver_namelogin.test.com;
SSL オン;
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 オン;
場所 / {
proxy_pass https://bsploginsvr;
proxy_setting.conf を含める;
}
access_log logs/login.log test_com;
}
登録用#login.test.com
サーバー {
10.10.10.230:80を聞いてください;
server_namelogin.test.com;
場所 / {
proxy_pass http://bspregistersvr;
proxy_setting.conf を含める;
}
access_log logs/register.log test_com;
}
}
proxy_redirect オフ;
proxy_set_header ホスト $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の例:
サーバー名 www.zxsv.com
ServerAdmin admin@zxsv.com
#リバースプロキシ設定
ProxyPass / http://www.zxsv.com:8080/
ProxyPassReverse / http://www.zxsv.com:8080/
# キャッシュディレクトリルート
CacheRoot “/var/www/proxy”
# 最大キャッシュストレージ
キャッシュサイズ 50000000
#時: 4時間ごと
CacheGcInterval 4
# ページの最大有効期限: 時間
CacheMaxExpire 240
# 有効期限 = (now – last_modified) * CacheLastModifiedFactor
CacheLastModifiedFactor 0.1
# デフォルトの有効期限タグ: 時間
CacheDefaultExpire 1
# コンテンツの割合が取得された後に強制的に完了: 60-90%
CacheForceCompletion 80
CustomLog /usr/local/apache/logs/dev_access_log を組み合わせた