文字

Shared Memory

  • 简介
  • 安装/配置
    • 需求
    • 安装
    • 运行时配置
    • 资源类型
  • 预定义常量
  • 范例
    • Basic usage
  • Shared Memory 函数
    • shmop_close — Close shared memory block
    • shmop_delete — Delete shared memory block
    • shmop_open — Create or open shared memory block
    • shmop_read — Read data from shared memory block
    • shmop_size — Get size of shared memory block
    • shmop_write — Write data into shared memory block

用户评论:

[#1] mattcsl at gmail dot com [2013-10-26 23:58:16]

thankyou,

this function had a missing comma between test1 and test2 and is fixed below

function get_cache_id($name) {
    // maintain list of caches here
    $id=array(  'test1' => 1,
                'test2' => 2
                );

    return $id[$name];
}

[#2] makr at makrit dot net [2013-03-09 23:05:38]

shmop made simple:
$test=get_cache('test');
save_cache($test, 'test1', 600);

To save/get cache that easily just save this as cache.php or whatever you see fit:

<?php

function save_cache($data$name$timeout) {
    
// delete cache
    
$id=shmop_open(get_cache_id($name), "a"00);
    
shmop_delete($id);
    
shmop_close($id);
    
    
// get id for name of cache
    
$id=shmop_open(get_cache_id($name), "c"0644strlen(serialize($data)));
    
    
// return int for data size or boolean false for fail
    
if ($id) {
        
set_timeout($name$timeout);
        return 
shmop_write($idserialize($data), 0);
    }
    else return 
false;
}

function 
get_cache($name) {
    if (!
check_timeout($name)) {
        
$id=shmop_open(get_cache_id($name), "a"00);

        if (
$id$data=unserialize(shmop_read($id0shmop_size($id)));
        else return 
false;          // failed to load data

        
if ($data) {                // array retrieved
            
shmop_close();
            return 
$data;
        }
        else return 
false;          // failed to load data
    
}
    else return 
false;              // data was expired
}

function 
get_cache_id($name) {
    
// maintain list of caches here
    
$id=array(  'test1' => 1
                
'test2' => 2
                
);

    return 
$id[$name];
}

function 
set_timeout($name$int) {
    
$timeout=new DateTime(date('Y-m-d H:i:s'));
    
date_add($timeoutdate_interval_create_from_date_string("$int seconds"));
    
$timeout=date_format($timeout'YmdHis');

    
$id=shmop_open(100"a"00);
    if (
$id$tl=unserialize(shmop_read($id0shmop_size($id)));
    else 
$tl=array();
    
shmop_delete($id);
    
shmop_close($id);

    
$tl[$name]=$timeout;
    
$id=shmop_open(100"c"0644strlen(serialize($tl)));
    
shmop_write($idserialize($tl), 0);
}

function 
check_timeout($name) {
    
$now=new DateTime(date('Y-m-d H:i:s'));
    
$now=date_format($now'YmdHis');

    
$id=shmop_open(100"a"00);
    if (
$id$tl=unserialize(shmop_read($id0shmop_size($id)));
    else return 
true;
    
shmop_close($id);

    
$timeout=$tl[$name];
    return (
intval($now)>intval($timeout));
}

?>

上一篇: 下一篇: