CacheItemInterface.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. */
  23. interface CacheItemInterface
  24. {
  25. /**
  26. * Returns the key for the current cache item.
  27. *
  28. * The key is loaded by the Implementing Library, but should be available to
  29. * the higher level callers when needed.
  30. *
  31. * @return string
  32. * The key string for this cache item.
  33. */
  34. public function getKey();
  35. /**
  36. * Retrieves the value of the item from the cache associated with this object's key.
  37. *
  38. * The value returned must be identical to the value originally stored by set().
  39. *
  40. * If isHit() returns false, this method MUST return null. Note that null
  41. * is a legitimate cached value, so the isHit() method SHOULD be used to
  42. * differentiate between "null value was found" and "no value was found."
  43. *
  44. * @return mixed
  45. * The value corresponding to this cache item's key, or null if not found.
  46. */
  47. public function get();
  48. /**
  49. * Confirms if the cache item lookup resulted in a cache hit.
  50. *
  51. * Note: This method MUST NOT have a race condition between calling isHit()
  52. * and calling get().
  53. *
  54. * @return bool
  55. * True if the request resulted in a cache hit. False otherwise.
  56. */
  57. public function isHit();
  58. /**
  59. * Sets the value represented by this cache item.
  60. *
  61. * The $value argument may be any item that can be serialized by PHP,
  62. * although the method of serialization is left up to the Implementing
  63. * Library.
  64. *
  65. * @param mixed $value
  66. * The serializable value to be stored.
  67. *
  68. * @return static
  69. * The invoked object.
  70. */
  71. public function set($value);
  72. /**
  73. * Sets the expiration time for this cache item.
  74. *
  75. * @param \DateTimeInterface $expiration
  76. * The point in time after which the item MUST be considered expired.
  77. * If null is passed explicitly, a default value MAY be used. If none is set,
  78. * the value should be stored permanently or for as long as the
  79. * implementation allows.
  80. *
  81. * @return static
  82. * The called object.
  83. */
  84. public function expiresAt($expiration);
  85. /**
  86. * Sets the expiration time for this cache item.
  87. *
  88. * @param int|\DateInterval $time
  89. * The period of time from the present after which the item MUST be considered
  90. * expired. An integer parameter is understood to be the time in seconds until
  91. * expiration. If null is passed explicitly, a default value MAY be used.
  92. * If none is set, the value should be stored permanently or for as long as the
  93. * implementation allows.
  94. *
  95. * @return static
  96. * The called object.
  97. */
  98. public function expiresAfter($time);
  99. }