在php memcache缓存里,我只找到 add,delete两种方法添加或删除缓存,没有分组功能像JAVA版 memecache有分组功能,如我用一个用户日记分组 当我更新了日记,可以删除此日记分组缓存,我的个人日记列表,我的WAP版日记列表也更新我想有如何功能add("cacheGroup","cacheName1",$value1);
add("cacheGroup","cacheName2",$value2);
add("cacheGroup","cacheName3",$value3);
deleteGroup("cacheGroup");
然后上面三个缓存都清除
php memecache缓存API如下
Memcache::add — 添加一个值,如果已经存在,则返回false
Memcache::addServer — 添加一个可供使用的服务器地址
Memcache::close — 关闭一个Memcache对象
Memcache::connect — 创建一个Memcache对象
memcache_debug — 控制调试功能
Memcache::decrement — 对保存的某个key中的值进行减法操作
Memcache::delete — 删除一个key值
Memcache::flush — 清除所有缓存的数据
Memcache::get — 获取一个key值
Memcache::getExtendedStats — 获取进程池中所有进程的运行系统统计
Memcache::getServerStatus — 获取运行服务器的参数
Memcache::getStats — 返回服务器的一些运行统计信息
Memcache::getVersion — 返回运行的Memcache的版本信息
Memcache::increment — 对保存的某个key中的值进行加法操作
Memcache::pconnect — 创建一个Memcache的持久连接对象
Memcache::replace — R对一个已有的key进行覆写操作
Memcache::set — 添加一个值,如果已经存在,则覆写
Memcache::setCompressThreshold — 对大于某一大小的数据进行压缩
Memcache::setServerParams — 在运行时修改服务器的参数 

解决方案 »

  1.   

    使用php的多维数组,应该能解决你的疑惑吧!
      

  2.   

    memcache好象没有这个功能!只能对应这去删除吧!
      

  3.   

    add("cacheGroup","cacheName1",$value1);
    add("cacheGroup","cacheName2",$value2);
    add("cacheGroup","cacheName3",$value3);
    deleteGroup("cacheGroup");你的这种方式,你可以扩充mem
    class myMem extends memcache {

    private $groupExpireDefault = 14400;

    public function setGroup($group, $name, $value) {

    parent::set($group.'_'.$name, $value);
    }

    public function delGroup($group) {
    parent::set($group, '-1');
    }

    public function setGroupExpire($group, $expire=null) {

    if(!$expire) $expire = time()+$this->groupExpireDefault;
    parent::set($group, $expire);
    }

    public function getGroup($group, $name) {
    // 如果已经过期
    if(parent::get($group) < time()) {
    return null;
    }

    return parent::get($group.'_'.$name);
    }
    }$mymem = new myMem();$mymem->setGroupExpire('group1', time()+14400);
    $mymem->setGroup('group1','xxx','xxx');
    $mymem->setGroup('group1','yyy','yyy');echo $mymem->getGroup('group1', 'xxx'); //xxx$mymem->delGroup('group1');echo $mymem->getGroup('group1', 'xxx'); // is empty
      

  4.   

    你可以把一个分类的key值作为一个数组存起来,在后续的操作中只需要更新这个数组即可。
      

  5.   


    我测试了你的程序是可行的,但是有几点不解1、
    你在 setGroupExpire 使用 parent::set($group, $expire); 这个不就是添加一个缓存么?他怎么成了设置缓存时间?2、
    parent::set($group.'_'.$name, $value);parent::set($group, '-1');这两个key值完全不一样,程序又如何实现删除一个$group把$group.'_'.$name所有缓存都删除了我没有看memcache源码。
    我在用 echo $mymem->get($group);  得到了$expire时间值
      

  6.   

    add("cacheGroup","cacheName1",$value1);
    add("cacheGroup","cacheName2",$value2);
    add("cacheGroup","cacheName3",$value3);
    deleteGroup("cacheGroup");这样也就是一个key 控制一数组class mem
    {
    public $memcache=null;
    public $array=array();
    public function __construct()
    {
    $this->memcache = new Memcache;
    $this->memcache->connect('localhost', 11211) or die ("Could not connect");
    }
    public function add($parent="",$key="",$value="")
    {
    $this->array[$key]=$value;
    $this->memcache->set($parent,$this->array,false,500);
    }
    public function deleteGroup($key=null)
    {
    $this->memcache->delete($key);
    }
    public function get($key=null)
    {
    return $this->memcache->get($key);
    }
    }
    $mem=new mem();
    $mem->add("cacheGroup","cacheName1","1");
    $mem->add("cacheGroup","cacheName2","2");
    $mem->add("cacheGroup","cacheName3","3");
    //$mem->deleteGroup("cacheGroup");
    var_Dump($mem->get('cacheGroup'));
      

  7.   

    这里其实是用了一个小技巧,来避免使用一个数组(作为一个group)存放内容,如果你的数据过多的话,并且group里元素个数也很多的话,会频繁更新同一个数组的内容,并且不见得就是要读取一个元素时就是要读取全部的数据。这种机制不是靠MC(memcache的缩写,下同)来控制缓存的过期时间,而是靠自己控制是否缓存过期!设置这个缓存的内容,就是整个组的过期时间。因为是靠
        public function getGroup($group, $name) {
            // 如果已经过期
            if(parent::get($group) < time()) {
                return null;
            }
            
            return parent::get($group.'_'.$name);
        }上面的代码来判断是否过期,这样所有这个group里的数据,都有了统一的时间验证标准,只要parent::get($group) < time()成立,即便MC中有数据或者说数据没有过期,我们也认为过期了
    其实这个数据并没有删除,还在MC中,但是在你重新存数据之前,已经是垃圾数据了!在group的缓存周期到期时,如果你不在存新的内容到相应的key(例如:$group.'_'.$name),那么在真正的内容过期(指定有默认过期时间)或者内存不够用(没有指定默认过期时间),MC就会通过GC(垃圾回收机制)自动回收了,因为这个数据经常不会用,也就是没有缓存命中,所以这方面你就不用担心了!
      

  8.   

    非常感谢 hnxxwyq ,设置一个缓存时间缓存--->这小技巧就是关键
      

  9.   

    class MyCache
    {
        private $mmc = null;
        private $group = null;
        private $version = 1;
        function __construct($group){
            if(!class_exists('memcache')){
                $this->mmc = false;
                return;
            }
            $this->mmc = new memcache();
            $ip   = //ip
            $port = //port
            $this->mmc->addServer($ip, $port); //可添加多个服务器
            $this->group = $group;
            $this->version = $this->mmc->get('version_'.$group);
            if(!$this->version) $this->version = 1;
        }
        
        function set($key, $var, $expire=3600){
            if(!$this->mmc)return;
            return $this->mmc->set($this->group.'_'.$this->version.'_'.$key, $var, $expire);
        }
        function get($key){
            if(!$this->mmc)return;
            return $this->mmc->get($this->group.'_'.$this->version.'_'.$key);
        }
        function incr($key, $value=1){
            if(!$this->mmc)return;
            return $this->mmc->increment($this->group.'_'.$this->version.'_'.$key, $value);
        }
        function decr($key, $value=1){
            if(!$this->mmc)return;
            return $this->mmc->decrement($this->group.'_'.$this->version.'_'.$key, $value);
        }
        function delete($key){
            if(!$this->mmc)return;
            return $this->mmc->delete($this->group.'_'.$this->version.'_'.$key);
        }
        function flush(){
            if(!$this->mmc)return;
            ++$this->version;
            $this->mmc->set('version_'.$this->group, $this->version);
        }
    }
      

  10.   

    楼上的代码有一定问题,不能灵活控制,经测试,这个方案应该比较好了,使用的时候外面new 一个mycache对象,传一个group名称即可class MyCache
    {
        private $mmc = null;
        private $group = null;
        private $version = 1;
        function __construct($group){
            if(!class_exists('memcache')){
                $this->mmc = false;
                return;
            }
            $this->mmc = new memcache();
            $ip   = K::$config->item('cache', 'memcache');
            $port = K::$config->item('cache', 'memport');
            $this->mmc->addServer($ip, $port); //可添加多个服务器
            $this->group = $group;
            $this->version = $this->mmc->get('version_'.$group);
            if(!$this->version) $this->version = 1;
        }
        
        //function test(){
        // $this->mmc->connect() or die ("Could not connect memcache");
        //}
        
        function set($key, $var, $expire=3600){
            if(!$this->mmc)return;
            return $this->mmc->set($this->group.'_'.$this->version.'_'.$key, $var, $expire);
        }
        function get($key){
            if(!$this->mmc)return;
            return $this->mmc->get($this->group.'_'.$this->version.'_'.$key);
        }
        function incr($key, $value=1){
            if(!$this->mmc)return;
            return $this->mmc->increment($this->group.'_'.$this->version.'_'.$key, $value);
        }
        function decr($key, $value=1){
            if(!$this->mmc)return;
            return $this->mmc->decrement($this->group.'_'.$this->version.'_'.$key, $value);
        }
        function delete($key){
            if(!$this->mmc)return;
            return $this->mmc->delete($this->group.'_'.$this->version.'_'.$key);
        }
        function flush(){
            if(!$this->mmc)return;
            ++$this->version;
            $this->mmc->set('version_'.$this->group, $this->version);
        }
    }