Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache layer for MVC - Model or controller?

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?

Approach 1 (psuedo-code):

// 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;
    }  
}

Approach 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;
    }  
}

Thoughts:

Approach 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

Approach 2:

  • Better performancewise - multiset/multiget is used

  • More code required

  • Harder to maintain

Tell me what you think!

like image 908
Industrial Avatar asked May 26 '10 17:05

Industrial


People also ask

How to cache data in ASP.NET MVC?

In ASP.NET MVC, there is an OutputCache filter attribute that you can apply and this is the same concept as output caching in web forms. The output cache enables you to cache the content returned by a controller action. Output caching basically allows you to store the output of a particular controller in the memory.

What is output cache in MVC?

The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.

What is a cache layer?

In computing, a cache is a high-speed data storage layer which stores a subset of data, typically transient in nature, so that future requests for that data are served up faster than is possible by accessing the data's primary storage location.


1 Answers

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.

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.

like image 137
Kenaniah Avatar answered Sep 27 '22 19:09

Kenaniah