memcachedb和memcached的两个主要区别:
1 前者不管理过期,每个存储都是长期有效
2 前者使用磁盘存储,有广阔的存储空间

这两个优点使得很多缓存的地方可以使用上memcachedb。

1 本地数据缓存
这个带来的最大便利是可以实现数据共享,备份简便,这是本地文件缓存无法比拟的。
2 网页缓存
在Nginx可以使用memcache接口,很简便的实现网页cache

以上两项应用需要解决过期问题方式不同:

1 对于本地数据缓存,可以再保存一个值是当前保存的数据的时间即可。
类似读写文件一样,记录修改时的时间戳
$key=>"http://www.aslibra.com/"
$key."_time"=>time()


2 对于nginx无法判断是否过期的情况,可以实现自动清理,阿权设计方案的是按秒存储数据,定期清理
清理的方法决定了保存的其它数据的方式,你也可以写入文件日志的方式,然后按日志清理
这里用自身的存储,记录相应的数据信息
$key=>"http://www.aslibra.com/"
$key2=>"http://www.aslibra.com/blog/"
$expire_time=>Num
$expire_time.'_'.Num=>$key
$expire_time.'_'.Num2=>$key2


这个主要在解决$expire_time的自增的问题,细节可以处理此问题
需要用到increment和add,他们都有键值是否存在的问题,根据此功效解决冲突。
先尝试increment,如果成功,说明已经有了,否则可能没有。在没有的情况下,尝试添加此键值,成功说明此时没有同时的进程,否则有同时请求的进程已经在此短暂的不行的时间里添加了,那就再次调用increment即可。

下面是封装了的PHP代码:

<?

/****************************************
*** memcachedb_manage version 0.1 @ 2009-6-12
*** code by hqlulu#gmail.com
*** http://www.aslibra.com
*** http://code.google.com/p/memcachedb-manage/
*** http://code.google.com/p/memcachedb/
*** let the memcachedb acts like memcached!
****************************************/

class memcachedb extends Memcache{

  //是否有调试信息
  var $debug = false;
  //兼容使用memcache,这样就不用set时再保存额外数据
  var $is_memcachedb = true;
  //清理时间开始为0时,默认最小的清理时间差
  var $mini_clear_time = 86400;
  //临时数据前缀
  var $prefix = 'db_';
  //临时数据,清理的最后时间戳
  var $last_timeid_key = 'last_timeid';
  
  //本地处理缓存有效性
  var $cache_expire = 900;
  var $cache_prefix = 'WWW_FILE_';
  var $cache_time_prefix = 'WWW_TIME_';

  function memcachedb(){
  }
  
  //改写函数
  //此功能适合前端无法判断数据是否有效的情况,需要自行清理过期数据
  function set($key, $value, $flag=0, $expire=0){

    if($expire>0 && $this->is_memcachedb){
      $mykey = $this->prefix.(time() + $expire);
      //尝试取得自增值
      $i = parent::increment($mykey);
      if($i == FALSE){
        //尝试增加key,防止用set产生的冲突
        $i = parent::add($mykey,1) ? 1 : parent::increment($mykey);
      }
      parent::set($this->mykey($mykey, $i), $key);
    }

    return parent::set($key, $value, $flag, $expire);
  }

  //管理自增的键值组合方式
  function mykey($mykey, $i){
    return $mykey.'_'.$i;
  }

  //保存上次清理的时间戳的键值
  function last_timeid_key(){
    return $this->prefix.$this->last_timeid_key;
  }
  
  //清理
  function clear($start=0, $end=0){

    //取得上次清理的最后时间戳,可以不需要从time_start开始
    $last_timeid = parent::get($this->last_timeid_key());
    if($last_timeid == false) $last_timeid = 0;
    $start = $last_timeid>$start ? $last_timeid : $start;
    $start = $start>0 ? $start : ( time() - $this->mini_clear_time );
    $end = $end>0 ? $end : time();
    if($this->debug)echo " clear $start to $end <br>";

    for($mytime = $start; $mytime<=$end; $mytime++){
      $mykey = $this->prefix.$mytime;
      $auto_id = parent::get($mykey);
      if($auto_id){
        if($this->debug)echo "<hr>clear time: $mytime<br>";
        for($i = 1;$i<=$auto_id;$i++){
          $key = parent::get($this->mykey($mykey, $i));
          if($this->debug)echo " delete ".$k."<br>";
          parent::delete($key);
          if($this->debug)echo " delete ".$this->mykey($mykey, $i)."<br>";
          parent::delete($this->mykey($mykey, $i));
        }
        if($this->debug)echo " delete $mykey<br>";
        parent::delete($mykey);
      }
    }

    //记下当前处理过的时间戳
    $this->set($this->last_timeid_key(), $end);
  }

  //缓存读写
  //此功能适合PHP使用的数据,自行判断数据是否有效的情况
  function get_cache($key, $expire=0){
    $expire = $expire>0 ? $expire : 0;
    if($expire == 0)
      return parent::get($this->cache_prefix.$key);
    $cache_time = parent::get($this->cache_time_prefix.$key);
    $cache_time = $cache_time == FALSE ? 0 : $cache_time;
    if(time() - $cache_time <= $expire)
      return parent::get($this->cache_prefix.$key);
    return FALSE;
  }

  function set_cache($key, $value, $flag=0, $expire=0){
    $expire = $expire>=0 ? $expire : $this->cache_expire;
    parent::set($this->cache_time_prefix.$key, time());
    return parent::set($this->cache_prefix.$key, $value, $flag, $expire);
  }
}
?>


一些示例:

<?
require_once('memcachedb.class.php');

$m = new memcachedb;
$m->connect('192.168.1.5', 11311);

//保存一个300秒有效期的数据
$m->set('test','www.aslibra.com', 0, 300);
//清理到1000秒前到100秒前过期的数据
$m->clear(time()-1000, time()-100);

//获取一个键值,不理会是否过期,相当于get
echo $m->get_cache("test", 0);
//获取一个键值,300秒内保存的才有效
$m->get_cache("test", 300);
//保存一个数据,300秒内有效
$m->set_cache("test", "www.aslibra.com", 0, 300);
//保存一个数据,长期有效,相当于set
$m->set_cache("test", "www.aslibra.com");

//清理到目前已经过期是数据
$m->clear();
?>


相关资料:

1 memcachedb-manage
2 memcachedb
3 代码下载

代码更新:

# Non-members may check out a read-only working copy anonymously over HTTP.
svn checkout http://memcachedb-manage.googlecode.com/svn/trunk/ memcachedb-manage-read-only



原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
发表评论
表情
emotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemotemotemotemotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写
 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐