diff --git a/src/Cache/CacheBase.php b/src/Cache/CacheBase.php index fbe5089..45c7110 100644 --- a/src/Cache/CacheBase.php +++ b/src/Cache/CacheBase.php @@ -2,7 +2,7 @@ namespace Drupal\redis\Cache; -use \DateInterval; +use DateInterval; use Drupal\Component\Assertion\Inspector; use Drupal\Component\Serialization\SerializationInterface; use Drupal\Core\Cache\Cache; @@ -221,21 +221,27 @@ abstract class CacheBase implements CacheBackendInterface { } /** - * Calculate the correct expiration time. + * Calculate the correct ttl value for redis. * * @param int $expire * The expiration time provided for the cache set. * * @return int - * The default expiration if expire is PERMANENT or higher than the default. - * May return negative values if the item is already expired. + * The default TTL if expire is PERMANENT or higher than the default. + * Otherwise, the adjusted lifetime of the cache. May return negative values + * if the item is already expired. */ protected function getExpiration($expire) { - if ($expire == Cache::PERMANENT || $expire > $this->permTtl) { + if ($expire == Cache::PERMANENT) { return $this->permTtl; } - return $expire - \Drupal::time()->getRequestTime(); + $expire_ttl = $expire - \Drupal::time()->getRequestTime(); + if ($expire_ttl > $this->permTtl) { + return $this->permTtl; + } + return $expire_ttl; } + /** * Return the key for the tag used to specify the bin of cache-entries. */ @@ -267,7 +273,7 @@ abstract class CacheBase implements CacheBackendInterface { else { if ($iv = DateInterval::createFromDateString($ttl)) { // http://stackoverflow.com/questions/14277611/convert-dateinterval-object-to-seconds-in-php - $this->permTtl = ($iv->y * 31536000 + $iv->m * 2592000 + $iv->days * 86400 + $iv->h * 3600 + $iv->i * 60 + $iv->s); + $this->permTtl = ($iv->y * 31536000 + $iv->m * 2592000 + $iv->d * 86400 + $iv->h * 3600 + $iv->i * 60 + $iv->s); } else { // Log error about invalid ttl. diff --git a/tests/src/Kernel/RedisCacheTest.php b/tests/src/Kernel/RedisCacheTest.php index 8668be3..ba5fc1d 100644 --- a/tests/src/Kernel/RedisCacheTest.php +++ b/tests/src/Kernel/RedisCacheTest.php @@ -21,7 +21,7 @@ class RedisCacheTest extends GenericCacheBackendUnitTestBase { * * @var array */ - public static $modules = ['system', 'redis']; + protected static $modules = ['system', 'redis']; public function register(ContainerBuilder $container) { self::setUpSettings(); diff --git a/tests/src/Kernel/RedisFloodTest.php b/tests/src/Kernel/RedisFloodTest.php index 62dd416..3c5ab71 100644 --- a/tests/src/Kernel/RedisFloodTest.php +++ b/tests/src/Kernel/RedisFloodTest.php @@ -20,7 +20,7 @@ class RedisFloodTest extends KernelTestBase { * * @var array */ - public static $modules = ['redis']; + protected static $modules = ['redis']; /** * Test flood control. diff --git a/tests/src/Kernel/RedisQueueTest.php b/tests/src/Kernel/RedisQueueTest.php index 3d728da..2ee031a 100644 --- a/tests/src/Kernel/RedisQueueTest.php +++ b/tests/src/Kernel/RedisQueueTest.php @@ -20,7 +20,7 @@ class RedisQueueTest extends CoreQueueTest { * * @var array */ - public static $modules = ['redis']; + protected static $modules = ['redis']; /** * Tests Redis non-blocking queue.