PHP企业级应用之常见缓存技术篇


Posted in PHP onJanuary 27, 2011

普遍缓存技术
数据缓存:这里所说的数据缓存是指数据库查询缓存,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据, 并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓存表或文件中获得。
用的最广的例子看Discuz的搜索功能,把结果ID缓存到一个表中,下次搜索相同关键字时先搜索缓存表。
举个常用的方法,多表关联的时候,把附表中的内容生成数组保存到主表的一个字段中,需要的时候数组分解一下,这样的好处是只读一个表,坏处就是两个 数据同步会多不少步骤,数据库永远是瓶颈,用硬盘换速度,是这个的关键点。
页面缓存:
每次访问页面的时候,都会先检测相应的缓存页面文件是否存在,如果不存在,就连接数据库,得到数据,显示页面并同时生成缓存页面文件,这样下次访问 的时候页面文件就发挥作用了。(模板引擎和网上常见的一些缓存类通常有此功能)
时间触发缓存:
检查文件是否存在并且时间戳小于设置的过期时间,如果文件修改的时间戳比当前时间戳减去过期时间戳大,那么就用缓存,否则更新缓存。
内容触发缓存:
当插入数据或更新数据时,强制更新缓存。
静态缓存:
这里所说的静态缓存是指静态化,直接生成HTML或xml等文本文件,有更新的时候重生成一次,适合于不太变化的页面,这就不说了。
以上内容是代码级的解决方案,我直接CP别的框架,也懒得改,内容都差不多,很容易就做到,而且会几种方式一起用,但下面的内容是服务器端的缓存方 案,非代码级的,要有多方的合作才能做到
内存缓存:
Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
这里说下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
基于反向代理的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 等大站就是这样做的,说简单点就是多服务器啦,把同一个页面或文件缓存到不同的服务器上,按南北自动解析到相关的服务器中。
PHP 相关文章推荐
PHP统计目录下的文件总数及代码行数(去除注释及空行)
Jan 17 PHP
php中全局变量global的使用演示代码
May 18 PHP
PHP禁止个别IP访问网站
Oct 30 PHP
php快递单号查询接口使用示例
May 05 PHP
php+ajax实现无刷新分页
Nov 18 PHP
在php中设置session用memcache来存储的方法总结
Jan 14 PHP
thinkphp修改配置进入默认首页的方法
Feb 07 PHP
对php 判断http还是https,以及获得当前url的方法详解
Jan 15 PHP
laravel通过a标签从视图向控制器实现传值
Oct 15 PHP
tp5框架的增删改查操作示例
Oct 31 PHP
php实现根据身份证获取精准年龄
Feb 26 PHP
如何用Laravel包含你自己的帮助函数
May 27 PHP
兼容性最强的PHP生成缩略图的函数代码(修改版)
Jan 18 #PHP
discuz的php防止sql注入函数
Jan 17 #PHP
PHP统计目录下的文件总数及代码行数(去除注释及空行)
Jan 17 #PHP
php短域名转换为实际域名函数
Jan 17 #PHP
PHP学习笔记之三 数据库基本操作
Jan 17 #PHP
PHP学习笔记之二
Jan 17 #PHP
PHP学习笔记之一
Jan 17 #PHP
You might like
php中global和$GLOBALS[]的分析之一
2012/02/02 PHP
phpphp图片采集后按原路径保存图片示例
2014/02/18 PHP
PHP中iconv函数转码时截断字符问题的解决方法
2015/01/21 PHP
PHP ajax 异步执行不等待执行结果的处理方法
2015/05/27 PHP
PHP不使用内置函数实现字符串转整型的方法示例
2017/07/03 PHP
php适配器模式简单应用示例
2019/10/23 PHP
escape、encodeURI 和 encodeURIComponent 的区别
2009/03/02 Javascript
JS实现在Repeater控件中创建可隐藏区域的代码
2010/09/16 Javascript
JavaScript控制网页层收起和展开效果的方法
2015/04/15 Javascript
JS实现的简洁纵向滑动菜单(滑动门)效果
2015/10/19 Javascript
正则表达式(语法篇推荐)
2016/06/24 Javascript
JavaScript关于提高网站性能的几点建议(一)
2016/07/24 Javascript
基于JavaScript实现添加到购物车效果附源码下载
2016/08/22 Javascript
微信小程序 教程之模块化
2016/10/17 Javascript
强大的 Angular 表单验证功能详细介绍
2017/05/23 Javascript
[js高手之路]原型式继承与寄生式继承详解
2017/08/28 Javascript
[02:38]DOTA2亚洲邀请赛 IG战队巡礼
2015/02/03 DOTA
使用python实现扫描端口示例
2014/03/29 Python
python中如何正确使用正则表达式的详细模式(Verbose mode expression)
2017/11/08 Python
python实现图像识别功能
2018/01/29 Python
python实现列表中由数值查到索引的方法
2018/06/27 Python
Python3基本输入与输出操作实例分析
2020/02/14 Python
Python中import导入不同目录的模块方法详解
2020/02/18 Python
Flask-SocketIO服务端安装及使用代码示例
2020/11/26 Python
DataList 能否分页,请问如何实现?
2015/05/03 面试题
vue项目实现分页效果
2021/03/24 Vue.js
公司委托书格式范文
2014/04/04 职场文书
小区文明倡议书
2014/05/16 职场文书
个人批评与自我批评发言稿
2014/09/28 职场文书
共青团员自我评价
2015/03/10 职场文书
《乌鸦喝水》教学反思
2016/02/19 职场文书
mysql批量新增和存储的方法实例
2021/04/07 MySQL
python如何读取.mtx文件
2021/04/22 Python
将图片保存到mysql数据库并展示在前端页面的实现代码
2021/05/02 MySQL
中国十大神话动漫电影排行榜 哪吒登顶 白蛇缘起排第七
2022/03/21 国漫
vue2的 router在使用过程中遇到的一些问题
2022/04/13 Vue.js