Yii2:如何缓存活动数据提供者? [英] Yii2 : how to cache active data provider?

查看:78
本文介绍了Yii2:如何缓存活动数据提供者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的PostSearch模型中,我有以下代码:

 公共功能搜索($ params)
{
$ query = Post :: find()-> where(['status'=> 1]);

$ dataProvider = new ActiveDataProvider([
'query'=> $ query,
'sort'=> ['defaultOrder'=> ['id'= > SORT_DESC]],
'pagination'= >> [
'pageSize'=> 10,
]
]);

if(!($ this-> load($ params)&& $ this-&validate(())){
return $ dataProvider;
}

$ query-∧ FilterWhere([
'id'=> $ this-> id,
'status'=> $ this ->状态,
]);

$ query-> andFilterWhere(['like','title',$ this-> title])
-> andFilterWhere(['like','text', $ this-> text]);

return $ dataProvider;

我的尝试,而不是上面的行 return $ dataProvider ,将是以下代码块:

  $ dependency = [
'class'=> ‘yii\缓存\DbDependency’,
‘sql’=> ‘SELECT MAX(updated_at)FROM post',
];

$ result = self :: getDb()-> cache(函数($ db){
return $ dataProvider;
},3600,$ dependency);

return $ result

我想缓存ADP返回的结果,基于updated_at字段。我的意思是我想从缓存中提供数据,直到进行一些更改。我的代码不起作用,我的意思是根本不应用缓存。我做错了,是否有可能在ADP上执行此操作?谢谢

解决方案

实例化后,几乎没有用缓存数据提供程序,因为它直到对数据库进行处理才真正对数据库进行任何选择准备好了。因此,实际上您将像现在这样缓存一个空的对象实例。



如果您有大量记录,请调用dataProviders的在缓存中预先准备()

  self :: getDb()->缓存(函数($ db)使用($ dataProvider){
$ dataProvider-> prepare();
},3600,$ dependency);
return $ dataProvider;

这实际上将缓存 dataProvider 的任何查询运行,因此下次将它们从查询缓存中提取。



如果记录数量有限,一次将它们全部缓存也可以:

  $ key ='MyCachedData'; // +唯一引用您的搜索参数的数据
$ cache = \Yii :: $ app-> cache;
$ dataProvider = $ cache-> get($ key);
if(!$ dataProvider){
$ dependency = \Yii :: createObject([
'class'=>'yii\caching\DbDependency',
'sql'=>'SELECT MAX(updated_at)FROM post',
]);

$ dataProvider =新的yii\data\ArrayDataProvider;
$ dataProvider-> allModels = $ query-> all();
$ cache-> set($ key,$ dataProvider,3600,$ dependency)
}
return $ dataProvider;

显然,这对于较大的数据集来说并不理想,但是这取决于您要查找的内容。 / p>

In my PostSearch model I have this code :

public function search($params)
{
    $query = Post::find()->where(['status' => 1]);

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort'=> ['defaultOrder' => ['id' => SORT_DESC]],
        'pagination' => [
            'pageSize' => 10,
        ]
    ]);

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        'id' => $this->id,
        'status' => $this->status,
    ]);

    $query->andFilterWhere(['like', 'title', $this->title])
        ->andFilterWhere(['like', 'text', $this->text]);

    return $dataProvider;

my try, instead of above line return $dataProvider, would be this block of code:

$dependency = [
    'class' => 'yii\caching\DbDependency',
    'sql' => 'SELECT MAX(updated_at) FROM post',
];

$result = self::getDb()->cache(function ($db) {
    return $dataProvider;
}, 3600, $dependency);

return $result

I would like to cache the result returned by ADP, based on the updated_at field. I mean I want to serve data from cache until some change is made. My code does not work, I mean caching is not applied at all. What I am doing wrong, and is it possible to do this on ADP ? Thanks

解决方案

It has little use caching the data provider after instantiating, since it's not actually doing any selecting on the database until it has been prepared. So you would actually be caching an empty object instance like it is now.

If you have a very large set of records, call the dataProviders' prepare() in advance in the cache:

 self::getDb()->cache(function ($db) use ($dataProvider) {
     $dataProvider->prepare();
 }, 3600, $dependency);
 return $dataProvider;

This will actually cache whatever queries the dataProvider runs ,so the next time they will be fetched from the query cache. This should result in what you are looking for.

If you have a finite amount of records, caching them all at once could also work:

$key = 'MyCachedData'; // + Data uniquely referring to your search parameters
$cache = \Yii::$app->cache;
$dataProvider = $cache->get($key);
if (!$dataProvider) {
   $dependency = \Yii::createObject([
      'class' => 'yii\caching\DbDependency',
      'sql' => 'SELECT MAX(updated_at) FROM post',
   ]);

   $dataProvider = new \yii\data\ArrayDataProvider;
   $dataProvider->allModels = $query->all();
   $cache->set($key, $dataProvider, 3600, $dependency) 
} 
return $dataProvider;

Obviously this is less than ideal for larger datasets, but it depends on what you are looking for.

这篇关于Yii2:如何缓存活动数据提供者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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