现在我正在尝试创建自己的微型 MVC(只是为了练习和理解 MVC 模式细节)。我想缓存部分页面(下拉列表、列表等),但我不知道组织它的最佳方式是什么。
假设我有 PostsController 和 getPostDetailsShortly($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;
}
现在我应该让这个控制器方法在 Views 中可用。因为我将在其他页面中使用它,例如,用于构建相关的帖子列表。
这样做的最佳做法是什么?也许我错了,有一些更好的方法来组织页面的缓存部分?
PS:对不起我的英语,但我希望它很清楚。
谢谢!