mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.26k stars 217 forks source link

Add tests and functionality for `model::cursor()` method. #310

Open mikebronner opened 4 years ago

mikebronner commented 4 years ago
dmason30 commented 4 years ago

@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.

mikebronner commented 4 years ago

@dmason30 Good analysis! I add this to the documentation that this method is not cached.