MVC 的缓存层 - 模型还是控制器?
我正在重新考虑在哪里实现缓存部分。您认为最合适的实施地点在哪里?
在每个模型中,还是在控制器中?
方法 1(伪代码):
// 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:
没有多重获取/多重设置。如果返回的键数量较多,则会产生开销。
更容易维护,所有数据库/缓存处理都在每个模型中
方法 2:
更好的性能 - 使用 multiset/multiget
>需要更多代码
更难维护
告诉我你的想法!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
缓存应该在模型中完成。如果我必须在一般情况下进行选择,我可能最终会透明地缓存模型的数据库交互,这不需要您对其余部分进行任何更改代码。这当然可以在模型的父类中完成。
绝对要专注于缓存数据库查询结果,因为与数据库的交互是您会看到最大开销的地方。我认为缓存数据库结果(或者可能是整个初始化模型)比其他任何东西都更有效。
请记住,您可以在缓存之前序列化对象,因此将复杂类型(数组或对象)发送到内存缓存应该不成问题。 PHP 5 提供了神奇的方法
__sleep()
和__wakeup()
来实现序列化和重建序列化对象的目的。在 PHP 中缓存完整对象基本上是小菜一碟。请参阅http://php.net/manual/en/language.oop5.magic。 php 了解更多信息。您决定在初始化后不久仅缓存您的数据还是整个模型,这取决于您。
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.
我会将我的缓存职责牢牢地保留在模型中。模型获取数据与控制器或视图无关。他们所关心的是当请求数据时,提供数据——这就是 MVC 范例的工作方式。
将 mem_cache 功能抽象到父模型类中。它将减少您需要编写的代码量(代码=时间=金钱),简化对系统的修改,并消除您构建的每个模型产生的错误数量(请参阅前面的公式)。
标准化,标准化。
I would keep my caching responsibilities firmly within the model. It is none of the controller's or view's business where the model is getting data. All they care about is that when data is requested, data is provided - this is how the MVC paradigm is supposed to work.
Abstract your mem_cache functionality into the parent model class. It will cut down on the amount of code you need to write (code = time = money), simplify modifications to the system, and eliminate the number of bugs you produce per model that you build (see previous formula).
Standardize, standardize.