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
Contributor

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!

@JosephSilber JosephSilber force-pushed the JosephSilber:lazy-collection-remember branch from 54ae1cc to 903a132 Oct 28, 2019
@JosephSilber JosephSilber force-pushed the JosephSilber:lazy-collection-remember branch 2 times, most recently from dd4f7bd to 3c0afdd Oct 28, 2019
@JosephSilber JosephSilber force-pushed the JosephSilber:lazy-collection-remember branch from 3c0afdd to e720eba Oct 28, 2019
@taylorotwell

This comment has been minimized.

Copy link
Member

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

@danilopinotti

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Contributor

tobiasthaden commented Oct 29, 2019

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

@36864

This comment has been minimized.

Copy link
Contributor

36864 commented Oct 29, 2019

How about just ->lazy()?

@narwy

This comment has been minimized.

Copy link

narwy commented Oct 29, 2019

partition() ? I love this idea !

@Arkanius

This comment has been minimized.

Copy link

Arkanius commented Oct 30, 2019

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

@netpok

This comment has been minimized.

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
2 checks passed
2 checks passed
continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@taylorotwell

This comment has been minimized.

Copy link
Member

taylorotwell commented Oct 31, 2019

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

@driesvints

This comment has been minimized.

Copy link
Member

driesvints commented Oct 31, 2019

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

kennith added a commit to kennith/docs that referenced this pull request Nov 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.