Cache & relationships

Why does if I set cache lifetime and then get related models than cache doesn't work? example:

$users = User::find([
    'cache' => ['lifetime' => 3600, 'key' => 'top-users'],
    'limit' => 100,
]);

some view:

foreach ($users as $user) {
   echo 
   "<tr>",
   "<td>", $user->name, "</td>",
   "<td>", $user->countAvatar() ? $user->avatar->src : "", "</td>",
   "</tr>";
}

With related model Avatar view every time loads longest (few seconds) that should be with cache.



85.4k

User::find() is doing a query only over a table: SELECT * FROM Users, how the related records could be cached if they are not part of the query?



23.0k

Ok, I see. Just thought that ORM has like functional for relations. Because of it makes no sense no cache User if there'll be db-request for Avatar for any single $user->avatar. Will try to do it another way, by caching results separately from built-in caching, for example.



85.4k

I added a dedicated chapter to the docs deepening in this issue, hope this help you

http://docs.phalconphp.com/en/1.0.0/reference/models-cache.html



23.0k

Thanks! It become very useful in understanding of Phalcon.