CacheItem.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Module\LCache;
  3. use Psr\Cache\CacheItemInterface;
  4. use \DateTimeInterface;
  5. class CacheItem implements CacheItemInterface
  6. {
  7. public int $ttl;
  8. public int $create_ts;
  9. public int $expire_ts = 0;
  10. public string $key;
  11. public mixed $value;
  12. private bool $isHit;
  13. public function __construct(string $key, mixed $value = null, int $create_ts = 0, int $ttl = 0)
  14. {
  15. $this->key = $key;
  16. $this->value = $value;
  17. if ($create_ts) {
  18. $this->create_ts = time();
  19. } else {
  20. $this->create_ts = $create_ts;
  21. }
  22. if ($ttl) {
  23. $this->expire_ts = $this->create_ts + $ttl;
  24. $this->ttl = $ttl;
  25. }
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getKey(): string
  31. {
  32. return $this->key;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function get(): mixed
  38. {
  39. if (!$this->isHit()) {
  40. return null;
  41. }
  42. return $this->value;
  43. }
  44. public function getValue()
  45. {
  46. return $this->value;
  47. }
  48. /**
  49. * A cache hit occurs when a Calling Library requests an Item by key
  50. * and a matching value is found for that key, and that value has
  51. * not expired, and the value is not invalid for some other reason.
  52. *
  53. * Calling Libraries SHOULD make sure to verify isHit() on all get() calls.
  54. *
  55. * {@inheritdoc}
  56. */
  57. public function isHit($force = false): bool
  58. {
  59. if (isset($this->isHit) && $force === false) {
  60. return $this->isHit;
  61. }
  62. if ($this->expire_ts) {
  63. if ($this->expire_ts < time()) {
  64. $this->isHit = false;
  65. return false;
  66. }
  67. }
  68. $this->isHit = true;
  69. return true;
  70. }
  71. public function setHit($hit)
  72. {
  73. $this->isHit = $hit;
  74. return $this;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function set(mixed $value): static
  80. {
  81. $this->value = $value;
  82. return $this;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function expiresAt(?\DateTimeInterface $expires): static
  88. {
  89. if ($expires instanceof DateTimeInterface && !$expires instanceof \DateTimeImmutable) {
  90. $timezone = $expires->getTimezone();
  91. $expires = \DateTimeImmutable::createFromFormat('U', (string)$expires->getTimestamp(), $timezone);
  92. if ($expires) {
  93. $expires = $expires->setTimezone($timezone);
  94. }
  95. }
  96. if ($expires instanceof DateTimeInterface) {
  97. $this->expires = $expires;
  98. $this->expire_ts = $expires->getTimestamp();
  99. } else {
  100. $this->expires = null;
  101. }
  102. return $this;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function expiresAfter(int|\DateInterval|null $time): static
  108. {
  109. if ($time === null) {
  110. $this->expires = null;
  111. return $this;
  112. }
  113. $this->expires = new \DateTimeImmutable();
  114. if (!$time instanceof \DateInterval) {
  115. $time2 = new \DateInterval(sprintf('PT%sS', abs($time)));
  116. }
  117. if($time > 0){
  118. $this->expires = $this->expires->add($time2);
  119. }else{
  120. $this->expires = $this->expires->sub($time2);
  121. }
  122. $this->expire_ts = $this->expires->getTimestamp();
  123. return $this;
  124. }
  125. public function getExpiresAt(): DateTimeInterface
  126. {
  127. if (!$this->expires) {
  128. $this->expires = new \DateTimeImmutable();
  129. $time = new \DateInterval(sprintf('PT%sS', $this->ttl));
  130. $this->expires->add($time);
  131. }
  132. return $this->expires;
  133. }
  134. }