Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How do i use cache in ORM?

I want to use cache in the Model class deriectly.How do i write the code ?



2.7k

Yes.I read that, but i am a little confused about 'modelsCache' service. Once you setup that service, how do i destroy cache when i update or delete models.Use events like beforeSave? And i want use \Phalcon\Cache\Backend\File CLASS in the Model's STATIC function find and findFirst, how do i setup that. @Karol Marcinkiewicz you're a kind person, thanks a lot. These questions are too naive, THANKS you again for your patient.



29.8k
Accepted
answer
edited Oct '14

Think if you really need to cache ORM, maybe caching view layer will work better? Your cache key could be md5($SERVER['REQUESTURI'])

But if you need to cache ORM, you need to invalidate cache beforeSave(), or beforeUpdate() events like you told. This forum is cached, so best for you would be read source code.

Here is setup for File Backend Cache:
* https://github.com/phalcon/forum/blob/master/app/config/services.php#L205 - registered service in bootstrap
* https://github.com/phalcon/forum/blob/master/app/models/CacheableModel.php - Base model class when you need caching * Look at * https://github.com/phalcon/forum/blob/master/app/models/PostsReplies.php#L160
* https://github.com/phalcon/forum/blob/master/app/models/PostsReplies.php#L176
* https://github.com/phalcon/forum/blob/master/app/models/PostsReplies.php#L217

Create your application that it works properly without caching, and then if it is needed fix bottleneck by cache views, views parts and ORM