缓存与组

<?php
//组数据量小,每个元素包含数据量也小
//设置单缓存
$gkey="1000";
$skey="100";
$sdata=array(/*max data*/);
$a=cache_get($gkey);
$a[$skey]=$sdata;
cache_set($gkey,$a);

//删除单个缓存
$gkey="1000";
$skey="100";
$a=cache_get($gkey);
unset($a[$skey]);
cache_set($gkey,$a);

//获取单个缓存
$gkey="1000";
$skey="100";
$a=cache_get($gkey);
$val=$a[$skey];

//删除全部缓存
$gkey="1000";
cache_delete($gkey);
?>
<?php
//每个元素包含数据量大,单元素数量不多
//设置单缓存
$gkey="1000";
$skey="100";
$sdata=array(/*max data*/);
$temp_key="your prefix".$gkey.uniqid();
$gval=cache_get($gkey);
isset($gval[$skey])&&cache_delete($gval[$skey]);
$gval[$skey]=$temp_key;
cache_set($gkey,$gval);
cache_set($temp_key,$sdata);

//获取单个缓存
$gkey="1000";
$skey="100";
$gval=cache_get($gkey);
$val=isset($gval[$skey])?cache_get($gval[$skey]):null;

//删除单个缓存
$gkey="1000";
$skey="100";
$gval=cache_get($gkey);
isset($gval[$skey])&&cache_delete($gval[$skey]);

//删除一批缓存
$gkey="1000";
$gval=cache_get($gkey);
is_array($gval)&&foreach($gval as $v)cache_delete($v);
cache_delete($gkey);
?>
<?php
//每个元素数据量大,且包含元素量也非常大
//split_len 推荐值:
//    1000 => 1
//    1000 -100000 =>2
//    100000 以上 => 3
//设置单缓存
$gkey="1000";
$skey="100";
$split_len=2;
$sdata=array(/*max data*/);
$temp_key="your prefix".$gkey.uniqid();
$gval=cache_get($gkey);
$rid=substr(md5($skey),0,$split_len);
if(isset($gval[$rid])){
    $gkey_prefix=$gval[$rid];
    $ggval=cache_get($gkey_prefix);
    isset($ggval[$skey])&&cache_delete($ggval[$skey]);    
    $ggval[$skey]=$temp_key;
    cache_set($gkey_prefix,$ggval);
}else{
    $gkey_prefix="your gkey prefix".$gkey.uniqid();    
    $gval[$rid]=$gkey_prefix;
    cache_set($gkey,$gval);
    $ggval=array($skey=>$temp_key);
    cache_set($gkey_prefix,$ggval);
}
cache_set($temp_key,$sdata);
//获取单个缓存
$gkey="1000";
$skey="100";
$split_len=2;
$gval=cache_get($gkey);
$rid=substr(md5($skey),0,$split_len);
if(isset($gval[$rid])){
    $ggval=cache_get($gval[$rid]);
    $val=isset($ggval[$skey])?cache_get($ggval[$skey]):null;    
}else
    $val=null;
//删除单个缓存
$gkey="1000";
$skey="100";
$split_len=2;
$gval=cache_get($gkey);
$rid=substr(md5($skey),0,$split_len);
if(isset($gval[$rid])){
    $ggval=cache_get($gval[$rid]);
    isset($ggval[$skey])?cache_delete($ggval[$skey]):null;    
}

//删除一批缓存
$gkey="1000";
$gval=cache_get($gkey);
is_array($gval)&&foreach($gval as $v){
    $ggval=cache_get($v);
    is_array($ggval)&&foreach($ggval as $vv)
        cache_delete($vv);
    cache_delete($v);
}
cache_delete($gkey);
?>

 

posted @ 2016-04-01 22:04  liushan  阅读(229)  评论(0编辑  收藏  举报