【问题标题】:Querying arrays in Redis with Predis使用 Predis 在 Redis 中查询数组
【发布时间】:2014-07-13 12:24:28
【问题描述】:

我想用 Predis 在 Redis 中存储和“选择”数组,我输入的数据如下:

$redis->hset("account_id_".$account_id, "access_time", time());

所以我把这个结构存储在redis中

db0
    account_id_1
        access_time: 1400901850
        ...
        other values
    account_id_2
        access_time: 1400901862
        ...
        other values
    ...
    other_accounts

我想选择一系列 unix 时间戳中的所有帐户,但到目前为止我还没有找到方法,我怀疑数据结构是否适合此目的。

【问题讨论】:

    标签: php redis predis nosql


    【解决方案1】:

    我怀疑数据结构是否适用于此目的。

    您可以使用散列来存储帐户数据,但如果您想要比在 access_time 字段上进行 O(N) 查找更好,则需要使用其他数据结构。也就是说,您应该使用排序集。

    您的数据库中应该有一个名为account:access_time 的排序集类型的附加键。当访问一个帐户时,应该运行以下 redis 命令(当然要填写适当的变量):

    ZADD account:access_time $access_time $account_id
    

    稍后当您想要根据访问时间进行查找时,请运行以下命令:

    ZRANGEBYSCORE account:access_time $min_access_time $max_access_time
    

    上述命令将返回一个帐户 ID 列表,然后您可以对其进行操作。

    【讨论】:

    • 非常感谢蒂姆,这正是我想要的
    猜你喜欢
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2016-05-26
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多