Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TTL in seconds - fractional seconds support #2359

Open
mvorisek opened this issue Jun 28, 2023 · 1 comment
Open

TTL in seconds - fractional seconds support #2359

mvorisek opened this issue Jun 28, 2023 · 1 comment

Comments

@mvorisek
Copy link

mvorisek commented Jun 28, 2023

It seems TTL values are typed as integers - https://github.com/phpredis/phpredis/blob/35a7cc09/redis.stub.php#L3066

Since Redis v2.6 the Redis stores all TTL values with milliseconds resolution, so I expect:

  • float values for TTL in seconds accepted everywhere (ex: $redis->setEx('key', 30.5, 'value');)
  • and for millisecond TTL as well - time is inprecise, and user should be not required to round/cast float values

PHP float type always accepts an int, even with strict types, so no BC break is implied.

Code like:

$redisClient->pSetEx($cacheKey, (int) ($this->getCacheDataTTLSeconds() * 1000), $value);

could be then written as:

$redisClient->pSetEx($cacheKey, $this->getCacheDataTTLSeconds() * 1000, $value);

or even

$redisClient->setEx($cacheKey, $this->getCacheDataTTLSeconds(), $value);

which is shorter and more readable.

@michael-grunder
Copy link
Member

It's an interesting question, but I don't really know the right answer.

The issue with code like this:
$redis->setEx('key', 30.5, 'value');

Is that PhpRedis would need to detect the float, redirect to PSETEX (along with a millisecond conversion).

127.0.0.1:6379> setex key 30.5 value
(error) ERR value is not an integer or out of range

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants