MultipleTrait.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php declare (strict_types=1);
  2. namespace Sabre\Cache;
  3. use Traversable;
  4. /**
  5. * This trait implements the 'multiple' functions of PSR-16.
  6. *
  7. * Caches that don't natively support 'multiple' operations can use this trait
  8. * for easy implementation.
  9. *
  10. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  11. * @author Evert Pot (https://evertpot.com/)
  12. * @license http://sabre.io/license/
  13. */
  14. trait MultipleTrait {
  15. /**
  16. * Obtains multiple cache items by their unique keys.
  17. *
  18. * This particular implementation returns its result as a generator.
  19. *
  20. * @param iterable $keys A list of keys that can obtained in a single
  21. * operation.
  22. * @param mixed $default Default value to return for keys that do not
  23. * exist.
  24. *
  25. * @throws \Psr\SimpleCache\InvalidArgumentException
  26. * MUST be thrown if $keys is neither an array nor a Traversable,
  27. * or if any of the $keys are not a legal value.
  28. * @return iterable A list of key => value pairs. Cache keys that do not
  29. * exist or are stale will have $default as value.
  30. */
  31. function getMultiple($keys, $default = null) {
  32. if (!is_array($keys) && !$keys instanceof Traversable) {
  33. throw new InvalidArgumentException('$keys must be traversable');
  34. }
  35. foreach ($keys as $key) {
  36. yield $key => $this->get($key, $default);
  37. }
  38. }
  39. /**
  40. * Persists a set of key => value pairs in the cache, with an optional TTL.
  41. *
  42. * @param iterable $values A list of key => value pairs for a
  43. * multiple-set operation.
  44. * @param null|int|DateInterval $ttl Optional. The TTL value of this
  45. * item. If no value is sent and the
  46. * driver supports TTL then the library
  47. * may set a default value for it or
  48. * let the driver take care of that.
  49. *
  50. * @throws \Psr\SimpleCache\InvalidArgumentException
  51. * MUST be thrown if $values is neither an array nor a Traversable,
  52. * or if any of the $values are not a legal value.
  53. * @return bool True on success and false on failure.
  54. */
  55. function setMultiple($values, $ttl = null) {
  56. if (!is_array($values) && !$values instanceof Traversable) {
  57. throw new InvalidArgumentException('$values must be traversable');
  58. }
  59. $result = true;
  60. foreach ($values as $key => $value) {
  61. if (!$this->set($key, $value, $ttl)) {
  62. $result = false;
  63. }
  64. }
  65. return $result;
  66. }
  67. /**
  68. * Deletes multiple cache items in a single operation.
  69. *
  70. * @param iterable $keys A list of string-based keys to be deleted.
  71. *
  72. * @throws \Psr\SimpleCache\InvalidArgumentException
  73. * MUST be thrown if $keys is neither an array nor a Traversable,
  74. * or if any of the $keys are not a legal value.
  75. * @return bool True if the items were successfully removed. False if there
  76. * was an error.
  77. */
  78. function deleteMultiple($keys) {
  79. if (!is_array($keys) && !$keys instanceof Traversable) {
  80. throw new InvalidArgumentException('$keys must be traversable');
  81. }
  82. $result = true;
  83. foreach ($keys as $key) {
  84. if (!$this->delete($key)) {
  85. $result = false;
  86. }
  87. }
  88. return $result;
  89. }
  90. }