gpt4 book ai didi

caching - 如何在 Phalcon 中使用查询生成器和分页进行缓存?

转载 作者:行者123 更新时间:2023-12-02 03:11:51 25 4
gpt4 key购买 nike

我有一个关于 Phalcon 缓存的问题。它与 Pagination 和 QueryBuilder 相连。所以,我会给你举个例子:

$builder = $this->modelsManager->createBuilder()
->columns('a.*, m.*')
->addFrom('Models\Articles', 'a')
->leftJoin('Models\Multimedia', "m.parent_id=a.id AND m.is_default=1 AND m.subtype='pictures' AND m.type={m_type:str}", 'm')
->where('a.i18n={i18n:str} AND a.is_active_from IS NOT NULL AND a.is_active_from <= {today:str}', [
'i18n' => $this->session->i18n,
'm_type' => 'Models\Articles',
'today' => date("Y-m-d H:i:s")
])
->orderBy('a.is_accent DESC, a.date DESC, a.id DESC');

$paginator = new \Phalcon\Paginator\Adapter\QueryBuilder([
"builder" => $builder,
"limit" => 33,
"page" => $this->request->getQuery('page', 'int')
]);

$this->view->page = $paginator->getPaginate();

现在......如果我们跳过带有查询参数和子句的部分......那么......我的问题是:

如何在此处添加缓存以进行查询?

在 Phalcon 文档中,我只能使用 getQuery 函数添加 ->cache,但在我的情况下 - 这里没有这样的东西。

所以,问题仍然存在:在这种情况下我如何使用缓存?

谢谢

最佳答案

嗯...经过一个星期的思考 xD ...我找到了一些很好的答案来解决我的问题。

首先我们需要在 Phalcon 中有缓存服务:

$di->set('dataCache', function() use ($config) {
$lifetime = 60*60; // 1 hour
if ($config->development) {
$lifetime = 60; // 1 min
}

$frontCache = new \Phalcon\Cache\Frontend\Data([
"lifetime" => $lifetime
]);

$cache = new \Phalcon\Cache\Backend\File($frontCache, [
"cacheDir" => $config->application->cache_dir."data/"
]);

return $cache;
});

现在进入 Controller ,我的分页在哪里(您可以从第一篇文章中进行比较):
     $builder = $this->modelsManager->createBuilder()
->columns('a.id, a.slug, a.is_accent, a.type, a.title, m.*')
->addFrom('Models\Articles', 'a')
->leftJoin('Models\Multimedia', "m.parent_id=a.id AND m.is_default=1 AND m.subtype='pictures' AND m.type={m_type:str}", 'm')
->where('a.i18n={i18n:str} AND a.is_active_from IS NOT NULL AND a.is_active_from <= {today:str}', [
'i18n' => $this->session->i18n,
'm_type' => 'Models\Articles',
'today' => date("Y-m-d H:i:s")
])
->orderBy('a.is_accent DESC, a.date DESC, a.id DESC');

$paginator = new \Phalcon\Paginator\Adapter\QueryBuilder([
"builder" => $builder,
"limit" => 33,
"page" => $this->request->getQuery('page', 'int', 1)
]);

// Cache
$cache_key = 'articles-index-'.$this->request->getQuery('page', 'int', 1);
if ($this->di->has('dataCache') and $this->dataCache->exists($cache_key)) {
$this->view->page = $this->dataCache->get($cache_key);
} else {
$this->view->page = $paginator->getPaginate();

if ($this->di->has('dataCache')) {
$this->dataCache->save($cache_key, $this->view->page);
}
}

所以,重要的部分是 最后几行 在“//缓存”注释之后 :) 我没有缓存查询,我缓存了所有分页器数据。为了优化,最好指定我们需要哪些列(在我的情况下,我有一个包含大量文本的列内容,所以我不需要它)。另一件事 - 当您更改/删除某些元素时,不要忘记刷新缓存(来自示例中的模型)......像这样(在模型中):
public function afterDelete()
{
// Clear articles cache (but first check if we have that service)
if ($this->getDI()->has('dataCache')) {
$this->getDI()->get('dataCache')->flush();
}
}

就是这样。从 127 毫秒的请求开始,现在我得到了 40 毫秒或更短的时间:)。祝大家好运:)

一些有用的链接: Phalcon Models Cache , Improving Performance in Phalcon , Phalcon File Cache

关于caching - 如何在 Phalcon 中使用查询生成器和分页进行缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39558698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com