我想用 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/