Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Add LazyCollection@remember method #30443

Merged
merged 1 commit into from
Oct 31, 2019

Conversation

JosephSilber
Copy link
Member

@JosephSilber JosephSilber commented Oct 28, 2019

This PR adds a remember method to the LazyCollection class.

Calling remember returns a new lazy collection that will remember any values that are enumerated, and will not pull it again from the source when it's enumerated again. Here are two examples:

Example 1

$users = User::cursor()->remember();

// No query has been executed yet.

$users->all();

// All values have been pulled from the DB.

$users->all();

// We did not hit the DB again. We got the users from `remember`'s cache.

Example 2

$users = User::cursor()->remember();

// No query has been executed yet.

$users->take(5)->all();

// The query has been executed, and the first 5 users have been streamed from the DB.

$users->take(20)->all();

// The first 5 users came from the cache. The rest continued the stream from the DB.

This PR includes extensive tests ensuring that it's fully lazy, and that two simultaneous runners don't step on each other's toes.


The partition method

After this is merged, I'll rewrite the partition method using this.

Currently, calling partition immediately enumerates the whole collection, since we had no way to create two streams for the same dataset.

Now with remember, we can get a truly lazy version of partition!

@taylorotwell
Copy link
Member

Just being honest these method names on LazyCollection seem so hard to figure out. When I see "remember" i think it has something to do with Laravel's caching system.

@danilopinotti
Copy link
Contributor

danilopinotti commented Oct 29, 2019

Just being honest these method names on LazyCollection seem so hard to figure out. When I see "remember" i think it has something to do with Laravel's caching system.

What about 'keep' or 'retain'?

@tobiasthaden
Copy link
Contributor

Obviously a breaking change but – what about make remember the default and add a fresh() method?

@Miguel-Serejo
Copy link

How about just ->lazy()?

@n10000k
Copy link

n10000k commented Oct 29, 2019

partition() ? I love this idea !

@Arkanius
Copy link
Contributor

I like the name "remember" because I can figure out that this is using cache like the functions of cache system

@netpok
Copy link
Contributor

netpok commented Oct 30, 2019

@tobiasthaden I think that would lead to a lot of confusion, and in my case I would probably write fresh more than remember with the current implementation.

@36864 It's an already existing method on the collection so that's not a good solution in my opinion.

collect()->lazy()->lazy();

@narwy That's already an [existing method])https://laravel.com/docs/6.x/collections#method-partition)

I like the remember, I don't see the reason to use different names in different parts in the software, just like we don't use different methods for dispatching a job or an event.

I want cache to remember something: cache()->remember();
I want the collection to remember somethin: $collection->remember()

@taylorotwell taylorotwell merged commit 70cb9f3 into laravel:6.x Oct 31, 2019
@taylorotwell
Copy link
Member

I'll add it but I can't say that I understand it, heh. 😬

@driesvints
Copy link
Member

@JosephSilber thanks! Can you also send in a pr to the docs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants