E
E
ErickSkrauch2015-09-05 23:10:09
Yii
ErickSkrauch, 2015-09-05 23:10:09

Implement global data caching in Yii2?

I plan to rewrite the project from PhalconPHP to Yii2. Without going into details, why I'm changing the framework, I'll ask this:
How can Yii2 implement global entity caching?
Explanation:
Take the entity User . The User has a Skin avatar . They are connected. Skin has an uploader, i.e. the one who uploaded it and, oddly enough, is User . After that, we have more complex entities: Comment and Post (in fact, there are more of them). Comment contains the author, and for output to the frontend (or response via API), we always need Skin as well, because how can we do without an avatar. EssencePost has the same author and all the same data is needed for normal operation. From this it turns out that for the selection of posts will always be used . And this means that we will constantly have the following requests:Post::find()->with('author', 'author.skin')->all()

SELECT * FROM posts
SELECT * FROM users WHERE id IN (...)
SELECT * FROM skins WHERE id IN (...)

And the problem could be somehow solved if we simply added ->cache() in the link declaration (the same getter that is returned by ActiveQuery), but this will be a cache only for a strictly matching set of id, which, in fact , meaningless.
While my project is running on PhalconPHP, I wrote an external class that does something like this: looks for the passed entity ids in the cache, which can pull from there, and selects and caches the rest. I also have a global tagging system, caching at the findFirst() level and the like, but still, to be honest, this is more like a redundant code than a "cool solution".
So, I will continue about the task: the site also has a separate page for the avatar and the user. And these entities are also used there, right by id. That is, it turns out that, in essence, the entities are always the same, and due to cache tagging (which is still a mystery to me in Yii2), the ttl of the cache can be close to infinity.
From the foregoing, the following questions (more specific) follow:
  • How to implement caching for findFirst() by entity and tags? Here, of course, the solution suggests itself quite obvious, but still someone will offer a very beautiful option.
  • How to implement a smarter selection of related data, namely, checking the entities in the cache, additional selection of the remaining ones and their further caching?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Makarov, 2015-09-07
@SamDark

1. There is cache tagging. See yii\caching\TagDependency.
2. Look at www.yiiframework.com/doc-2.0/guide-db-active-recor...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question