php - 使用 Predis 查询 Redis 中的数组

标签 php redis predis nosql

我想用 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 时间戳中的所有帐户,但到目前为止我还没有找到方法,我怀疑数据结构是否适合此目的。

最佳答案

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

您可以使用散列来存储帐户数据,但如果您想在 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 列表,然后您可以对其进行操作。

关于php - 使用 Predis 查询 Redis 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23841149/

相关文章:

php - 为什么这个登录页面没有检测到正确的密码?

php - 将路径数组转换为 UL 列表

php - 如何高度模式 4 :3 works in wordpress?

mongodb - 正在寻找 DynamoDB/MongoDB+Redis 组合?

Redis ERR 未知命令 'BZPOPMIN'

php - fatal error : Class 'Predis\Client' not found

php - php中日语字符的编码问题

Redis SCAN 命令返回与模式不匹配的键

php - predis hmset 嵌套数组值

php - 可以从 REDIS 订阅返回响应吗?