We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



98.9k

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?



32.5k

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.



98.9k

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

https://docs.phalcon.io/en/1.0.0/reference/models-cache.html



32.5k

Thanks! It become very useful in understanding of Phalcon.