RetrievesMultipleKeys.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Illuminate\Cache;
  3. trait RetrievesMultipleKeys
  4. {
  5. /**
  6. * Retrieve multiple items from the cache by key.
  7. *
  8. * Items not found in the cache will have a null value.
  9. *
  10. * @param array $keys
  11. * @return array
  12. */
  13. public function many(array $keys)
  14. {
  15. $return = [];
  16. $keys = collect($keys)->mapWithKeys(function ($value, $key) {
  17. return [is_string($key) ? $key : $value => is_string($key) ? $value : null];
  18. })->all();
  19. foreach ($keys as $key => $default) {
  20. $return[$key] = $this->get($key, $default);
  21. }
  22. return $return;
  23. }
  24. /**
  25. * Store multiple items in the cache for a given number of seconds.
  26. *
  27. * @param array $values
  28. * @param int $seconds
  29. * @return bool
  30. */
  31. public function putMany(array $values, $seconds)
  32. {
  33. $manyResult = null;
  34. foreach ($values as $key => $value) {
  35. $result = $this->put($key, $value, $seconds);
  36. $manyResult = is_null($manyResult) ? $result : $result && $manyResult;
  37. }
  38. return $manyResult ?: false;
  39. }
  40. }