linux 系统下显示共享内存使用情况
ipcs -m
[root@iZwz953trdqtvmkegyoartZ ~]# ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00003124 131072 www 644 103 0
0x0000311a 4259841 www 755 103 0
0x0000311b 4292610 www 755 225 0
-------------------------------------------------------------------------
一般系统都有几十m以上的分配,用来存临时数据绰绰有余
//共享内存一般最小有32m 悠着点
//高并发请用锁
//key只允许int也就是数字
/**
* 保存变量
* @param [int] $key [description]
* @param [type] $bin [description]
* @return flase OR true
*/
function shm_set($key,$bin){
shm_del($key);
$bin=serialize($bin);
$len=strlen($bin);
$id=shmop_open($key,"n",0755,$len);
if (empty($id)) {
die($key."无法使用共享内存!!".$len."hhhh");
}
$ret=shmop_write($id, $bin,0);
shmop_close($id);
if($ret == false){
return false;
}else{
return true;
}
}
/**
* 读取变量
* @param [int] $key [description]
* @param [type] $bin [description]
* @return flase OR true
*/
function shm_get($key,&$bin){
@$id=shmop_open($key,"a",0755,0);
if (empty($id)) {
return false;
}
$bin='';
@$bin=unserialize(shmop_read($id,0,shmop_size ($id)));
shmop_close($id);
if($bin == ''){
return false;
}else{
return true;
}
}
/**
* 删除变量
* @param [int] $key [description]
*/
function shm_del($key){
@$id=shmop_open($key,"a",0755,0);
if (empty($id)) {
return;
}
shmop_delete($id);
shmop_close($id);
}
/**
* 读取会过期变量 变量timex
* @param [int] $key [description]
* @param [type] $bin [description]
* @return flase OR true
*/
function shm_get_gq($key,&$bin){
if(!shm_get($key,$bin)){
return false;
}
if(!isset($bin['timex'])){
return false;
}
if($bin['timex'] <= $gq){
return false;
}
$bin=$bin['bin'];
return true;
}
/**
* 设置会过期的变量
* @param [type] $key [description]
* @param [type] &$bin [description]
* @param [type] $gq 秒数
* @return [type] [description]
*/
function shm_set_gq($key,&$bin,$gq){
$d['timex']=time()+$gq;
$d['bin']=$bin;
return shm_set($key,$d);
}
用着非常方便,什么rds见鬼去吧