|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
讲了这么多,无非是想说:学习PHP不仅要掌握方法,更多的是付出汗水,我不希望看到中途放弃的人,相信自己,相信自己的选择,更要相信自己的能力,如果自己想放弃,暴力一点的话,就自己抽自己一个嘴巴。
之前咱们曾深切的切磋过PHP缓存手艺,个中次要提到了数据缓存。数据缓存次要是指数据库查询缓存,每次会见页面的时分,城市先检测响应的缓存数据是不是存在,假如不存在,就毗连数据库,失掉数据, 并把查询了局序列化后保留到文件中,今后一样的查询了局就直接从缓存表或文件中取得。
用的最广的例子看Discuz的搜刮功效,把了局ID缓存到一个表中,下次搜刮不异关头字时先搜刮缓存表。
举个经常使用的办法,多表联系关系的时分,把附表中的内容生成数组保留到主表的一个字段中,需求的时分数组分化一下,如许的优点是只读一个表,害处就是两个 数据同步会多很多步调,数据库永久是瓶颈,用硬盘换速度,是这个的关头点。
页面缓存
每次会见页面的时分,城市先检测响应的缓存页面文件是不是存在,假如不存在,就毗连数据库,失掉数据,显示页面并同时生成缓存页面文件,如许下次会见 的时分页面文件就发扬感化了。(模板引擎和网上罕见的一些缓存类凡是有此功效)
工夫触发缓存
反省文件是不是存在而且工夫戳小于设置的过时工夫,假如文件修正的工夫戳比以后工夫戳减去过时工夫戳大,那末就用缓存,不然更新缓存。
内容触发缓存
当拔出数据或更新数据时,强迫更新缓存。
静态缓存
这里所说的静态缓存是指静态化,直接生成HTML或xml等文本文件,有更新的时分更生成一次,合适于不太变更的页面,这就不说了。
内存缓存
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等等。
MySQL缓存
这也算非代码级的,经典的数据库就是用的这类体例,看上面的运转工夫,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的例子:
<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>
然后大吼:别人可以,我为什么就不可以?(是不是有点阎罗教练的味道,默默的确是电影看多了,抽嘴巴是会痛的,各位其实明白这个道理了就行了) |
|