定时删除公司费用
- 查询费用并删除数据,保持原子操作
- 准备数据
sadd sms:send:batchDeduct:company 000002
sismember sms:send:batchDeduct:company 000002
hgetall sms:send:batchDeduct:company:000002
hmset sms:send:batchDeduct:company:000002 name "张三"
hincrby sms:send:batchDeduct:company:000002 num 100
- 编写LUA脚本
eval "redis.call('srem',KEYS[1],KEYS[2]) local flat_map=redis.call('hgetall',KEYS[3]) local result={} for i=1,#flat_map,2 do result[flat_map[i]]=flat_map[i+1] end redis.call('del',KEYS[3]) return cjson.encode(result)" 3 sms:send:batchDeduct:company 000002 sms:send:batchDeduct:company:000002
- 将脚本加载到redis中,并执行
script load "redis.call('srem',KEYS[1],KEYS[2]) local flat_map=redis.call('hgetall',KEYS[3]) local result={} for i=1,#flat_map,2 do result[flat_map[i]]=flat_map[i+1] end redis.call('del',KEYS[3]) return cjson.encode(result)"
evalsha bd5216e08be2590520bd3e1e81bff535a340dcef 3 sms:send:batchDeduct:company 000002 sms:send:batchDeduct:company:000002
标签:flat,000002,batchDeduct,REDIS,company,sms,send,LUA,1102
From: https://www.cnblogs.com/wanwei2018/p/16717752.html