【发布时间】:2016-09-22 14:28:28
【问题描述】:
我正在尝试使用 Lua 脚本在哈希中搜索字段值,但我做错了 :) 我有关键的“文章”,它是 zset 持有文章 ID 和关键文章:n 其中“n”是文章编号。 以下脚本:
local ids = redis.call("zrange", 'articles', '0', '-1')
local ret = {}
for k, id in pairs(ids) do
local row = redis.call("hgetall", "article:"..id)
ret[k] = row
end
return ret
返回这个:
1) 1) "slug"
2) "first-title"
3) "title"
4) "first title"
2) 1) "slug"
2) "second-title"
3) "title"
4) "second title"
我试图包含条件以仅返回标题中包含字符串“秒”的键,但它什么也不返回。
local ids = redis.call("zrange", 'articles', '0', '-1')
local ret = {}
for k, id in pairs(ids) do
local row = redis.call("hgetall", "article:"..id)
if (string.find(row[4], "second")) then
ret[k] = row
end
end
return ret
你能帮帮我吗?
【问题讨论】:
-
不管条件有什么问题,您应该注意脚本中使用的所有键名都应该使用
KEYS参数表传递 - 动态访问键(例如,基于Sorted Set range) 不能保证在集群环境中工作。