Download the PHP package overblog/dataloader-php without Composer

On this page you can find all versions of the php package overblog/dataloader-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package dataloader-php

DataLoaderPHP

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.

GitHub Actions Code Coverage Latest Stable Version

Requirements

This library requires PHP >= 7.3 to work.

Getting Started

First, install DataLoaderPHP using composer.

To get started, create a DataLoader object.

Batching

Batching is not an advanced feature, it's DataLoader's primary feature. Create loaders by providing a batch loading function.

A batch loading callable / callback accepts an Array of keys, and returns a Promise which resolves to an Array of values.

Then load individual values from the loader. DataLoaderPHP will coalesce all individual loads which occur within a single frame of execution (using await method) and then call your batch function with all requested keys.

A naive application may have issued four round-trips to a backend for the required information, but with DataLoaderPHP this application will make at most two.

DataLoaderPHP allows you to decouple unrelated parts of your application without sacrificing the performance of batch data-loading. While the loader presents an API that loads individual values, all concurrent requests will be coalesced and presented to your batch loading function. This allows your application to safely distribute data fetching requirements throughout your application and maintain minimal outgoing data requests.

Batch Function

A batch loading function accepts an Array of keys, and returns a Promise which resolves to an Array of values. There are a few constraints that must be upheld:

For example, if your batch function was provided the Array of keys: [ 2, 9, 6, 1 ], and loading from a back-end service returned the values:

Our back-end service returned results in a different order than we requested, likely because it was more efficient for it to do so. Also, it omitted a result for key 6, which we can interpret as no value existing for that key.

To uphold the constraints of the batch function, it must return an Array of values the same length as the Array of keys, and re-order them to ensure each index aligns with the original keys [ 2, 9, 6, 1 ]:

Caching (current PHP instance)

DataLoader provides a memoization cache for all loads which occur in a single request to your application. After ->load() is called once with a given key, the resulting value is cached to eliminate redundant loads.

In addition to relieving pressure on your data storage, caching results per-request also creates fewer objects which may relieve memory pressure on your application:

Clearing Cache

In certain uncommon cases, clearing the request cache may be necessary.

The most common example when clearing the loader's cache is necessary is after a mutation or update within the same request, when a cached value could be out of date and future loads should not use any possibly cached value.

Here's a simple example using SQL UPDATE to illustrate.

Caching Errors

If a batch load fails (that is, a batch function throws or returns a rejected Promise), then the requested values will not be cached. However if a batch function returns an Error instance for an individual value, that Error will be cached to avoid frequently loading the same Error.

In some circumstances you may wish to clear the cache for these individual Errors:

Disabling Cache

In certain uncommon cases, a DataLoader which does not cache may be desirable. Calling new DataLoader(myBatchFn, new Option(['cache' => false ])) will ensure that every call to ->load() will produce a new Promise, and requested keys will not be saved in memory.

However, when the memoization cache is disabled, your batch function will receive an array of keys which may contain duplicates! Each key will be associated with each call to ->load(). Your batch loader should provide a value for each instance of the requested key.

For example:

More complex cache behavior can be achieved by calling ->clear() or ->clearAll() rather than disabling the cache completely. For example, this DataLoader will provide unique keys to a batch function due to the memoization cache being enabled, but will immediately clear its cache when the batch function is called so later requests will load new values.

API

class DataLoader

DataLoaderPHP creates a public API for loading data from a particular data back-end with unique keys such as the id column of a SQL table or document name in a MongoDB database, given a batch loading function.

Each DataLoaderPHP instance contains a unique memoized cache. Use caution when used in long-lived applications or those which serve many users with different access permissions and consider creating a new instance per web request.

new DataLoader(callable $batchLoadFn, PromiseAdapterInterface $promiseAdapter [, Option $options])

Create a new DataLoaderPHP given a batch loading instance and options.

load($key)

Loads a key, returning a Promise for the value represented by that key.

loadMany($keys)

Loads multiple keys, promising an array of values:

This is equivalent to the more verbose:

clear($key)

Clears the value at $key from the cache, if it exists. Returns itself for method chaining.

clearAll()

Clears the entire cache. To be used when some event results in unknown invalidations across this particular DataLoaderPHP. Returns itself for method chaining.

prime($key, $value)

Primes the cache with the provided key and value. If the key already exists, no change is made. (To forcefully prime the cache, clear the key first with $loader->clear($key)->prime($key, $value). Returns itself for method chaining.

static await([$promise][, $unwrap])

You can synchronously force promises to complete using DataLoaderPHP's await method. When an await function is invoked it is expected to deliver a value to the promise or reject the promise. Await method process all waiting promise in all dataLoaderPHP instances.

Using with Webonyx/GraphQL

DataLoader pairs nicely well with Webonyx/GraphQL. GraphQL fields are designed to be stand-alone functions. Without a caching or batching mechanism, it's easy for a naive GraphQL server to issue new database requests each time a field is resolved.

Consider the following GraphQL request:

Naively, if me, bestFriend and friends each need to request the backend, there could be at most 13 database requests!

When using DataLoader, we could define the User type at most 4 database requests, and possibly fewer if there are cache hits.

You can also see an example.

Using with Symfony

See the bundle.

Credits

Overblog/DataLoaderPHP is a port of dataLoader NodeJS version by Facebook.

Also, large parts of the documentation have been ported from the dataLoader NodeJS version Docs.

License

Overblog/DataLoaderPHP is released under the MIT license.


All versions of dataloader-php with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package overblog/dataloader-php contains the following files

Loading the files please wait ....