只对排序的数字list进行分段存取,性能上有所下降,但是没有测试下降多少
list_add.lua
local config = redis.call('config','get','list-max-ziplist-entries')
local maxzipNum=tonumber(config[2])
local metainfo=KEYS[1]..':split:meta'
local metainfonum=KEYS[1]..':split:meta:num'
local tmpLen,tmpKey,tmpV
local argvLen = #ARGV
if argvLen ~= 1 then
return false
end
local firstelement = redis.call('lrange',metainfo,0,0)
local firstKey,firstLen
if (#firstelement == 0) then
firstKey = KEYS[1]..':'..ARGV[1]
firstLen = 0
else
firstKey = KEYS[1]..':'..firstelement[1]
firstLen = redis.call('llen', firstKey)
end
tmpV = table.remove(ARGV,1);
if (firstLen > 0 and firstLen < maxzipNum) then
redis.call('lpush',firstKey, tmpV)
else
redis.call('lpush', metainfo, tmpV)
tmpKey = KEYS[1] .. ':' .. tmpV;
redis.call('lpush',tmpKey, tmpV)
end
return redis.call('incrby',metainfonum,1)
local maxzipNum=tonumber(config[2])
local metainfo=KEYS[1]..':split:meta'
local metainfonum=KEYS[1]..':split:meta:num'
local tmpLen,tmpKey,tmpV
local argvLen = #ARGV
if argvLen ~= 1 then
return false
end
local firstelement = redis.call('lrange',metainfo,0,0)
local firstKey,firstLen
if (#firstelement == 0) then
firstKey = KEYS[1]..':'..ARGV[1]
firstLen = 0
else
firstKey = KEYS[1]..':'..firstelement[1]
firstLen = redis.call('llen', firstKey)
end
tmpV = table.remove(ARGV,1);
if (firstLen > 0 and firstLen < maxzipNum) then
redis.call('lpush',firstKey, tmpV)
else
redis.call('lpush', metainfo, tmpV)
tmpKey = KEYS[1] .. ':' .. tmpV;
redis.call('lpush',tmpKey, tmpV)
end
return redis.call('incrby',metainfonum,1)
test_add.sh
#!/bin/bash
cmd=`cat list_add.lua`
for ((i=1; i<=$1; i++))
do
#a="eval \"$cmd\" 1 user:box:31458 $i"
a="lpush testlist $i"
echo $a|redis-cli
done
cmd=`cat list_add.lua`
for ((i=1; i<=$1; i++))
do
#a="eval \"$cmd\" 1 user:box:31458 $i"
a="lpush testlist $i"
echo $a|redis-cli
done

本文介绍了如何使用Lua脚本在Redis中对已排序的数字List进行分段处理,包括添加、获取数量、删除和批量添加元素。通过lua脚本优化了性能,虽然性能有所下降,但未具体测试下降程度。
最低0.47元/天 解锁文章
5951

被折叠的 条评论
为什么被折叠?



