下面举一个例子,来说明tmpfs和shm是如何被系统用到并做缓存来用的。这里是cache.inc.php中的obtain_cache函数:
functionobtain_config()
{
global$db,$global_db_prefix;
$config_cache_file='../../data/cache/config.cache';
if(defined('SHM_SUPPORT')){
global$shm;
@$CONF=$shm->get_var(SHM_VAR_PARA_CACHE);
if($CONF['cfg_end']!==0){
$sql="SELECT*FROM{$global_db_prefix}config";
$res=$db->sql_query($sql);
$CONF=array();
while($r=$db->sql_fetchrow($res)){
$CONF[$r['config_varname']]=$r['config_value'];
}
$CONF['cfg_end']=0;
$shm->put_var(SHM_VAR_PARA_CACHE,$CONF);
}
}
else{
if(!@file_exists($config_cache_file)){
$str="<?phprn//ConfigCacheFile...DONOTMODIFYTHISFILEPLEASE!!!rnif(!defined('IN_BSG')){rntexit;rn}rn".'$CONF=array('."rn";
$sql="SELECT*FROM{$global_db_prefix}config";
$query=$db->sql_query($sql);
while($r=$db->sql_fetchrow($query)){
$value="'".addslashes($r['config_value'])."'";
$str.="t'".$r['config_varname']."'ttt".'=>'.$value.",rn";
}
$str.="t'cfg_end'ttt=>0rn";
$str.=");rn?>";
if(!@$fp=fopen($config_cache_file,'w'))
returnfalse;
fwrite($fp,$str);
fclose($fp);
}
include($config_cache_file);
}
return$CONF;
}
这个函数分为两个部分,分别对应于shm和文件。首先它会检查系统是否支持共享内存(这个常量是在common.inc.php中已经设置过的),如果支持,函数会在config表中(具体应用中我也不知道你会把它放在哪里)读取所有的变量,并把它们放到一个数组中直接保存到shm里(当然实际操作不是这样简单的),如果系统不支持shm,函数会试图生成一个php文件。当再次调用这个函数时,如果shm里已经有了这个数组存在,或者已经有了这个文件存在的话(前面已经规定这个文件会被保存在tmpfs上),函数会直接返回它们的内容,不必再去读取数据库。{
global$db,$global_db_prefix;
$config_cache_file='../../data/cache/config.cache';
if(defined('SHM_SUPPORT')){
global$shm;
@$CONF=$shm->get_var(SHM_VAR_PARA_CACHE);
if($CONF['cfg_end']!==0){
$sql="SELECT*FROM{$global_db_prefix}config";
$res=$db->sql_query($sql);
$CONF=array();
while($r=$db->sql_fetchrow($res)){
$CONF[$r['config_varname']]=$r['config_value'];
}
$CONF['cfg_end']=0;
$shm->put_var(SHM_VAR_PARA_CACHE,$CONF);
}
}
else{
if(!@file_exists($config_cache_file)){
$str="<?phprn//ConfigCacheFile...DONOTMODIFYTHISFILEPLEASE!!!rnif(!defined('IN_BSG')){rntexit;rn}rn".'$CONF=array('."rn";
$sql="SELECT*FROM{$global_db_prefix}config";
$query=$db->sql_query($sql);
while($r=$db->sql_fetchrow($query)){
$value="'".addslashes($r['config_value'])."'";
$str.="t'".$r['config_varname']."'ttt".'=>'.$value.",rn";
}
$str.="t'cfg_end'ttt=>0rn";
$str.=");rn?>";
if(!@$fp=fopen($config_cache_file,'w'))
returnfalse;
fwrite($fp,$str);
fclose($fp);
}
include($config_cache_file);
}
return$CONF;
}
这就是一个简单的cache概念。究竟什么样的数据可以并且适合被cache?这和cache的更新方式有关。cache有定时间隔更新的,有不定时更新的。定时更新的指cache存在若干时间后再次重新生成cache,通常用于统计数据,比如在线人数等。不定时更新的是指生成后就一直保持不变,直到再次检测到不存在或已过期、已损坏等情况出现,通常见于参数调用、模板编译结果等。这些数据的特点是它们都是临时的,可以被丢弃的,比如没人会在乎一个模板是否被重新编译过,除了在编译的那次执行中多占用一点时间。这批可丢弃的数据就可以被放心地保存在内存或者tmpfs中,因为它们不怕丢失,并且随时可以被重建。
早期版本的PHPWIND论坛的cache机制是很差的,虽然它很快,但是很脆弱,一旦cache文件损坏或丢失,它不会自己去创建它,而是直接导致程序无法运行,这种只能叫做临时文件,而不能叫cache。我不知道现在的PHPWIND什么样,因为我一直没兴趣去看它……
下面是shm.inc.php的源码,我不想对它加太多的注释,因为它很机械,没什么好注释的。唯一需要注意的是php的两种支持shm的方式。一种是shmop,一种是sysv的shm,不同的是sysv只在UNIX/LINUX系统中存在,shmop更底层,只接受字符串数据。
<?php
classBsmShm
{
var$shm_id;
var$shm_mod;
functionBsmShm()
{
//InitSharedMemorySupport...
//BothSysVShmandShmoparesupportunder*NIXOperatingSystem
//ButOnlyShmopcanbeusedinWindows.
if(get_sys()==SYSTEM_WIN){
if(function_exists('shmop_open')){
$this->shm_mod='shmop';
}
else{
$this->shm_mod='none';
$this->shm_id=false;
}
}
else{
if(function_exists('shm_attach')){
$this->shm_mod='sysv';
}
elseif(function_exists('shmop_open')){
$this->shm_mod='shmop';
}
else{
//NoModuleinstalled
$this->shm_mod='none';
$this->shm_id=false;
}
}
if($this->shm_mod=='sysv'){
$this->shm_id=shm_attach(ftok(__FILE__,'g'),SHM_SIZE,0600);
}
elseif($this->shm_mod=='shmod'){
//ifno"sysv"moduleinstalled,function"ftok())"isunavailiable.
$this->shm_id=shmop_open(SHM_KEY,'n',0600,SHM_SIZE);
}
return;
}
functionput_var($varkey,$varval)
{
//Writeavalueintoshm
if($this->shm_mod=='sysv')
returnshm_put_var($this->shm_id,$varkey,$varval);
elseif($this->shm_mod=='shmod'){
//shmopismuchmorelow-levelthansysv,youneedtooperateeverybyteyourself!
$curr=shmop_read($this->shm_id,0,shmop_size($this->shm_id));
$curr=base64_decode($curr);
$curr=substr($curr,0,strpos($curr,"
这个class同时支持sysv和shmop,对于shmop,它把数据做了序列化,并用一个\0做为数据的结束。因为序列化本身并不是很快,所以有可能的话,还是sysv的shm稳定一些。classBsmShm
{
var$shm_id;
var$shm_mod;
functionBsmShm()
{
//InitSharedMemorySupport...
//BothSysVShmandShmoparesupportunder*NIXOperatingSystem
//ButOnlyShmopcanbeusedinWindows.
if(get_sys()==SYSTEM_WIN){
if(function_exists('shmop_open')){
$this->shm_mod='shmop';
}
else{
$this->shm_mod='none';
$this->shm_id=false;
}
}
else{
if(function_exists('shm_attach')){
$this->shm_mod='sysv';
}
elseif(function_exists('shmop_open')){
$this->shm_mod='shmop';
}
else{
//NoModuleinstalled
$this->shm_mod='none';
$this->shm_id=false;
}
}
if($this->shm_mod=='sysv'){
$this->shm_id=shm_attach(ftok(__FILE__,'g'),SHM_SIZE,0600);
}
elseif($this->shm_mod=='shmod'){
//ifno"sysv"moduleinstalled,function"ftok())"isunavailiable.
$this->shm_id=shmop_open(SHM_KEY,'n',0600,SHM_SIZE);
}
return;
}
functionput_var($varkey,$varval)
{
//Writeavalueintoshm
if($this->shm_mod=='sysv')
returnshm_put_var($this->shm_id,$varkey,$varval);
elseif($this->shm_mod=='shmod'){
//shmopismuchmorelow-levelthansysv,youneedtooperateeverybyteyourself!
$curr=shmop_read($this->shm_id,0,shmop_size($this->shm_id));
$curr=base64_decode($curr);
$curr=substr($curr,0,strpos($curr,"
共享内存的原本用途并不是做缓存,也不是做buffer,它是用来做进程间通信的。它可以保存临时队列,传递信号量等。我们在这里变通地用它来保存点东西,只是因为它的速度快得实在不是文件和数据库存取能比的。而且因为它的共享性,它在一段web脚本结束之后不会消失,所以它特别适合用来实现Application变量(不用再羡慕ASP了)。
