• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2022-01-26) 15次浏览 已收录 0个评论

普遍缓存技术
数据缓存:这里所说的数据缓存是指数据库查询缓存,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据, 并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓存表或文件中获得。
用的最广的例子看Discuz的搜索功能,把结果ID缓存到一个表中,下次搜索相同关键字时先搜索缓存表。
举个常用的方法,多表关联的时候,把附表中的内容生成数组保存到主表的一个字段中,需要的时候数组分解一下,这样的好处是只读一个表,坏处就是两个 数据同步会多不少步骤,数据库永远是瓶颈,用硬盘换速度,是这个的关键点。
页面缓存:
每次访问页面的时候,都会先检测相应的缓存页面文件是否存在,如果不存在,就连接数据库,得到数据,显示页面并同时生成缓存页面文件,这样下次访问 的时候页面文件就发挥作用了。(模板引擎和网上常见的一些缓存类通常有此功能)
时间触发缓存:
检查文件是否存在并且时间戳小于设置的过期时间,如果文件修改的时间戳比当前时间戳减去过期时间戳大,那么就用缓存,否则更新缓存。
内容触发缓存:
当插入数据或更新数据时,强制更新缓存。
静态缓存:
这里所说的静态缓存是指静态化,直接生成HTML或xml等文本文件,有更新的时候重生成一次,适合于不太变化的页面,这就不说了。
以上内容是代码级的解决方案,我直接CP别的框架,也懒得改,内容都差不多,很容易就做到,而且会几种方式一起用,但下面的内容是服务器端的缓存方 案,非代码级的,要有多方的合作才能做到
内存缓存:
Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
这里说下Memcached的例子:

 <BR><?php <BR>$memcache = new Memcache; <BR>$memcache->connect(‘localhost', 11211) or die (“Could not connect”); <BR>$version = $memcache->getVersion(); <BR>echo “Server's version: “.$version.””; <BR>$tmp_object = new stdClass; <BR>$tmp_object->str_attr = ‘test'; <BR>$tmp_object->int_attr = 123; <BR>$memcache->set(‘key', $tmp_object, false, 10) or die (“Failed to save data at the server”); <BR>echo “Store data in the cache (data will expire in 10 seconds)\n”; <BR>$get_result = $memcache->get(‘key'); <BR>echo “Data from the cache:\n”; <BR>var_dump($get_result); <BR>?> <BR>


读库的例子:

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


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的例子

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


而SQUID的例子我就不说明了,这方面网上有写的太多,大家自己搜索一下
DNS轮询:
BIND是一款开放源码的DNS服务器软件,这个要说起来就大了,自己搜索去,大家知道有这个东西就行了。
我知道的有chinacache 等大站就是这样做的,说简单点就是多服务器啦,把同一个页面或文件缓存到不同的服务器上,按南北自动解析到相关的服务器中。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP企业级应用之常见缓存技术篇_php基础
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址