Search code examples
phptimeredisphpredis

PHPRedis - time() does not work


I have a problem.

In my project I have few servers which modify same data in redis. All the servers may have different time. In other words, I can't rely on app server time in some cases.

So I would like to use Redis time as general base point.

Everything could be fine, PHPRedis documentation says there is a function which does exactly what I need: time().

But here is a BIG surprise:

//... create connection etc...
var_dump($redis->time());

Output is:

bool(false)

The question is - what the hell is this? Have you ever faced such a problem?

How can I get redis server time using it's functions?

Note: other redis functions work fine, I am able to read and write data. Connection is ok and is created correctly.


Solution

  • Got no help here.

    As well as anywhere else.

    So I needed to invent a walkaround...

    Here is it, if someone needs it.

        // Generate unique temporary key
        $key = uniqid() . time();
    
        // Take some random future TS
        $future_ts = time() + 1000;
    
        $redis->setex($key, time(), 5);
        $redis->expireAt($key, $future_ts);
    
        // This variable now contains what I need
        $redis_ts = $future_ts - $redis->ttl($key);