duanmajing9332 2014-05-24 03:53
浏览 139
已采纳

使用Predis查询Redis中的数组

I want to to store and "SELECT" arrays in Redis with Predis, I entered data as follow:

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

So I have this structure stored in redis

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

I want to select all accounts in a range of unix timestamps but I haven't find a way so far, I'm in doubt if the data structure is correct for this purpose.

  • 写回答

1条回答 默认 最新

  • duanque3125 2014-05-24 22:15
    关注

    i'm in doubt if the data structure is correct for this purpose.

    Your use of a hash is fine for storing the account data, but you need to make use of another data structure if you want better than O(N) lookup on the access_time field. Namely, you should use a sorted set.

    You should have an additional key in your database called account:access_time of type sorted set. When an account is accessed, the following redis command should be run (with the appropriate variables filled in, of course):

    ZADD account:access_time $access_time $account_id
    

    Later on when you want to do a lookup based on access time, run the following:

    ZRANGEBYSCORE account:access_time $min_access_time $max_access_time
    

    The above command will return a list of account IDs that you can then do your work on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站