Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy query cache from yii\db\Command to yii\db\Query::createCommand() and yii\db\ActiveQuery::createCommand(). #15398

Merged

Conversation

hubeiwei
Copy link
Contributor

@hubeiwei hubeiwei commented Dec 21, 2017

Q A
Is bugfix? no
New feature? yes
Breaks BC? no
Tests pass? yes
Fixed issues -

@samdark samdark added the pr:missing usecase It is not clear what is the use case for the pull request. label Dec 21, 2017
@yii-bot
Copy link

yii-bot commented Dec 21, 2017

Thank you for putting effort in the improvement of the Yii framework.
We have reviewed your pull request.

Unfortunately a use case is missing. It is required to get a better understanding of the pull request and helps us to determine the necessity and applicability of the suggested change to the framework.

Could you supply us with a use case please? Please be as detailed as possible and show some code!

Thanks!

This is an automated comment, triggered by adding the label pr:missing usecase.

@hubeiwei
Copy link
Contributor Author

hubeiwei commented Dec 22, 2017

Command will check the cache in the query, Query should be able to set the cache

(new Query())->cache(7200)->all();
User::find()->cache(7200)->all();

@hubeiwei hubeiwei changed the title Copy query cache from yii\db\Command to yii\db\Query::createCommand() and yii\db\Active::createCommand(). Copy query cache from yii\db\Command to yii\db\Query::createCommand() and yii\db\ActiveQuery::createCommand(). Dec 22, 2017
@samdark samdark added this to the 2.0.14 milestone Dec 22, 2017
@samdark
Copy link
Member

samdark commented Feb 5, 2018

I think http://www.yiiframework.com/doc-2.0/guide-caching-data.html#query-caching is enough as a way to achieve it.

@yiisoft/reviewers, @yiisoft/core-developers need opinions.

@samdark samdark removed the pr:missing usecase It is not clear what is the use case for the pull request. label Feb 5, 2018
@hubeiwei
Copy link
Contributor Author

hubeiwei commented Feb 6, 2018

Yes,I think you can write less code in this way.

@rob006
Copy link
Contributor

rob006 commented Feb 6, 2018

@samdark Is there any reason to not implementing it? IMO it may be useful and will improve consistency.

@samdark
Copy link
Member

samdark commented Feb 6, 2018

Two reasons:

  1. Another way to do the same thing.
  2. We have to support it.

@hubeiwei
Copy link
Contributor Author

hubeiwei commented Feb 7, 2018

yii\db\Connection::enableQueryCache defaults to true, yii\db\Command will first check the cache before query, even if you don't use it, I think it is meaningful to do so.

@rob006
Copy link
Contributor

rob006 commented Feb 7, 2018

@samdark yii\db\Command already has such method - it is quite weird that low-level DB component (Command) has such shortcut and high-level DB component (Query) does not.

@samdark
Copy link
Member

samdark commented Feb 7, 2018

@rob006 the idea of having it only at connection level was essentially that it can wrap anything and there's no need for more ways to handle it.

@rob006
Copy link
Contributor

rob006 commented Feb 7, 2018

the idea of having it only at connection level

The idea seems to be dropped, since we have:

public function cache($duration = null, $dependency = null)

@samdark
Copy link
Member

samdark commented Feb 7, 2018

While I personally don't like the idea much, if you think it's fine I'm OK merging it.

@cebe, @SilverFire, @klimov-paul thoughts?

@SilverFire
Copy link
Member

We already have 2 ways to cache a query:

$db->cache(function($db) {
    return User::find()->all();
});
$cache->getOrSet(['meaningful-key'], function () {
    return User::find()->all();
})

But the suggested one can be a shorter and more convenient.

I'd suggest to move properties queryCacheDuration and queryCacheDependency together with cache(), noCache() and hasCache() methods to the CacheableQueryTrait, that can be used in Query and Command.

Copy link
Member

@SilverFire SilverFire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above. Otherwise good to go

@hubeiwei
Copy link
Contributor Author

hubeiwei commented Feb 8, 2018

@SilverFire Command::cache() have default cache duration, through $this->db->queryCacheDuration, but Query no $db this member variables. if want to use CacheableQueryTrait, I think we should give it up.

@SilverFire SilverFire merged commit a036fac into yiisoft:master Feb 10, 2018
@rob006
Copy link
Contributor

rob006 commented Feb 10, 2018

@SilverFire Why did you merge this without any tests? I'm pretty sure that this will not work as expected: MyModel::find()->cache()->one($myCustomDb). Default queryCacheDuration will be taken from Yii::$app->db instead of $myCustomDb.

Also hasCache() is misleading, since it does not say whether the query has cache, it only says that cache is enabled. I'm also not sure why it is public or whether implementation is correct (does it make sense do enable cache with queryCacheDuration === null but queryCacheDependency !== null?).

@SilverFire
Copy link
Member

It was accidentally, I'm now working on fix

@SilverFire
Copy link
Member

See 5bd6ed5

@rob006
Copy link
Contributor

rob006 commented Feb 10, 2018

I would keep cache() API consistent with Command::cache() and use $duration === null as "use Connection::$queryCacheDuration". We could just add private Query::$isCacheEnabled and pass $queryCacheDuration and $queryCacheDependency directly to command - it will simplify the code and API.

@hubeiwei hubeiwei deleted the copy-query-cache-from-command-to-query branch February 16, 2018 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants