CacheItem.php 4.6 KB

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