CakePHP 3:带缓存的 find() [英] CakePHP 3: find() with cache

查看:12
本文介绍了CakePHP 3:带缓存的 find()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 get() 方法,我阅读了 这里:

About the get() method, I read here:

像 find() get 集成了缓存.您可以在调用 get() 时使用缓存选项来执行读取缓存

Like find() get has caching integrated. You can use the cache option when calling get() to perform read-through caching

但稍后,在专用于 find() 方法的部分(这里),缓存没有提到,没有缓存和缓存的例子支持的选项中没有提到选项.

But later, in the section dedicated to the find() method (here), the cache is not mentioned, there are no examples for the cache and the cache option is not mentioned among the supported options.

所以我想知道:我可以在 find() 方法中使用 cache 选项吗?如果是,怎么办?

So I would like to know: can I use the cache option with the find() method? If so, how?

谢谢.

感谢 ndm.所以:

$query = $this->Pages->find('all');
$query->cache('all_pages');

$this->set('pages', $query->all());

或者(更简单):

$query = $this->Pages->find('all')->cache('all_pages');

$this->set('pages', $query->all());

推荐答案

查询构建器不支持通过选项缓存,它有一个单独的方法可以使用,Query::cache()代码>,你喜欢使用

The query builder doesn't suport caching via options, it has a separate method that is to be used, Query::cache(), which you'd use like

$query = $table->find();
$query->cache('cache_key');

$query->cache('cache_key', 'configName');

$query->cache(function($query) {
    return md5(
        serialize($query->clause('select')) .
        serialize($query->clause('where'))
    );
});

// ...

get() 支持通过选项缓存,因为这是配置内部查找调用的唯一方法,因为它会立即执行,以便 get() 可以返回 throw可能的错误并返回一个实体.

get() supports caching via an option as that's the only way to configure the internal find call, since it's being executed immediately so that get() can return throw possible errors and return an entity.

这篇关于CakePHP 3:带缓存的 find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆