3 * @see https://github.com/zendframework/zend-cache for the canonical source repository
4 * @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
8 namespace Zend\Cache\Psr\CacheItemPool
;
11 use DateTimeImmutable
;
12 use DateTimeInterface
;
14 use Psr\Cache\CacheItemInterface
;
16 final class CacheItem
implements CacheItemInterface
31 * True if the cache item lookup resulted in a cache hit or if they item is deferred or successfully saved
34 private $isHit = false;
37 * Timestamp item will expire at if expiresAt() called, null otherwise
40 private $expiration = null;
43 * Seconds after item is stored it will expire at if expiresAfter() called, null otherwise
60 public function __construct($key, $value, $isHit)
63 $this->value
= $isHit ?
$value : null;
64 $this->isHit
= $isHit;
65 $this->utc
= new DateTimeZone('UTC');
71 public function getKey()
87 public function isHit()
92 $ttl = $this->getTtl();
93 return $ttl === null ||
$ttl > 0;
99 * This function is called by CacheItemPoolDecorator::saveDeferred() and is not intended for use by other calling
102 * @param boolean $isHit
105 public function setIsHit($isHit)
107 $this->isHit
= $isHit;
115 public function set($value)
117 $this->value
= $value;
125 public function expiresAt($expiration)
127 if (! ($expiration === null ||
$expiration instanceof DateTimeInterface
)) {
128 throw new InvalidArgumentException('$expiration must be null or an instance of DateTimeInterface');
131 $this->expiration
= $expiration instanceof DateTimeInterface ?
$expiration->getTimestamp() : null;
140 public function expiresAfter($time)
142 if ($time instanceof DateInterval
) {
143 $now = new DateTimeImmutable('now', $this->utc
);
144 $end = $now->add($time);
145 $this->ttl
= $end->getTimestamp() - $now->getTimestamp();
146 } elseif (is_int($time) ||
$time === null) {
149 throw new InvalidArgumentException(sprintf('Invalid $time "%s"', gettype($time)));
152 $this->expiration
= null;
158 * Returns number of seconds until item expires
160 * If NULL, the pool should use the default TTL for the storage adapter. If <= 0, the item has expired.
164 public function getTtl()
167 if ($this->expiration
!== null) {
168 $ttl = $this->expiration
- time();