CacheItem.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think\cache;
  13. use DateInterval;
  14. use DateTime;
  15. use DateTimeInterface;
  16. use Psr\Cache\CacheItemInterface;
  17. use think\cache\exception\InvalidArgumentException;
  18. /**
  19. * CacheItem实现类
  20. */
  21. class CacheItem implements CacheItemInterface
  22. {
  23. /**
  24. * 缓存Key
  25. * @var string
  26. */
  27. protected $key;
  28. /**
  29. * 缓存内容
  30. * @var mixed
  31. */
  32. protected $value;
  33. /**
  34. * 过期时间
  35. * @var int|DateTimeInterface
  36. */
  37. protected $expire;
  38. /**
  39. * 缓存tag
  40. * @var string
  41. */
  42. protected $tag;
  43. /**
  44. * 缓存是否命中
  45. * @var bool
  46. */
  47. protected $isHit = false;
  48. public function __construct(string $key = null)
  49. {
  50. $this->key = $key;
  51. }
  52. /**
  53. * 为此缓存项设置「键」
  54. * @access public
  55. * @param string $key
  56. * @return $this
  57. */
  58. public function setKey(string $key)
  59. {
  60. $this->key = $key;
  61. return $this;
  62. }
  63. /**
  64. * 返回当前缓存项的「键」
  65. * @access public
  66. * @return string
  67. */
  68. public function getKey()
  69. {
  70. return $this->key;
  71. }
  72. /**
  73. * 返回当前缓存项的有效期
  74. * @access public
  75. * @return DateTimeInterface|int|null
  76. */
  77. public function getExpire()
  78. {
  79. if ($this->expire instanceof DateTimeInterface) {
  80. return $this->expire;
  81. }
  82. return $this->expire ? $this->expire - time() : null;
  83. }
  84. /**
  85. * 获取缓存Tag
  86. * @access public
  87. * @return string
  88. */
  89. public function getTag()
  90. {
  91. return $this->tag;
  92. }
  93. /**
  94. * 凭借此缓存项的「键」从缓存系统里面取出缓存项
  95. * @access public
  96. * @return mixed
  97. */
  98. public function get()
  99. {
  100. return $this->value;
  101. }
  102. /**
  103. * 确认缓存项的检查是否命中
  104. * @access public
  105. * @return bool
  106. */
  107. public function isHit(): bool
  108. {
  109. return $this->isHit;
  110. }
  111. /**
  112. * 为此缓存项设置「值」
  113. * @access public
  114. * @param mixed $value
  115. * @return $this
  116. */
  117. public function set($value)
  118. {
  119. $this->value = $value;
  120. $this->isHit = true;
  121. return $this;
  122. }
  123. /**
  124. * 为此缓存项设置所属标签
  125. * @access public
  126. * @param string $tag
  127. * @return $this
  128. */
  129. public function tag(string $tag = null)
  130. {
  131. $this->tag = $tag;
  132. return $this;
  133. }
  134. /**
  135. * 设置缓存项的有效期
  136. * @access public
  137. * @param mixed $expire
  138. * @return $this
  139. */
  140. public function expire($expire)
  141. {
  142. if (is_null($expire)) {
  143. $this->expire = null;
  144. } elseif (is_numeric($expire) || $expire instanceof DateInterval) {
  145. $this->expiresAfter($expire);
  146. } elseif ($expire instanceof DateTimeInterface) {
  147. $this->expire = $expire;
  148. } else {
  149. throw new InvalidArgumentException('not support datetime');
  150. }
  151. return $this;
  152. }
  153. /**
  154. * 设置缓存项的准确过期时间点
  155. * @access public
  156. * @param DateTimeInterface $expiration
  157. * @return $this
  158. */
  159. public function expiresAt($expiration)
  160. {
  161. if ($expiration instanceof DateTimeInterface) {
  162. $this->expire = $expiration;
  163. } else {
  164. throw new InvalidArgumentException('not support datetime');
  165. }
  166. return $this;
  167. }
  168. /**
  169. * 设置缓存项的过期时间
  170. * @access public
  171. * @param int|DateInterval $timeInterval
  172. * @return $this
  173. * @throws InvalidArgumentException
  174. */
  175. public function expiresAfter($timeInterval)
  176. {
  177. if ($timeInterval instanceof DateInterval) {
  178. $this->expire = (int) DateTime::createFromFormat('U', (string) time())->add($timeInterval)->format('U');
  179. } elseif (is_numeric($timeInterval)) {
  180. $this->expire = $timeInterval + time();
  181. } else {
  182. throw new InvalidArgumentException('not support datetime');
  183. }
  184. return $this;
  185. }
  186. }