overblog/dataloader-bundle
Composer 安装命令:
composer require overblog/dataloader-bundle
包简介
DataLoader Symfony bundle implementation.
关键字:
README 文档
README
This bundle allows to easy use DataLoaderPHP in your Symfony project by configuring it through configuration.
Requirements
This library requires PHP >= 5.5 to work.
Installation
Download the Bundle
composer require overblog/dataloader-bundle
Enable the Bundle
class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new Overblog\DataLoaderBundle\OverblogDataLoaderBundle(), ]; // ... } // ... }
Getting Started
Here a fast example of how you can use the bundle
overblog_dataloader: defaults: # required promise_adapter: "overblog_dataloader.react_promise_adapter" # optional factory: ~ options: batch: true cache: true max_batch_size: ~ cache_map: "overblog_dataloader.cache_map" cache_key_fn: ~ loaders: users: alias: "users_dataloader" batch_load_fn: "@app.user:getUsers" posts: batch_load_fn: "Post::getPosts" options: max_batch_size: 15 batch: false cache: false cache_map: "app.cache.map" cache_key_fn: "@app.cache" images: factory: my_factory batch_load_fn: "Image\\Loader::get"
This example will create 3 loaders as services:
- "@overblog_dataloader.users_loader" with alias "@users_dataloader"
- "@overblog_dataloader.posts_loader"
- "@overblog_dataloader.images_loader" create using custom factory function "my_factory"
Here the list of existing promise adapters:
- ReactPhp/Promise: overblog_dataloader.react_promise_adapter
- GuzzleHttp/Promises: overblog_dataloader.guzzle_http_promise_adapter
- Webonyx/GraphQL-PHP Sync Promise: overblog_dataloader.webonyx_graphql_sync_promise_adapter
Configuration using attributes
This bundle supports autoconfiguration via attributes. Add Overblog\DataLoaderBundle\Attribute\AsDataLoader on the service you want to expose as data loader:
<?php #[Overblog\DataLoaderBundle\Attribute\AsDataLoader(name: "users", alias: "users_dataloader")] class UserLoader { public function __invoke(array $ids): array { return ["John", "Steve", "Nash"]; } } ?>
Combine with GraphQLBundle
This bundle can be use with GraphQLBundle. Here an example:
- Bundle config
#graphql overblog_graphql: definitions: schema: query: Query services: promise_adapter: "webonyx_graphql.sync_promise_adapter" #dataloader overblog_dataloader: defaults: promise_adapter: "overblog_dataloader.webonyx_graphql_sync_promise_adapter" loaders: ships: alias: "ships_loader" batch_load_fn: "@app.graph.ships_loader:all"
- Batch loader function
services: app.graph.ship_repository: class: AppBundle\Entity\Repository\ShipRepository factory: ["@doctrine.orm.entity_manager", getRepository] arguments: - AppBundle\Entity\Ship app.graph.ships_loader: class: AppBundle\GraphQL\Loader\ShipLoader arguments: - "@overblog_graphql.promise_adapter" - "@app.graph.ship_repository"
<?php namespace AppBundle\GraphQL\Loader; use AppBundle\Entity\Repository\ShipRepository; use GraphQL\Executor\Promise\PromiseAdapter; class ShipLoader { private $promiseAdapter; private $repository; public function __construct(PromiseAdapter $promiseAdapter, ShipRepository $repository) { $this->promiseAdapter = $promiseAdapter; $this->repository = $repository; } public function all(array $shipsIDs) { $qb = $this->repository->createQueryBuilder('s'); $qb->add('where', $qb->expr()->in('s.id', ':ids')); $qb->setParameter('ids', $shipsIDs); $ships = $qb->getQuery()->getResult(); return $this->promiseAdapter->all($ships); } }
- Usage in a resolver
public function resolveShip($shipID) { $promise = $this->container->get('ships_loader')->load($shipID); return $promise; }
This is an example using the sync promise adapter of Webonyx.
License
Overblog/DataLoaderBundle is released under the MIT license.
overblog/dataloader-bundle 适用场景与选型建议
overblog/dataloader-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.61M 次下载、GitHub Stars 达 47, 最近一次更新时间为 2017 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「caching」 「Batching」 「dataLoader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 overblog/dataloader-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 overblog/dataloader-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 overblog/dataloader-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
DataLoaderPhp is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.
Query caching for Laravel 5
Port of Facebook's dataloader to PHP
Iterator that fetches values in batch
A speed layer that enables batch loading, de-duplication, and caching for efficient data fetching over any storage backend.
统计信息
- 总下载量: 2.61M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 47
- 点击次数: 20
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-04