MVC的缓存层-模型还是控制器? [英] Cache layer for MVC - Model or controller?

查看:69
本文介绍了MVC的缓存层-模型还是控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在哪里实现缓存部分有一些新的想法.您认为最合适的实施位置在哪里?

I am having some second thoughts about where to implement the caching part. Where is the most appropriate place to implement it, you think?

在每个模型中还是在控制器中?

Inside every model, or in the controller?

// mycontroller.php

MyController extends Controller_class {
   function index () {
        $data = $this->model->getData();
        echo $data;
   }
}

// myModel.php

MyModel extends Model_Class{
    function getData() {

        $data = memcached->get('data');

        if (!$data) {
            $query->SQL_QUERY("Do query!");
        }

        return $data;
    }  
}

方法2:

// mycontroller.php

MyController extends Controller_class {
   function index () {
        $dataArray = $this->memcached->getMulti('data','data2');

        foreach ($dataArray as $key) {
            if (!$key) {
                $data = $this->model->getData();
                $this->memcached->set($key, $data);
            }
        }

        echo $data;
   }
}

// myModel.php

MyModel extends Model_Class{
    function getData() {           
        $query->SQL_QUERY("Do query!");

        return $data;
    }  
}

想法:

方法1:

  • 无多重/多重设置.如果返回大量密钥,则会导致开销.

  • No multiget/multi-set. If a high number of keys would be returned, overhead would be caused.

易于维护,所有数据库/缓存处理均在每个模型中

Easier to maintain, all database/cache handling is in each model

方法2:

  • 更好的性能-使用多集/多集

  • Better performancewise - multiset/multiget is used

需要更多代码

维护困难

告诉我您的想法!

推荐答案

应该在模型中进行缓存.如果我不得不选择一般方式,我可能最终会透明地缓存模型的数据库交互,不需要您对其余代码进行任何更改.当然,这将在模型的父类中完成.

Caching should be done in the model. If I had to choose in general, I would probably end up transparently caching the model's database interaction, which wouldn't require you to make any changes to the rest of your code. This of course would be done in the parent class of your models.

一定要专注于缓存数据库查询结果,因为与数据库的接口是您最大的开销.我认为,缓存数据库结果(或者可能是整个初始化模型)比其他任何方式都更有效率.

Definitely focus on caching your database query results, as interfacing with your database is where you will see the most overhead. I would argue that it would be more efficient to cache your database results (or maybe your entire initialized model) more than anything else.

请记住,您可以在缓存之前序列化对象,因此将复杂类型(数组或对象)发送到内存缓存中不会有问题. PHP 5提供了神奇的方法__sleep()__wakeup(),用于序列化和重构序列化对象的目的.在PHP中缓存完整对象基本上是小菜一碟.参见 http://php.net/manual/en/language.oop5.magic. php 了解更多信息.

Remember that you can serialize your objects before caching, so sending complex types (arrays or objects) into memcache shouldn't be a problem. PHP 5 provides the magic methods __sleep() and __wakeup() for the very purposes of seralizing and reconstructing your serialized objects. Caching full objects in PHP is basically a piece of cake. See http://php.net/manual/en/language.oop5.magic.php for more info.

在初始化后不久决定是否仅缓存数据或整个模型由您决定.

Whether you decide to cache just your data or your entire model shortly after initialization is up to you.

这篇关于MVC的缓存层-模型还是控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆