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.

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

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了