CacheItemInterface.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Psr\Cache;
  3. /**
  4. * CacheItemInterface defines an interface for interacting with objects inside a cache.
  5. *
  6. * Each Item object MUST be associated with a specific key, which can be set
  7. * according to the implementing system and is typically passed by the
  8. * Cache\CacheItemPoolInterface object.
  9. *
  10. * The Cache\CacheItemInterface object encapsulates the storage and retrieval of
  11. * cache items. Each Cache\CacheItemInterface is generated by a
  12. * Cache\CacheItemPoolInterface object, which is responsible for any required
  13. * setup as well as associating the object with a unique Key.
  14. * Cache\CacheItemInterface objects MUST be able to store and retrieve any type
  15. * of PHP value defined in the Data section of the specification.
  16. *
  17. * Calling Libraries MUST NOT instantiate Item objects themselves. They may only
  18. * be requested from a Pool object via the getItem() method. Calling Libraries
  19. * SHOULD NOT assume that an Item created by one Implementing Library is
  20. * compatible with a Pool from another Implementing Library.
  21. */
  22. interface CacheItemInterface
  23. {
  24. /**
  25. * Returns the key for the current cache item.
  26. *
  27. * The key is loaded by the Implementing Library, but should be available to
  28. * the higher level callers when needed.
  29. *
  30. * @return string
  31. * The key string for this cache item.
  32. */
  33. public function getKey();
  34. /**
  35. * Retrieves the value of the item from the cache associated with this object's key.
  36. *
  37. * The value returned must be identical to the value originally stored by set().
  38. *
  39. * If isHit() returns false, this method MUST return null. Note that null
  40. * is a legitimate cached value, so the isHit() method SHOULD be used to
  41. * differentiate between "null value was found" and "no value was found."
  42. *
  43. * @return mixed
  44. * The value corresponding to this cache item's key, or null if not found.
  45. */
  46. public function get();
  47. /**
  48. * Confirms if the cache item lookup resulted in a cache hit.
  49. *
  50. * Note: This method MUST NOT have a race condition between calling isHit()
  51. * and calling get().
  52. *
  53. * @return bool
  54. * True if the request resulted in a cache hit. False otherwise.
  55. */
  56. public function isHit();
  57. /**
  58. * Sets the value represented by this cache item.
  59. *
  60. * The $value argument may be any item that can be serialized by PHP,
  61. * although the method of serialization is left up to the Implementing
  62. * Library.
  63. *
  64. * @param mixed $value
  65. * The serializable value to be stored.
  66. *
  67. * @return static
  68. * The invoked object.
  69. */
  70. public function set($value);
  71. /**
  72. * Sets the expiration time for this cache item.
  73. *
  74. * @param \DateTimeInterface|null $expiration
  75. * The point in time after which the item MUST be considered expired.
  76. * If null is passed explicitly, a default value MAY be used. If none is set,
  77. * the value should be stored permanently or for as long as the
  78. * implementation allows.
  79. *
  80. * @return static
  81. * The called object.
  82. */
  83. public function expiresAt($expiration);
  84. /**
  85. * Sets the expiration time for this cache item.
  86. *
  87. * @param int|\DateInterval|null $time
  88. * The period of time from the present after which the item MUST be considered
  89. * expired. An integer parameter is understood to be the time in seconds until
  90. * expiration. If null is passed explicitly, a default value MAY be used.
  91. * If none is set, the value should be stored permanently or for as long as the
  92. * implementation allows.
  93. *
  94. * @return static
  95. * The called object.
  96. */
  97. public function expiresAfter($time);
  98. }