## 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...
~~~
- 基本说明
- 基本操作
- October cms 安装
- 后台控制器路径
- 图标
- 获取安装网上的插件/主题
- 插件构造器使用
- 定时任务
- October后台控制器
- vscode编辑器
- ajax操作
- 使用
- ajax更新组件
- ajax属性API
- JavaScript API
- ajax综合使用
- 主题
- 多语言主题
- 安装市场主题
- 主题程序处理
- 主题
- 页面
- 部件
- 布局
- 内容
- 组件
- 媒体
- 主题表单操作
- 表单使用
- 表单后端程序处理
- 插件
- 自定义插件
- 插件说明
- 插件导航条
- 插件数据库设置
- 插件的设置管理
- 插件的配置文件config
- 组件
- app服务
- app容器
- 扩展行为
- 缓存
- Collection类
- Lazy Collections
- Collection方法
- 助手函数
- 数组助手函数
- 路径助手函数
- 玄乐助手函数
- 其他助手函数
- 错误与记录
- 事件处理
- HTML页面
- 文件与目录操作
- 散列和加密
- 邮件
- 邮件内容
- 邮件发送
- 分页
- 模板解析器
- 动态解析器语法
- 队列消息
- 请求与输入
- 响应
- 视图
- 路由器
- 配置
- 验证操作
- 处理错误消息
- 错误消息与视图
- 可用的验证规则
- 有条件的验证规则
- 验证数组
- 错误消息
- 自定义验证规则
- 模型操作
- 定义模型与其属性
- 检索模型
- 插入与更新
- 删除模型
- 查询范围
- 事件操作
- 关联操作
- 定义关系
- 关系类型
- 多肽关系
- 关系查询
- 渴望加载
- 插入模型
- 数据库操作
- 基本用法
- 数据表结构
- 查询连贯操作
- 结果检索
- select子句
- 插入更新
- where子句
- 排序,分组,限制和偏移
- 文件附件
- Collection操作
- 属性操作
- 系列化json
- 数据库属性
- 数据库行为
- 控制器
- 后台控制器定义
- 后台页面
- 后台组件
- 后台表单
- 表单组件
- 表单视图
- 表单行为
- 后台列表
- 列表行为
- 列表过滤器
- 可用列类型
- 关系行为
- 关系行为类型
- 扩展关系行为
- 列表排序操作
- 导入导出操作
- 用于与权限
- corlate模板修改
- 修改顶部导航
- laravel问题
- 控制器不存在
- 控制器
- 路由组
- laravel笔记
- laravel 安装
- 伪静态配置
- 依赖注入 & 控制器
- 中间件
- 路由文件
- 视图