MVC,如何组织页面的缓存部分以及如何与View结合?

发布于 2024-08-24 10:51:39 字数 978 浏览 11 评论 0原文

现在我正在尝试创建自己的小型 MVC(仅用于练习和理解 MVC 模式细节)。我想缓存部分页面(下拉列表、列表等),但我不知道组织它的最佳方式是什么。

假设我有 PostsController 和 getPostDetailsS​​hortly($post_id) 方法。 这个方法可能看起来像这样...

public function getPostDetailsShortly($post_id) {

  if (!$post_id) return false;
  $content = $this->memcache->get("post" . $post_id); //Trying to get post details HTML from memcache

  if (!$content) { //Not fount in memcache

    $model = new PostsModel();
    $data = $model->getPostDetailsShortly($post_id);

    $this->view->assign('data', $data);

    ob_start();
    $this->view->render();
    $content = ob_get_contents(); //Getting view output into variable
    ob_end_clean();

    $this->memcache->set('post' . $post_id, $content, 1000); //Writing variable to memcache

  }

  return $content;

}

现在我应该使这个控制器方法可以从视图中使用。因为我将在其他页面内使用它,例如,用于构建相关帖子列表。

这样做的最佳实践是什么?也许我错了,有一些更好的方法来组织页面的缓存部分?

PS:抱歉我的英语不好,但我希望它是清楚的。

谢谢你!

Right now I'm trying to create my own tiny MVC (just for practice and for understanding MVC pattern details). I'd like to cache parts of pages (dropdowns, lists etc.) and I don't know what is the best way to organize it.

Let's imagine that I have PostsController with method getPostDetailsShortly($post_id).
This method could look like this...

public function getPostDetailsShortly($post_id) {

  if (!$post_id) return false;
  $content = $this->memcache->get("post" . $post_id); //Trying to get post details HTML from memcache

  if (!$content) { //Not fount in memcache

    $model = new PostsModel();
    $data = $model->getPostDetailsShortly($post_id);

    $this->view->assign('data', $data);

    ob_start();
    $this->view->render();
    $content = ob_get_contents(); //Getting view output into variable
    ob_end_clean();

    $this->memcache->set('post' . $post_id, $content, 1000); //Writing variable to memcache

  }

  return $content;

}

Now I should make this controller method be available from Views. Because I will use it inside of other pages, for example, for building related posts list.

What is the best practice of doing it? Maybe I'm wrong and there are some better methods to organize caching parts of pages?

PS:Sorry for my English, but I hope it is clear.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2024-08-31 10:51:40

对于那些可能不知道的读者来说,MVC 背后的想法是分离站点的模型、视图和控制器架构。我最熟悉的是CakePHP MVC框架。所以这个答案将基于我对 MVC 的了解,因为它与 CakePHP 相关。

由于您提供的信息需要提供给视图,因此我建议将其构建为 HTML 帮助器。 Helper 旨在为视图提供可重用的代码。控制器是代码背后逻辑的实现(如何处理来自视图上表单的数据、调用哪些视图、向模型请求数据等)。

查看您正在谈论的有关缓存这些项目的问题。我认为如果您预计会获得大量流量,这是一件好事,但否则就没有必要。话虽如此,问题似乎更多地与架构有关,而不是与缓存有关。当前的架构似乎很合理,但我会将功能移至可从所有控制器访问的“组件”中。这将允许您从任何需要的控制器调用它,而不必每次都调用特定的控制器。

The idea behind MVC for those who may be reading this who don't know, is to separate the MODEL, VIEW, and CONTROLLER architecture of the site. I am most familiar with CakePHP MVC framework. So this answer will be based on my knowledge of MVC as it pertains to CakePHP.

Since the information you are providing needs to be provided to the view, I would suggest building it as an HTML helper. A Helper is designed to provide reusable code to the view. The Controller is the implementation of the logic behind the code (what to do with the data coming from the forms on the view, what views to call, asking the model for data, etc.).

Looking over the question you are speaking about caching these items. I think it is a good thing if you anticipate getting large amounts of traffic, but not really necessary otherwise. Having said that, it seems the question is more about the architecture than the caching. The current architecture seems sound, but I would move the functionality into a "component" which is accessible from all Controllers. This will allow you to call it from any controller where you need it, without having to call a specific controller every time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文