企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## Lazy Collections ### 介绍 > **注意:**在详细了解October的惰性集合之前,请花一些时间来熟悉[PHP生成器](https://www.php.net/manual/en/language.generators.overview.php)。 为了补充已经强大的`Collection`类,`LazyCollection`该类利用PHP的[生成器](https://www.php.net/manual/en/language.generators.overview.php)来允许您处理非常大的数据集,同时保持较低的内存使用率。 例如,假设您的应用程序需要处理一个千兆字节的日志文件,同时利用October的收集方法来解析日志。懒惰的收集可以用来在给定的时间仅将一小部分文件保留在内存中,而不是一次将整个文件读入内存: ~~~ use App\LogEntry; use Illuminate\Support\LazyCollection; LazyCollection::make(function () { $handle = fopen('log.txt', 'r'); while (($line = fgets($handle)) !== false) { yield $line; } })->chunk(4)->map(function ($lines) { return LogEntry::fromLines($lines); })->each(function (LogEntry $logEntry) { // Process the log entry... }); ~~~ 或者,假设您需要遍历10,000个Eloquent模型。使用传统的October集合时,必须同时将所有10,000个Eloquent模型加载到内存中: ~~~ $users = App\User::all()->filter(function ($user) { return $user->id > 500; }); ~~~ 但是,查询生成器的`cursor`方法返回一个`LazyCollection`实例。这样,您仍然可以只对数据库运行单个查询,也可以一次仅将一个Eloquent模型加载到内存中。在此示例中,在`filter`我们实际逐个迭代每个用户之前,不会执行回调,从而大大减少了内存使用量: ~~~ $users = App\User::cursor()->filter(function ($user) { return $user->id > 500; }); foreach ($users as $user) { echo $user->id; } ~~~ ### [](https://octobercms.com/docs/services/collections#creating-lazy-collections)创建惰性集合 要创建一个懒惰的集合实例,您应该将PHP生成器函数传递给集合的`make`方法: ~~~ use Illuminate\Support\LazyCollection; LazyCollection::make(function () { $handle = fopen('log.txt', 'r'); while (($line = fgets($handle)) !== false) { yield $line; } }); ~~~ ### [](https://octobercms.com/docs/services/collections#the-enumerable-contract)无数合同 `Collection`该类上几乎所有可用的方法也都可用`LazyCollection`。这两个类都实现了`Illuminate\Support\Enumerable`合同,该合同定义了以下方法: [所有](https://octobercms.com/docs/services/collections#method-all)[平均](https://octobercms.com/docs/services/collections#method-average)[平均](https://octobercms.com/docs/services/collections#method-avg)[大块](https://octobercms.com/docs/services/collections#method-chunk)[崩塌](https://octobercms.com/docs/services/collections#method-collapse)[收集](https://octobercms.com/docs/services/collections#method-collect)[相结合](https://octobercms.com/docs/services/collections#method-combine)[的concat](https://octobercms.com/docs/services/collections#method-concat)[包含](https://octobercms.com/docs/services/collections#method-contains)[containsStrict](https://octobercms.com/docs/services/collections#method-containsstrict)[计数](https://octobercms.com/docs/services/collections#method-count)[countBy](https://octobercms.com/docs/services/collections#method-countBy)[CROSSJOIN](https://octobercms.com/docs/services/collections#method-crossjoin)[DD](https://octobercms.com/docs/services/collections#method-dd)[差异](https://octobercms.com/docs/services/collections#method-diff)[diffAssoc](https://octobercms.com/docs/services/collections#method-diffassoc)[diffKeys](https://octobercms.com/docs/services/collections#method-diffkeys)[转储](https://octobercms.com/docs/services/collections#method-dump)[重复](https://octobercms.com/docs/services/collections#method-duplicates)[duplicatesStrict](https://octobercms.com/docs/services/collections#method-duplicatesstrict)[每个](https://octobercms.com/docs/services/collections#method-each)[eachSpread](https://octobercms.com/docs/services/collections#method-eachspread)[每](https://octobercms.com/docs/services/collections#method-every)[除了](https://octobercms.com/docs/services/collections#method-except)[过滤器](https://octobercms.com/docs/services/collections#method-filter)[第一](https://octobercms.com/docs/services/collections#method-first)[firstWhere](https://octobercms.com/docs/services/collections#method-first-where)[flatMap](https://octobercms.com/docs/services/collections#method-flatmap)[压扁](https://octobercms.com/docs/services/collections#method-flatten)[翻转](https://octobercms.com/docs/services/collections#method-flip)[forPage](https://octobercms.com/docs/services/collections#method-forpage)[GET](https://octobercms.com/docs/services/collections#method-get)[GROUPBY](https://octobercms.com/docs/services/collections#method-groupby)[有](https://octobercms.com/docs/services/collections#method-has)[破灭](https://octobercms.com/docs/services/collections#method-implode)[相交](https://octobercms.com/docs/services/collections#method-intersect)[intersectByKeys](https://octobercms.com/docs/services/collections#method-intersectbykeys)[的isEmpty](https://octobercms.com/docs/services/collections#method-isempty)[isNotEmpty](https://octobercms.com/docs/services/collections#method-isnotempty)[加入](https://octobercms.com/docs/services/collections#method-join)[keyBy](https://octobercms.com/docs/services/collections#method-keyby)[键](https://octobercms.com/docs/services/collections#method-keys)[最后](https://octobercms.com/docs/services/collections#method-last)[宏观](https://octobercms.com/docs/services/collections#method-macro)[化妆](https://octobercms.com/docs/services/collections#method-make)[地图](https://octobercms.com/docs/services/collections#method-map)[mapInto](https://octobercms.com/docs/services/collections#method-mapinto)[mapSpread](https://octobercms.com/docs/services/collections#method-mapspread)[mapToGroups](https://octobercms.com/docs/services/collections#method-maptogroups)[mapWithKeys](https://octobercms.com/docs/services/collections#method-mapwithkeys)[最大](https://octobercms.com/docs/services/collections#method-max)[位数](https://octobercms.com/docs/services/collections#method-median)[合并](https://octobercms.com/docs/services/collections#method-merge)[mergeRecursive](https://octobercms.com/docs/services/collections#method-mergerecursive)[分钟](https://octobercms.com/docs/services/collections#method-min)[模式](https://octobercms.com/docs/services/collections#method-mode)[第n](https://octobercms.com/docs/services/collections#method-nth)[只](https://octobercms.com/docs/services/collections#method-only)[垫](https://octobercms.com/docs/services/collections#method-pad)[分区](https://octobercms.com/docs/services/collections#method-partition)[管](https://octobercms.com/docs/services/collections#method-pipe)[勇气](https://octobercms.com/docs/services/collections#method-pluck)[随机](https://octobercms.com/docs/services/collections#method-random)[降低](https://octobercms.com/docs/services/collections#method-reduce)[拒绝](https://octobercms.com/docs/services/collections#method-reject)[更换](https://octobercms.com/docs/services/collections#method-replace)[replaceRecursive](https://octobercms.com/docs/services/collections#method-replacerecursive)[反向](https://octobercms.com/docs/services/collections#method-reverse)[搜索](https://octobercms.com/docs/services/collections#method-search)[洗牌](https://octobercms.com/docs/services/collections#method-shuffle)[跳过](https://octobercms.com/docs/services/collections#method-skip)[切片](https://octobercms.com/docs/services/collections#method-slice)[一些](https://octobercms.com/docs/services/collections#method-some)[排序](https://octobercms.com/docs/services/collections#method-sort)[sortBy](https://octobercms.com/docs/services/collections#method-sortby)[sortByDesc](https://octobercms.com/docs/services/collections#method-sortbydesc)[SORTKEYS](https://octobercms.com/docs/services/collections#method-sortkeys)[sortKeysDesc](https://octobercms.com/docs/services/collections#method-sortkeysdesc)[分裂](https://octobercms.com/docs/services/collections#method-split)[和](https://octobercms.com/docs/services/collections#method-sum)[取](https://octobercms.com/docs/services/collections#method-take)[自来水](https://octobercms.com/docs/services/collections#method-tap)[倍](https://octobercms.com/docs/services/collections#method-times)[指定者](https://octobercms.com/docs/services/collections#method-toarray)[的toJSON](https://octobercms.com/docs/services/collections#method-tojson)[工会](https://octobercms.com/docs/services/collections#method-union)[独特](https://octobercms.com/docs/services/collections#method-unique)[uniqueStrict](https://octobercms.com/docs/services/collections#method-uniquestrict)[除非](https://octobercms.com/docs/services/collections#method-unless)[unlessEmpty](https://octobercms.com/docs/services/collections#method-unlessempty)[unlessNotEmpty](https://octobercms.com/docs/services/collections#method-unlessnotempty)[解包](https://octobercms.com/docs/services/collections#method-unwrap)[的值](https://octobercms.com/docs/services/collections#method-values)[时](https://octobercms.com/docs/services/collections#method-when)[whenEmpty](https://octobercms.com/docs/services/collections#method-whenempty)[whenNotEmpty](https://octobercms.com/docs/services/collections#method-whennotempty)[其中](https://octobercms.com/docs/services/collections#method-where)[whereStrict](https://octobercms.com/docs/services/collections#method-wherestrict)[whereBetween](https://octobercms.com/docs/services/collections#method-wherebetween)[其中](https://octobercms.com/docs/services/collections#method-wherein)[whereInStrict](https://octobercms.com/docs/services/collections#method-whereinstrict)[whereInstanceOf](https://octobercms.com/docs/services/collections#method-whereinstanceof)[whereNotBetween](https://octobercms.com/docs/services/collections#method-wherenotbetween)[whereNotIn](https://octobercms.com/docs/services/collections#method-wherenotin)[whereNotInStrict](https://octobercms.com/docs/services/collections#method-wherenotinstrict)[涡卷](https://octobercms.com/docs/services/collections#method-wrap)[拉链](https://octobercms.com/docs/services/collections#method-zip) > **注:**方法是变异的集合(如`shift`,`pop`,`prepend`等)都*无法*提供的`LazyCollection`类。 ### [](https://octobercms.com/docs/services/collections#lazy-collection-methods)延迟收集方法 除了`Enumerable`合同中定义的方法外,`LazyCollection`该类还包含以下方法: #### [](https://octobercms.com/docs/services/collections#method-tapEach)`tapEach()` 尽管该`each`方法立即为集合中的每个项目调用给定的回调,但是该`tapEach`方法仅调用给定的回调,因为这些项目正被逐一从列表中拉出: ~~~ $lazyCollection = LazyCollection::times(INF)->tapEach(function ($value) { dump($value); }); // Nothing has been dumped so far... $array = $lazyCollection->take(3)->all(); ~~~ #### [](https://octobercms.com/docs/services/collections#method-remember)`remember()` 该`remember`方法返回一个新的惰性集合,该集合将记住已枚举的所有值,并且在再次枚举该集合时将不会再次检索它们: ~~~ $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 hydrated from the database... $users->take(20)->all(); // First 5 users come from the collection's cache... The rest are hydrated from the database... ~~~