On 11/27/2016 05:07 PM, Nicolas Grekas wrote:
Thanks for the answer Jordi,

I fine with your answers! Just a few notes:


    IMO this kinda goes in the simplicity category, which is why I
    still think enforcing an array return value vs a traversable would
    also be simpler.


Understood.

Then I'd like to suggest to add a second `$default = null` argument to getMultiple(). That would be consistent with get(), and allow detecting cache misses if really required, by e.g. providing an object there and check the resulting values identities.


        - returning `array` for `getMultiple` is not consistent with PSR-6
        `getItems` which returns `array|Traversable`


    See 2 points above. I think array only would be best. If it's
    wrapping a PSR-6 pool it can just normalize that with
    iterator_to_array()


No strong opinion on this on my side.

If the concern here is PSR-6 compatibility, bear in mind that since PSR-6 uses a CacheItem object iterator_to_array() won't work. Additionally, since it could return either an array or traversable, iterator_to_array() cannot be called blindly anyway. I see two ways that the bridge could work:

public function getMultiple(array $keys) : iterable {
  foreach ($this->pool->getItems($keys) as $item) {
    yield $item->getKey() => $item->get();
  }
}

public function getMultiple(array $keys) : array {
  $result = [];
  foreach ($this->pool->getItems($keys) as $item) {
    $result[$item->getKey()] = $item->get();
  }
  return $result;
}

(Note: Above is entirely untested; I probably made some stupid syntax error.)

In both cases, a cache miss will result in a null entry as Niklas is asking for. However, a complete cache miss would result in an empty array/iterator, which is how PSR-6 behaves. This also obviates the need for a $default parameter.

My preference is to follow PSR-6 and allow an iterable for memory safety and consistency (if someone wants an array they can then call iterator_to_array() safely), but I won't make my vote contingent on that.

--Larry Garfield

--
You received this message because you are subscribed to the Google Groups "PHP 
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/php-fig/8683f786-b019-b223-a533-d1ac4ec64afe%40garfieldtech.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to