Skip to content

Commit dc0375d

Browse files
committed
Fixed issue on (P)Redis with negative TTLs awaiting more specification of the PSR6 about this.
1 parent 5afb92b commit dc0375d

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/phpFastCache/Drivers/Predis/Driver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ protected function driverWrite(CacheItemInterface $item)
7474
if ($item instanceof Item) {
7575
$ttl = $item->getExpirationDate()->getTimestamp() - time();
7676

77-
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
77+
/**
78+
* @see https://redis.io/commands/setex
79+
* @see https://redis.io/commands/expire
80+
*/
81+
if($ttl <= 0){
82+
return $this->instance->expire($item->getKey(), 0);
83+
}else{
84+
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
85+
}
7886
} else {
7987
throw new \InvalidArgumentException('Cross-Driver type confusion detected');
8088
}

src/phpFastCache/Drivers/Redis/Driver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ protected function driverWrite(CacheItemInterface $item)
6565
if ($item instanceof Item) {
6666
$ttl = $item->getExpirationDate()->getTimestamp() - time();
6767

68-
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
68+
/**
69+
* @see https://redis.io/commands/setex
70+
* @see https://redis.io/commands/expire
71+
*/
72+
if($ttl <= 0){
73+
return $this->instance->expire($item->getKey(), 0);
74+
}else{
75+
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
76+
}
6977
} else {
7078
throw new \InvalidArgumentException('Cross-Driver type confusion detected');
7179
}

0 commit comments

Comments
 (0)