简体   繁体   English

Redis:KEYS *的结果如何排序?

[英]Redis: How is the result of KEYS * sorted?

I have a very simple php redis application which creates keys at certain events. 我有一个非常简单的php redis应用程序,可以在某些事件中创建密钥。 All the keys are just counters and have an expire time of 24 hours. 所有钥匙都只是柜台,过期时间为24小时。 Basically a rolling window of 24 hours for every key to gather some statistics. 基本上是每个键24小时滚动窗口,以收集一些统计信息。

if ($redis->exists($key)) {
    $redis->incr($key); 
}
else {
    $redis->set($key, '1');
    $now = time(); // current timestamp
    $redis->expireAt($key, $now + 86400);
}

When I extract an overview of all my keys with $list = $redis->keys("*"); 当我使用$list = $redis->keys("*");提取所有键的概述时$list = $redis->keys("*"); (or in a redis-cli console with keys * ), I would suspect a chronological order by creation date. (或者在带有keys *的redis-cli控制台中),我怀疑按创建日期按时间顺序排列。 However this is not case. 然而,事实并非如此。 Neither are they ordered alphabetically, by value... 它们都没有按字母顺序排序,按价值排序......

So my question is, how is this list sorted? 所以我的问题是,这个列表是如何排序的?

First of all, don't use keys * it is debug function not designed for a production, you can kill your server... If you need enumerate all keys in DB in safe way, use SCAN function with LIMIT . 首先,不要使用keys *它不是为生产而设计的调试功能,你可以杀死你的服务器......如果你需要安全地枚举DB中的所有密钥,请使用带LIMIT SCAN功能。

Anyway results of keys or scan is not sorted in any manner, order of results related to internal memory structure of redis's hashtable. 无论如何, keysscan结果都没有以任何方式排序,结果的顺序与redis的哈希表的内部存储器结构有关。

About your php script, you can do it by a single command, without exists set expireat just run: 关于你的php脚本,你可以通过一个命令来完成,没有exists set expireat只是运行:

SET key 1 EX 86400 NX

EX 86400 mean expire in 86400 (1 day) seconds from now EX 86400平均值从现在起86400(1天)后到期

NX mean create a key only if it doesn't exists. NX意味着只有在密钥不存在时才创建密钥。

If this command return (nil) run regular INCR key its mean that the key already exists. 如果此命令返回(nil)运行常规INCR key ,则表示该键已存在。 BTW INCR command will not remove your expire settings. BTW INCR命令不会删除您的过期设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM