简体   繁体   English

PHPRedis-time()不起作用

[英]PHPRedis - time() does not work

I have a problem. 我有个问题。

In my project I have few servers which modify same data in redis. 在我的项目中,我很少有服务器在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. 所以我想将Redis时间用作一般基点。

Everything could be fine, PHPRedis documentation says there is a function which does exactly what I need: time() . 一切都可以,PHPRedis文档说有一个函数可以完全满足我的需要: 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? 我如何使用它的功能获取Redis服务器时间?

Note: other redis functions work fine, I am able to read and write data. 注意:其他redis函数工作正常,我能够读写数据。 Connection is ok and is created correctly. 连接正常,创建正确。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM