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

  • 相关阅读:
    飞控相关资料
    PID
    详解NXP Cortex-M3加密设置
    ucos ii 百度官方介绍
    两个静态页面之间值传递方式
    Wex5各组件介绍
    链接学习
    WeX5基础
    Oracle中用触发器实现自动记录表数据被修改的历史信息
    HTML DOM setInterval() 方法
  • 原文地址:https://www.cnblogs.com/imxiu/p/3451709.html
Copyright © 2011-2022 走看看