一、数据缓存

这里所说的数据缓存是指数据库查询缓存,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓存表或文件中获得。

用的最广的例子看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;

apache mod_proxy的例子:

复制代码代码如下:

<VirtualHost *>
ServerName jb51.net
ServerAdmin admin@zxsv.com
# reverse proxy setting
ProxyPass / http://jb51.net:8080/
ProxyPassReverse / http://jb51.net: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/jb51_net_access_log combined
</VirtualHost>

十、DNS轮询

BIND是一款开放源码的DNS服务器软件,这个要说起来就大了,自己搜索去,大家知道有这个东西就行了。

我知道的有chinacache等大站就是这样做的,说简单点就是多服务器啦,把同一个页面或文件缓存到不同的服务器上,按南北自动解析到相关的服务器中。

转载于:https://www.cnblogs.com/ZDPPU/p/6838346.html

PHP常用的缓存技术汇总相关推荐

  1. 缓存的基本概念和常用的缓存技术

    摘要: 介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1         概念 1.1   缓存能解决的问题 · 性 ...

  2. .Net环境下的缓存技术介绍 (转)

    摘要: 介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1         概念 1.1   缓存能解决的问题 · 性 ...

  3. 前端性能优化之缓存技术

    缓存一直以来都是用来提高性能的一项必不可少的技术 , 利用这项技术可以很好地提高web的性能. 缓存可以很有效地降低网络的时延,同时也会减少大量请求对于服务器的压力. 接下来这篇文章将会详细地介绍在w ...

  4. .Net环境下的缓存技术介绍

    摘要: 介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1         概念 1.1   缓存能解决的问题 · 性 ...

  5. java ee有哪些工具_JavaEE开发中最常用到的技术和工具汇总

    原标题:JavaEE开发中最常用到的技术和工具汇总 今天千锋广州小编给大家来介绍一下关于目前JavaEE开发中最常用到的技术和工具的介绍,下面我们一起来看一下吧. 项目管理:Ant,项目管理事实上的标 ...

  6. Python常用6个技术网站汇总分享!

    Python是一门面向对象的编程语言,它具有丰富和强大的库,能够把用其他语言编写的各种模块轻松地联结在一起,因此也常被称为"胶水语言".Python技术会随着互联网的不断发展一直迭 ...

  7. 阿里巴巴开源技术汇总:115个软件(四)

    阿里巴巴开源技术汇总:115个软件(四) 摘要:阿里巴巴开源技术汇总:115个软件(四)今天发布了!在前面三期中相信大家已经浏览了很多优秀的开源作品,领略到了开源的魅力.我们相信今天第四期的内容也不会 ...

  8. 阿里巴巴开源技术汇总:115个软件(六)

    阿里巴巴开源技术汇总:115个软件(六) 摘要: 今天我们的阿里巴巴开源技术汇总更新到了第六期,也算是这个系列的最后一期了.回顾这一百多个开源项目,它们凝聚了许多阿里人无数的心血和汗水,但我们相信阿里 ...

  9. 阿里巴巴开源技术汇总:115个软件(三)

    阿里巴巴开源技术汇总:115个软件(三) 摘要:Hi~大家好,第三期阿里开源技术汇总又和大家见面了!在这一期中我们又给大家带来了20个项目的介绍,保证能让你们看得大呼过瘾.准备好了吗?现在就让我们跟随 ...

最新文章

  1. Anaconda查看、删除、增加channel
  2. 2021年,作为算法工程师的你们会在CV业务落地上用Transformer吗?
  3. 微信支付-接收返回通知结果
  4. Dockerfile命令
  5. 为什么我启动哪一个tomcat都是启动同一个tomcat(tomcat7)
  6. 技术宝典 | WebRTC 技术解析之 Android VDM
  7. 数据上报痛点解决方案
  8. 关于Ubuntu的默认python版本
  9. 迈克尔·戴尔再次到访2019戴尔科技峰会,为“戴尔中国4.0+战略”加个码……
  10. svnadmin服务安装
  11. vac虚拟声卡我linux,下载_Virtual Audio Cable(虚拟声卡) V4.65 完美免费版_6z6z下载站
  12. HTML零基础入门教程完整版
  13. QQ对话框、背景渐变色
  14. paypal php 退款,PayPal的Restful-API方式退款、WEB支付、回调
  15. android版本高低有啥好处与不好,WP跟安卓比流畅 但为什么就不好用呢?
  16. 出外旅游,应该如何换汇最划算——人民币兑换外币手续费成本最低经验谈,如美元澳元日元欧元英镑新元港币...
  17. python抓取网络摄像头,Python抓取酒店针孔摄像头 防偷窥
  18. 轻松学Java基础篇 小白必备!
  19. python图像倾斜校正_python 图像倾斜校正
  20. 路由器与交换机配置——交换机默认网关(实现跨网段telnet)

热门文章

  1. pytorch中上采样的实现
  2. Dollar toolbox 学习笔记(一)
  3. leetcode —— 面试题36. 二叉搜索树与双向链表
  4. leetcode - 638. 大礼包
  5. Java中Math3 各种随机数生成器的使用(Random Generator)
  6. C++中时间相关函数的使用
  7. Quartz实线定期运行程序(Java)
  8. 强跟踪卡尔曼滤波STF估算车辆质量——matab simulink仿真
  9. TensorFlow第十步CNN BP 编程求解
  10. 离散余弦变换原理及实现过程【转载】