Redis实战系列(6) 利用lua对redis特定类型元素的list进行分段
2012-06-07 14:19
441 查看
只对排序的数字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)
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
list_getnum.lua
local metainfonum=KEYS[1]..':split:meta:num'
return redis.call('get',metainfonum)
test_getnum.sh
#!/bin/bash
cmd=`cat list_getnum.lua`
a="eval \"$cmd\" 1 user:box:31458 "
echo $a|redis-cli
list_remove.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 tmpKey,tmpV,tmpV1
local argvLen = #ARGV
if argvLen ~= 1 then
return false
end
tmpV = table.remove(ARGV,1)
local tmpTable = redis.call('lrange', metainfo, 0,-1)
if (#tmpTable == 0) then
return false
end
for _,v in ipairs(tmpTable) do
if (v <= tmpV) then
tmpV1 = v
break
end
end
tmpKey = KEYS[1] .. ':' .. tmpV1;
if (redis.call('lrem',tmpKey, 1, tmpV) ~= 0) then
return redis.call('incrby',metainfonum,-1)
end
return redis.call('get',metainfonum)
test_remove.sh
#!/bin/bash
a=`cat list_remove.lua`
a="eval \"$a\" 1 user:box:31458 26"
echo $a|redis-cli
list_getlist.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 offset = tonumber(ARGV[1])
local pagesize = tonumber(ARGV[2])
if offset < 0 then
offset = 0
end
if pagesize < 1 then
pagesize = 1
end
local len = tonumber(redis.call('get',metainfonum));
if (len == nil) then
return false
end
if (offset >= len) then
return false
end
local element = redis.call('lrange',metainfo,0,-1)
local tmpKey,tmpLen, tmpTable
local i=1
if (element == nil) then
return false
end
while (true) do
if element[i] == nil then
return false
end
tmpKey = KEYS[1]..':'..element[i]
if (i==1)then
tmpLen = redis.call('llen',tmpKey)
else
tmpLen = maxzipNum
end
offset = offset - tmpLen
if offset < 0 then
offset = offset + tmpLen
break
end
i = i+1
end
local tmpList = {}
tmpLen = pagesize
while (#tmpList < pagesize) do
if element[i] == nil then
break;
end
tmpKey = KEYS[1]..':'..element[i]
tmpTable = redis.call('lrange',tmpKey, offset, offset+tmpLen-1)
if (#tmpTable == pagesize) then
tmpList = tmpTable
break
end
if (#tmpTable == 0) then
break;
end
for _,v in ipairs(tmpTable) do
table.insert(tmpList,v)
end
i = i+1
tmpLen = tmpLen - #tmpTable
if (offset > 0) then
offset = 0
end
end
return tmpList
test_getlist.sh
#!/bin/bash
a=`cat list_getlist.lua`
a="eval \"$a\" 1 user:box:31458 $1 $2 "
echo $a|redis-cli
list_madd.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
local increment = argvLen
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
while(argvLen > 0 and firstLen > 0 and firstLen < maxzipNum) do
tmpV = table.remove(ARGV,1);
if tmpV == nil then
break
end
redis.call('lpush',firstKey, tmpV)
firstLen = firstLen+1
argvLen = argvLen - 1
end
while (argvLen > 0) do
tmpKey = KEYS[1]..':'..ARGV[1];
tmpLen = 0;
redis.call('lpush', metainfo, ARGV[1])
while(tmpLen < maxzipNum) do
tmpV = table.remove(ARGV,1);
if tmpV == nil then
break
end
redis.call('lpush',tmpKey, tmpV)
tmpLen = tmpLen+1
end
argvLen = argvLen-tmpLen
end
return redis.call('incrby',metainfonum,increment)
test_madd.sh
#!/bin/bash
seq=''
cmd=`cat list_madd.lua`
for ((i=1; i<=$1; i++))
do
seq="$seq $i"
if (($i%100==0))
then
a="eval \"$cmd\" 1 user:box:31458 $seq"
#a="lpush testlist $seq"
echo $a| redis-cli
seq=''
fi
done
a="eval \"$cmd\" 1 user:box:31458 $seq"
echo $a| redis-cli
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)
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
list_getnum.lua
local metainfonum=KEYS[1]..':split:meta:num'
return redis.call('get',metainfonum)
test_getnum.sh
#!/bin/bash
cmd=`cat list_getnum.lua`
a="eval \"$cmd\" 1 user:box:31458 "
echo $a|redis-cli
list_remove.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 tmpKey,tmpV,tmpV1
local argvLen = #ARGV
if argvLen ~= 1 then
return false
end
tmpV = table.remove(ARGV,1)
local tmpTable = redis.call('lrange', metainfo, 0,-1)
if (#tmpTable == 0) then
return false
end
for _,v in ipairs(tmpTable) do
if (v <= tmpV) then
tmpV1 = v
break
end
end
tmpKey = KEYS[1] .. ':' .. tmpV1;
if (redis.call('lrem',tmpKey, 1, tmpV) ~= 0) then
return redis.call('incrby',metainfonum,-1)
end
return redis.call('get',metainfonum)
test_remove.sh
#!/bin/bash
a=`cat list_remove.lua`
a="eval \"$a\" 1 user:box:31458 26"
echo $a|redis-cli
list_getlist.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 offset = tonumber(ARGV[1])
local pagesize = tonumber(ARGV[2])
if offset < 0 then
offset = 0
end
if pagesize < 1 then
pagesize = 1
end
local len = tonumber(redis.call('get',metainfonum));
if (len == nil) then
return false
end
if (offset >= len) then
return false
end
local element = redis.call('lrange',metainfo,0,-1)
local tmpKey,tmpLen, tmpTable
local i=1
if (element == nil) then
return false
end
while (true) do
if element[i] == nil then
return false
end
tmpKey = KEYS[1]..':'..element[i]
if (i==1)then
tmpLen = redis.call('llen',tmpKey)
else
tmpLen = maxzipNum
end
offset = offset - tmpLen
if offset < 0 then
offset = offset + tmpLen
break
end
i = i+1
end
local tmpList = {}
tmpLen = pagesize
while (#tmpList < pagesize) do
if element[i] == nil then
break;
end
tmpKey = KEYS[1]..':'..element[i]
tmpTable = redis.call('lrange',tmpKey, offset, offset+tmpLen-1)
if (#tmpTable == pagesize) then
tmpList = tmpTable
break
end
if (#tmpTable == 0) then
break;
end
for _,v in ipairs(tmpTable) do
table.insert(tmpList,v)
end
i = i+1
tmpLen = tmpLen - #tmpTable
if (offset > 0) then
offset = 0
end
end
return tmpList
test_getlist.sh
#!/bin/bash
a=`cat list_getlist.lua`
a="eval \"$a\" 1 user:box:31458 $1 $2 "
echo $a|redis-cli
list_madd.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
local increment = argvLen
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
while(argvLen > 0 and firstLen > 0 and firstLen < maxzipNum) do
tmpV = table.remove(ARGV,1);
if tmpV == nil then
break
end
redis.call('lpush',firstKey, tmpV)
firstLen = firstLen+1
argvLen = argvLen - 1
end
while (argvLen > 0) do
tmpKey = KEYS[1]..':'..ARGV[1];
tmpLen = 0;
redis.call('lpush', metainfo, ARGV[1])
while(tmpLen < maxzipNum) do
tmpV = table.remove(ARGV,1);
if tmpV == nil then
break
end
redis.call('lpush',tmpKey, tmpV)
tmpLen = tmpLen+1
end
argvLen = argvLen-tmpLen
end
return redis.call('incrby',metainfonum,increment)
test_madd.sh
#!/bin/bash
seq=''
cmd=`cat list_madd.lua`
for ((i=1; i<=$1; i++))
do
seq="$seq $i"
if (($i%100==0))
then
a="eval \"$cmd\" 1 user:box:31458 $seq"
#a="lpush testlist $seq"
echo $a| redis-cli
seq=''
fi
done
a="eval \"$cmd\" 1 user:box:31458 $seq"
echo $a| redis-cli
相关文章推荐
- 利用Redis数据类型的特点解决特定问题
- 编程技巧系列(2)Java 集合(List,Set,Map)遍历时有条件删除特定元素
- 如何利用C++ vector的sort算法对元素是自定义类型进行排序
- RHEL6入门系列之二十五,利用yum进行软件管理
- 【101】java的List中使用filter过滤出符合特定条件的元素List
- redis类型讲解[string, list, set, sorted set, hash]
- redis类型[string 、list 、 set 、sorted set 、hash]
- XmlTransform 对xml中的特定名称空间的元素进行填充
- 怎样在Redis通过StackExchange.Redis 存储集合类型List
- Flex中如何利用rowCount属性,使List控件Resize时特定的行被显示
- 利用随机类生成数组,并用冒泡排序以及选择排序对不同数组的元素进行从小到大的排序
- Exchange Server2010系列之八:利用通讯组方便地进行收件人管理
- Redis系列四:redis支持的数据类型
- Java对List集合中的元素进行排序
- redis学习系列--3.redis-hashes 类型
- Redis系列-存储篇list主要操作函数小结(转)
- 对List集合中的元素进行排序
- Redis的List列表类型
- 一个完整利用InternetOpen等系列函数进行下载的测试例子
- 对List集合中的元素进行排序