緩存與組

<?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);
?>
相關文章
相關標籤/搜索