overblog / dataloader-php

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.
MIT License
199 stars 21 forks source link

Make it possible to call key functions when extending Dataloader #9

Closed alafon closed 7 years ago

alafon commented 7 years ago

We might need to call getCacheKeyFromKey and checkKey when extending the Dataloader class, in order to have homogeneous keys.

Here is an exemple:

    public function load($key)
    {
        $cacheItem = $this->primeCache->getItem($this->getCacheKeyFromKey($key));

        if (!$cacheItem->isHit()) {
            $promise = parent::load($key);
            if ($promise instanceof Promise) {
                $promise->then(function ($value) use ($key) {
                    $this->prime($key, $value);
                });
            }
            return $promise;
        } else {
            return $cacheItem->get();
        }
    }