求教大佬,线上 redis cluster 中适合使用 redis lua script 吗?以及使用时有什么注意事项? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ryan961
V2EX    Redis

求教大佬,线上 redis cluster 中适合使用 redis lua script 吗?以及使用时有什么注意事项?

  •  
  •   ryan961 2023-07-18 12:02:30 +08:00 1420 次点击
    这是一个创建于 888 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我将一部分逻辑代码通过 lua 脚本实现,变相的实现原子操作(如下),但之前使用 lua 脚本是遇到过 not in the same slot 的错误,感觉我的这个脚本可能与会遇到。如果我真的通过 key 加前缀的方式保证他们在同一个 slot 内是不是也会导致 cluster 处于一个不健康的集群状态。

    local userAttributiOnKey= KEYS[1]; local humeSource = KEYS[2]; local attributeKeys = cjson.decode(KEYS[3]); if redis.call("EXISTS", userAttributionKey) == 1 then -- get attribution cache local res = redis.call("GET", userAttributionKey); if res then return { 1, res }; end end local matchResult = {}; local hits = {}; local latestTime = 0; for i = 1, #attributeKeys do local indexInfoString = redis.call("GET", attributeKeys[i]); if indexInfoString then local indexInfo = cjson.decode(indexInfoString); local reportSource = indexInfo['report_source']; local uniqId = indexInfo['uniq_id']; local timestamp = tonumber(indexInfo['timestamp']); if humeSource == '' or humeSource == indexInfo['report_source'] then -- check attribute index info if reportSource ~= '' and uniqId ~= '' then if timestamp > latestTime then indexInfo['hit'] = attributeKeys[i]; indexInfo['match_res'] = 'matched'; matchResult = indexInfo; latestTime = timestamp; end table.insert(hits, attributeKeys[i]); else return redis.error_reply("Invalid Info:" .. indexInfoString); end end -- delete attribute key cache redis.call("DEL", attributeKeys[i]) end end if #hits > 0 then matchResult['hits'] = hits; local matchResultStr = cjson.encode(matchResult); -- set attribution cache redis.call("SET", userAttributionKey, matchResultStr, "EX", ARGV[1]); return { 1, matchResultStr }; end return { 0, cjson.encode(matchResult) }; 

    请教下大佬和前辈,我这是不是一种错误的使用方式?以及 lua script 使用时有什么注意事项?

    guyeu
        1
    guyeu  
       2023-07-19 14:02:46 +08:00
    加 key 能保证在同一个 slot 吗?
    guyeu
        2
    guyeu  
       2023-07-19 14:03:01 +08:00
    key 加前缀
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5686 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 40ms UTC 06:16 PVG 14:16 LAX 22:16 JFK 01:16
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86