注意:本脚本只适用于数据量较少的集合进行排序,数据量太大会严重影响redis的性能。
local t = redis.call('hgetall', KEYS[1]); local arr = {}; for i, v in pairs(t) do if i % 2 == 0 then local j = cjson.decode(v) if j.language == ARGV[1] then j.languageEquals = 1 else j.languageEquals = 0 end; if j.country == ARGV[2] then j.countryEquals = 1 else j.countryEquals = 0 end; table.insert(arr, j) end end table.sort(arr, function (a, b) if a.languageEquals - b.languageEquals == 0 then if a.countryEquals - b.countryEquals == 0 then return a.createTime < b.createTime else return a.countryEquals > b.countryEquals end; end; return a.languageEquals > b.languageEquals; end) return cjson.encode(arr);
标签:countryEquals,arr,languageEquals,end,redis,return,lua,hash From: https://www.cnblogs.com/math-and-it/p/17318770.html