【发布时间】:2016-08-27 00:22:13
【问题描述】:
我在 redis 密钥库中有一个列表。它包含日期作为键名,像这样。
key
===
20160429
20160430
20160501
20160502
现在我想键入最后两个键,为此我在我的 lua 脚本中执行以下操作。
local data = {};
local keyslist = redis.call('keys', 'stats:day:*');
local key, users, redisData;
-- keyslist = #keyslist.sort(#keyslist, function(a, b) return a[2] > b[2] end);
-- keyslist = #keyslist.sort(#keyslist, function(a,b) if a>b then return true; else return false; end end);
for iCtr = 1, #keyslist do
key = string.gsub(keyslist[iCtr], 'stats:day:','');
redisData = redis.call('hmget', keyslist[iCtr], 'image','video');
table.insert(data, {date=key, imgctr=redisData[1], vidctr=redisData[2]});
if iCtr == 2 then break end
end
但这是返回前 2 条记录,我需要最后 2 条记录(例如以下键)
20160501
20160502
如何获取降序键列表?
【问题讨论】: