zoukankan      html  css  js  c++  java
  • Yii 打造带有缓存功能的AR

    继承AR类 重写 findByPk方法为pk  还有afterSave afterDelete

    通过对象主键缓存其属性  在insert update delete 操作时候 都会自动更新缓存
    还是挺方便的

    public function afterDelete()
    {
        $cache = $this->getCache();
        $cache->cdelete($this->cacheGroup(), $this->getPKValue());
        parent::afterDelete();
    }
    
    public function cacheGroup()
    {
        return $this->tableName();
    }
    
    public function pk($value)
    {
        $attr = $this->getCache()->cget($this->cacheGroup(), $value);
        if (!$attr) {
            $model = $this->findByPk($value);
            if ($model) {
                $attr = $model->getAttributes();
            }
            $this->getCache()->cset($this->cacheGroup(), $value, $attr = !empty($attr) ? $attr :
                MMemcache::NULL_VAR);
        } elseif (MMemcache::isNULL($attr)) {
            $model = null;
        } else {
            $model = $this->populateRecord($attr);
        }
        return $model;
    }
    
    public function afterSave()
    {
        $cache = $this->getCache();
        $cache->cdelete($this->cacheGroup(), $this->getPKValue());
        if ($this->getIsNewRecord()) {
            $cache->cadd($this->tableName(), $this->getPKValue(), $this->getAttributes());
        } else {
            $cache->cadd($this->tableName(), $this->getPKValue(), $this->getAttributes());
        }
        parent::afterSave();
    }

    MMemcache 自己的情况实现

    大家有好思路 多分享哦  这样Yii才会越来越强大 

    From: http://hc1988.diandian.com/page/2?tag=yii

  • 相关阅读:
    进度条加载后显示页面
    解决跨域问题
    js下IE和FF的一些兼容写法总结
    linux
    linux 批量替换文件内容
    DVWA-1.9之fileupload
    python库安装失败的解决方法
    python程序打包
    CF 1133C Balanced Team
    CF 1133B Preparation for International Women's Day
  • 原文地址:https://www.cnblogs.com/imxiu/p/3451709.html
Copyright © 2011-2022 走看看