问题描述
我有一些这样写的代码:
I have some code written like this:
foreach ($models as $model) {
Redis::hset('model_App\ServiceModel', $model[$primaryKey], json_encode($model->toArray()));
}
模型按名称字段的字母顺序排列在 foreach 之前(alpha、green、zed)
The models are ordered alphabetically by name field prior to the foreach (alpha, green, zed)
数据,一旦添加到redis,看起来像这样:
The data, once added to redis, looks something like this:
row | key | value
1 490 {"service_id":490, "name":"zed"}
2 489 {"service_id":489, "name":"alpha"}
3 491 {"service_id":491, "name": "green"}
有谁知道为什么我的订单被忽略/覆盖?我是否缺少有关 redis 工作原理的信息?'row' 是否像 MySQL 数据库中的自动递增 ID,如果是这样 - 它是如何在 'alpha' 之前变成 'zed' 的?
Does anyone know why my ordering is being ignored / overwritten? Am I missing something about how redis works? Is 'row' like an auto incrementing id in a MySQL DB, and if so - how is it getting 'zed' before 'alpha'?
这是我第一次认真"使用它,我很感激我能得到的所有建议.
This is the first time I am 'seriously' using it, and I appreciate all the advice I can get.
推荐答案
Redis 的哈希是无序的 - 这是预期的行为.如果需要排序,请考虑使用排序集,SORT
命令,或(最好)对客户端中的数据进行排序.
Redis' Hashes are unordered - that is the expected behavior. If sorting is needed, either look into using Sorted Sets, the SORT
command, or (preferably) sorting the data in the client.
这篇关于Redis 插入乱序,或排序奇怪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!