laravel-model-caching
laravel-model-caching copied to clipboard
Add tests and functionality for `model::cursor()` method.
- https://www.youtube.com/watch?v=Hfgcg09srSo at around 35:00
@mikebronner I have had a look at this and it seems that it completely skips caching as it uses Query\Builder
under the hood to execute the query for each record.
Eloquent\Builder
public function cursor()
{
// Swaps to using QueryBuilder ⬇️ then ⬇️ creates `LazyCollection`
return $this->applyScopes()->query->cursor()->map(function ($record) {
return $this->newModelInstance()->newFromBuilder($record);
});
}
Query\Builder
public function cursor()
{
if (is_null($this->columns)) {
$this->columns = ['*'];
}
return new LazyCollection(function () {
yield from $this->connection->cursor(
$this->toSql(), $this->getBindings(), ! $this->useWritePdo
);
});
}
So you could say use of cursor
is not cached. The only other option would be write your own implementation of the cursor
method
In my opinion it is probably not necessary to cache the cursor
function since it is generally used for large data quantities. So if you cached each if the individual record queries it could put quite some load on the cache instance.
@dmason30 Good analysis! I add this to the documentation that this method is not cached.