CacheInterface.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Psr\SimpleCache;
  3. interface CacheInterface
  4. {
  5. /**
  6. * Fetches a value from the cache.
  7. *
  8. * @param string $key The unique key of this item in the cache.
  9. * @param mixed $default Default value to return if the key does not exist.
  10. *
  11. * @return mixed The value of the item from the cache, or $default in case of cache miss.
  12. *
  13. * @throws \Psr\SimpleCache\InvalidArgumentException
  14. * MUST be thrown if the $key string is not a legal value.
  15. */
  16. public function get(string $key, mixed $default = null): mixed;
  17. /**
  18. * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
  19. *
  20. * @param string $key The key of the item to store.
  21. * @param mixed $value The value of the item to store, must be serializable.
  22. * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
  23. * the driver supports TTL then the library may set a default value
  24. * for it or let the driver take care of that.
  25. *
  26. * @return bool True on success and false on failure.
  27. *
  28. * @throws \Psr\SimpleCache\InvalidArgumentException
  29. * MUST be thrown if the $key string is not a legal value.
  30. */
  31. public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool;
  32. /**
  33. * Delete an item from the cache by its unique key.
  34. *
  35. * @param string $key The unique cache key of the item to delete.
  36. *
  37. * @return bool True if the item was successfully removed. False if there was an error.
  38. *
  39. * @throws \Psr\SimpleCache\InvalidArgumentException
  40. * MUST be thrown if the $key string is not a legal value.
  41. */
  42. public function delete(string $key): bool;
  43. /**
  44. * Wipes clean the entire cache's keys.
  45. *
  46. * @return bool True on success and false on failure.
  47. */
  48. public function clear(): bool;
  49. /**
  50. * Obtains multiple cache items by their unique keys.
  51. *
  52. * @param iterable<string> $keys A list of keys that can be obtained in a single operation.
  53. * @param mixed $default Default value to return for keys that do not exist.
  54. *
  55. * @return iterable<string, mixed> A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
  56. *
  57. * @throws \Psr\SimpleCache\InvalidArgumentException
  58. * MUST be thrown if $keys is neither an array nor a Traversable,
  59. * or if any of the $keys are not a legal value.
  60. */
  61. public function getMultiple(iterable $keys, mixed $default = null): iterable;
  62. /**
  63. * Persists a set of key => value pairs in the cache, with an optional TTL.
  64. *
  65. * @param iterable $values A list of key => value pairs for a multiple-set operation.
  66. * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
  67. * the driver supports TTL then the library may set a default value
  68. * for it or let the driver take care of that.
  69. *
  70. * @return bool True on success and false on failure.
  71. *
  72. * @throws \Psr\SimpleCache\InvalidArgumentException
  73. * MUST be thrown if $values is neither an array nor a Traversable,
  74. * or if any of the $values are not a legal value.
  75. */
  76. public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool;
  77. /**
  78. * Deletes multiple cache items in a single operation.
  79. *
  80. * @param iterable<string> $keys A list of string-based keys to be deleted.
  81. *
  82. * @return bool True if the items were successfully removed. False if there was an error.
  83. *
  84. * @throws \Psr\SimpleCache\InvalidArgumentException
  85. * MUST be thrown if $keys is neither an array nor a Traversable,
  86. * or if any of the $keys are not a legal value.
  87. */
  88. public function deleteMultiple(iterable $keys): bool;
  89. /**
  90. * Determines whether an item is present in the cache.
  91. *
  92. * NOTE: It is recommended that has() is only to be used for cache warming type purposes
  93. * and not to be used within your live applications operations for get/set, as this method
  94. * is subject to a race condition where your has() will return true and immediately after,
  95. * another script can remove it making the state of your app out of date.
  96. *
  97. * @param string $key The cache item key.
  98. *
  99. * @return bool
  100. *
  101. * @throws \Psr\SimpleCache\InvalidArgumentException
  102. * MUST be thrown if the $key string is not a legal value.
  103. */
  104. public function has(string $key): bool;
  105. }