| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace App\Module\LCache;
- use Psr\Cache\CacheItemInterface;
- use \DateTimeInterface;
- class CacheItem implements CacheItemInterface
- {
- public int $ttl;
- public int $create_ts;
- public int $expire_ts = 0;
- public string $key;
- public mixed $value;
- private bool $isHit;
- public function __construct(string $key, mixed $value = null, int $create_ts = 0, int $ttl = 0)
- {
- $this->key = $key;
- $this->value = $value;
- if ($create_ts) {
- $this->create_ts = time();
- } else {
- $this->create_ts = $create_ts;
- }
- if ($ttl) {
- $this->expire_ts = $this->create_ts + $ttl;
- $this->ttl = $ttl;
- }
- }
- /**
- * {@inheritdoc}
- */
- public function getKey(): string
- {
- return $this->key;
- }
- /**
- * {@inheritdoc}
- */
- public function get(): mixed
- {
- if (!$this->isHit()) {
- return null;
- }
- return $this->value;
- }
- public function getValue()
- {
- return $this->value;
- }
- /**
- * A cache hit occurs when a Calling Library requests an Item by key
- * and a matching value is found for that key, and that value has
- * not expired, and the value is not invalid for some other reason.
- *
- * Calling Libraries SHOULD make sure to verify isHit() on all get() calls.
- *
- * {@inheritdoc}
- */
- public function isHit($force = false): bool
- {
- if (isset($this->isHit) && $force === false) {
- return $this->isHit;
- }
- if ($this->expire_ts) {
- if ($this->expire_ts < time()) {
- $this->isHit = false;
- return false;
- }
- }
- $this->isHit = true;
- return true;
- }
- public function setHit($hit)
- {
- $this->isHit = $hit;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function set(mixed $value): static
- {
- $this->value = $value;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function expiresAt(?\DateTimeInterface $expires): static
- {
- if ($expires instanceof DateTimeInterface && !$expires instanceof \DateTimeImmutable) {
- $timezone = $expires->getTimezone();
- $expires = \DateTimeImmutable::createFromFormat('U', (string)$expires->getTimestamp(), $timezone);
- if ($expires) {
- $expires = $expires->setTimezone($timezone);
- }
- }
- if ($expires instanceof DateTimeInterface) {
- $this->expires = $expires;
- $this->expire_ts = $expires->getTimestamp();
- } else {
- $this->expires = null;
- }
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function expiresAfter(int|\DateInterval|null $time): static
- {
- if ($time === null) {
- $this->expires = null;
- return $this;
- }
- $this->expires = new \DateTimeImmutable();
- if (!$time instanceof \DateInterval) {
- $time2 = new \DateInterval(sprintf('PT%sS', abs($time)));
- }
- if($time > 0){
- $this->expires = $this->expires->add($time2);
- }else{
- $this->expires = $this->expires->sub($time2);
- }
- $this->expire_ts = $this->expires->getTimestamp();
- return $this;
- }
- public function getExpiresAt(): DateTimeInterface
- {
- if (!$this->expires) {
- $this->expires = new \DateTimeImmutable();
- $time = new \DateInterval(sprintf('PT%sS', $this->ttl));
- $this->expires->add($time);
- }
- return $this->expires;
- }
- }
|