From da13a0a6fad6074748d724de1cb9d9e239c306c2 Mon Sep 17 00:00:00 2001 From: "a.chernyi" Date: Thu, 27 Jan 2022 11:13:49 +0300 Subject: [PATCH] Expiration time for workers --- src/Activator/CacheActivator.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Activator/CacheActivator.php b/src/Activator/CacheActivator.php index f82035e..206af72 100644 --- a/src/Activator/CacheActivator.php +++ b/src/Activator/CacheActivator.php @@ -48,6 +48,13 @@ class CacheActivator implements FeatureActivatorInterface */ private $memory = []; + /** + * Expiration time in seconds + * + * @var int + */ + private int $expirationTime = 0; + /** * CacheActivator constructor. * @@ -88,7 +95,7 @@ public function isActive($name, Context $context) $hash = static::CACHE_KEY . '#' . $this->getName() . '#' . md5($name . '-' . $context->serialize()); // Step 1: Try get from memory cache - if (array_key_exists($hash, $this->memory)) { + if (array_key_exists($hash, $this->memory) && $this->expirationTime > time()) { return $this->memory[$hash]; } @@ -111,8 +118,15 @@ public function isActive($name, Context $context) $cacheItem->set($this->memory[$hash]); $cacheItem->expiresAfter($this->cacheTtl); $this->cachePool->save($cacheItem); + + $this->setExpirationTime($this->cacheTtl); } return $this->memory[$hash]; } + + private function setExpirationTime(int $time): void + { + $this->expirationTime = time() + $time; + } }